【发布时间】:2011-08-31 03:43:44
【问题描述】:
我正试图弄清楚如何为我的购物车设置一个干净的架构,而不会过度架构它或以一个贫乏的领域模型告终。现在我只想使用没有任何 ORM 框架的标准 ADO 数据库逻辑。 (我也在学习 EF4.1,但还不够好,无法在生产中使用)
理想情况下,我只需要为每个业务对象/实体创建一个 POCO,以及一个用于处理持久性的存储库/数据类。为简单起见,我正在考虑将 POCO 紧密耦合到数据层,它将返回 POCO。如果我也将 DTO 添加到组合中,那么我最终会为每个区域(gc、订单、项目、付款等)拥有 5-6 个类文件,这对于一个简单的应用程序来说似乎太多了。我以后总是可以改进的。
我正在做的第一堂课是礼券。其中一种方法是创建一个新的 GC。在这种方法中,我需要查询数据库以确保系统中不存在新代码。在这个方法中只调用数据层/repo 可以吗?
数据层/存储库应该是静态的吗?我应该只通过 POCO 本身暴露它吗?
我是否应该完全放弃数据层,直接在我的 POCO 中调用数据(活动记录样式)?
我需要一个简单的架构,它可以让我在不使事情过于复杂的情况下分离一些关注点。至少在未来几年内,数据库提供程序和表结构不会改变。
这是一些代码.. 只需要弄清楚零件的去向即可。
public GiftCertificateModel
{
public int GiftCerticiateId {get;set;}
public string Code {get;set;}
public decimal Amount {get;set;}
public DateTime ExpirationDate {get;set;}
public void Redeem(string code, decimal amount)
{
//need to validate the input
//need to insert a record to the transaction log table (call the repo or does this entire method need to be in the repo?)
}
public void GetNewCode()
{
//need to create random alpha num code
//need to make sure the code is unique in the db... (again, (call the repo or does this entire method need to be in the repo?
}
}
public GiftCertificateRepo : DALBase (DALBase has methods for connecting, etc)
{
//mapping code here to map SQLDataReader values to GiftCertificateModel properties
//i can also setup separate DTOs if it makes more sense...
//should the repo be static?
public static GiftCertificateModel GetById(int GiftCertificateId)
{
//call db to get one and return single model
}
public static bool IsUnique(string code)
{
//call db to see if any records exists for code
}
public static List<GiftCertificateModel> GetMany()
{
//call db to get many and return list
}
public static void Save(GiftCertificateModel gc)
{
//call db to save
}
}
调用代码:
GiftCertificateModel gc = new GiftCertificateModel();
gc.Code = gc.GetNewCode(); //do i call the is unique here or in the GetNewCode method?
gc.Amount = 10;
gc.ExpirationDate = "1/1/2012";
GiftCertificateRepo.Save(gc);
【问题讨论】:
-
请不要给你的GC一个Collect()方法。
-
您不能在数据库级别创建您的 NewCode 吗?在我的应用程序中,我通常在保存新记录之前在存储过程中创建一个新的 guid。
标签: c# asp.net architecture poco