Architecture mode logical layer mode: Table Model)

Source: Internet
Author: User

There are two notable differences between the table module and the domain model:

1: The classes in the table module correspond to the database tables one by one, while the domain model does not;

2: The class objects in the table module process all records in the table, and an object in the domain model represents a row of records in the table;

Generally, we can strictly distinguish whether your design is a table module or a domain model based on the second point. For example, if we have many orders, each order of the domain model has an object, and the table module only has one object to process all orders. (Note that the class here, it refers to the class at the business logic layer, rather than the entity class. The Class Object of the table module is similar to the object of the conventional domain model, but the key difference is that it does not have an identifier to mark the object it represents ). For example, if you want to query an order, the table module will encode it like this:

Anordermodule. getorder (string orderid );

Because the table module only has one object to process all orders, the table module can be an instance or a static class that only contains static methods.

The table module code is similar to the transaction script:

Class tablemodel
{
Protected datatable table;

Protected tablemodel (Dataset ds, string tablename)
{
Table = Ds. Tables [tablename];
}
}

Class contract: tablemodel
{
Public Contract (Dataset DS): Base (DS, "contracts ")
{
}

Public datarow this [long id]
{
Get
{
String filter = "id =" + ID;
Return table. Select (filter) [0];
}
}

Public void calculaterecognitions (long contactid)
{
Datarow contractrow = This [contactid];
Double amount = (double) contractrow ["amount"];
Revenuerecognition RR = new revenuerecognition (table. dataset );
Product prod = new product (table. dataset );
Long prodid = getproductid (contactid );

If (prod. getproducttype (prodid) = "W ")
{
Rr. insert (contactid, amount, (datetime) getwhensigned (contactid ));
} Else if (prod. getproducttype (prodid) = "S") // workbook class
{
// The SQL "insert into revenuecongnitions (contract, amount, recognizedon) values (?,?,?) "
Datetime datesigned = (datetime) getwhensigned (contactid );
Rr. insert (contactid, amount/3, datesigned );
Rr. insert (contactid, amount/3, datesigned. adddays (60 ));
Rr. insert (contactid, amount/3, datesigned. adddays (90 ));
} Else if (prod. getproducttype (prodid) = "D") // Database
{
Datetime datesigned = (datetime) getwhensigned (contactid );
Rr. insert (contactid, amount/3, datesigned );
Rr. insert (contactid, amount/3, datesigned. adddays (30 ));
Rr. insert (contactid, amount/3, datesigned. adddays (60 ));
}
}

Private long getproductid (long contactid)
{
Return (long) This [contactid] ["productid"];
}

Private datetime getwhensigned (long contactid)
{
Return (datetime) This [contactid] ["datesigned"];
}
}

Class revenuerecognition: tablemodel
{
Public revenuerecognition (Dataset DS): Base (DS, "revenuerecognitions ")
{
}

Public long insert (long contactid, double amount, datetime whensigned)
{
Datarow newrow = table. newrow ();
Long id = getnextid ();
Newrow ["ID"] = ID;
Newrow ["contactid"] = contactid;
Newrow ["amount"] = amount;
Newrow ["datesigned"] = whensigned;
Table. Rows. Add (newrow );
Return ID;
}

// Obtain the number of records recorded one day ago
Public double recognizedrevenue (long contractnumber, datetime asof)
{
// The SQL "select amount from revenuecongnitions where contract =? And recognizedon <=? ";
String filter = string. Format ("contactid = {0} and date <= # {1: d}", contractnumber, asof );
Datarow [] rows = table. Select (filter );
Double R = 0.0;
Foreach (datarow DR in rows)
{
R + = (double) Dr ["amount"];
}

Return R;
}

Private long getnextid ()
{
Throw new exception ();
}
}

Class product: tablemodel
{
Public Product (Dataset DS): Base (DS, "Products ")
{
}

Public datarow this [long id]
{
Get
{
String filter = "id =" + ID;
Return table. Select (filter) [0];
}
}

Public String getproducttype (long productid)
{
Return (string) This [productid] ["type"];
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.