架构模式逻辑层模式之:表模块(Table Model)
2014-08-13 14:26
134 查看
表模块和领域模型比,有两个显著区别:
1:表模块中的类和数据库表基本一一对应,而领域模型则无此要求;
2:表模块中的类的对象处理表中的所有记录,而领域模型的一个对象代表表中的一行记录;
一般情况下,我们可以基于第二点来严格区分你的设计是表模块的,还是领域模型的。如:如果我们有许多订单,则领域模型的每一个订单都有一个对象,而表模块只有一个对象来处理所有订单(注意,这里的类,都是指业务逻辑层的类,而不是实体类。表模块的类的对象和常规的领域模型的对象很类似,但是关键区别是:它没有标识符来标出它所代表的实体对象)。举例来说,如果要查询某个订单,表模块会像这样进行编码:
anOrderModule.GetOrder(string orderId);
因为表模块只有一个对象来处理所有订单,所以表模块可以是一个实例,也可以是一个只包含静态方法的静态类。
表模块 的代码和 事务脚本类似:
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") // 电子表格类
{
// 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") // 数据库
{
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;
}
// 得到哪天前入账了多少
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"];
}
}
1:表模块中的类和数据库表基本一一对应,而领域模型则无此要求;
2:表模块中的类的对象处理表中的所有记录,而领域模型的一个对象代表表中的一行记录;
一般情况下,我们可以基于第二点来严格区分你的设计是表模块的,还是领域模型的。如:如果我们有许多订单,则领域模型的每一个订单都有一个对象,而表模块只有一个对象来处理所有订单(注意,这里的类,都是指业务逻辑层的类,而不是实体类。表模块的类的对象和常规的领域模型的对象很类似,但是关键区别是:它没有标识符来标出它所代表的实体对象)。举例来说,如果要查询某个订单,表模块会像这样进行编码:
anOrderModule.GetOrder(string orderId);
因为表模块只有一个对象来处理所有订单,所以表模块可以是一个实例,也可以是一个只包含静态方法的静态类。
表模块 的代码和 事务脚本类似:
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") // 电子表格类
{
// 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") // 数据库
{
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;
}
// 得到哪天前入账了多少
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"];
}
}
相关文章推荐
- 再谈业务逻辑架构模式(事务脚本,表模块,活动记录,领域模型)
- 业务逻辑架构模式(事务脚本,表模块,活动记录,领域模型)
- [转编] 企业架构模式之领域逻辑模式的实现(事务脚本、领域模型、表模块、活动记录)
- asp.net mvc(模式)和三层架构(BLL、DAL、Model)的联系与区别
- .NET框架设计(高级框架架构模式)―钝化程序、逻辑冻结、冻结程序的延续、瞬间转移
- 重析业务逻辑架构模式,浅浅滴
- MVC模式中如何区分应用程序逻辑(Controller层)和业务逻辑(Model层)
- mvvm架构模式中的Command与ViewModel的关系
- 重析业务逻辑架构模式
- 业务逻辑架构模式
- .NET框架设计(高级框架架构模式)—钝化程序、逻辑冻结、冻结程序的延续、瞬间转移
- asp.net mvc(模式)和三层架构(BLL、DAL、Model)的联系与区别
- 企业应用架构模式 之 领域逻辑模式
- asp.net mvc(模式)和三层架构(BLL、DAL、Model)的联系与区别 转载自:http://blog.csdn.net/luoyeyu1989/article/details/8275866
- QAbstractTableModel ------ Model-View-Control(MVC)架构(3)
- .NET框架设计(高级框架架构模式)—钝化程序、逻辑冻结、冻结程序的延续、瞬间转移
- Software Model - 软件架构模式和设计模式
- asp.net mvc(模式)和三层架构(BLL、DAL、Model)的联系与区别
- MVC模式中如何区分应用程序逻辑(Controller层)和业务逻辑(Model层)?
- MySQL架构组成之逻辑模块组成