可以从POCO调用数据层/ repo吗?

jps*_*ook 0 c# architecture asp.net poco

我试图弄清楚如何为我的购物车设置一个干净的架构,而不是过度设计或最终导致贫血的域模型.现在我只想使用没有任何ORM框架的标准ADO数据库逻辑.(我也在学习EF4.1,但还不够好用于制作)

理想情况下,我只为每个Business对象/实体和将处理持久性的存储库/数据类都有一个POCO.为了简单起见,我想将POCO紧密地耦合到数据层,它将返回POCO.如果我也将DTO添加到混音中,那么我最终会为每个区域(gc,顺序,项目,付款等)提供5-6个类文件,这对于简单的应用来说似乎太过分了.我可以随时改进.

我正在做的第一堂课是礼券.其中一种方法是创建一个新的GC.在此方法中,我将需要查询数据库以确保系统中尚不存在新代码.可以在此方法中调用数据层/ repo吗?

数据层/ 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

    }
}
Run Code Online (Sandbox Code Playgroud)

来电代码:

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);
Run Code Online (Sandbox Code Playgroud)

Den*_*aub 5

根据定义,POCO是持久性无知的,即它不知道它以及如何持久存在.

如果您将业务对象耦合到您的存储库或持久层,根据您的具体情况,这可能是完全正常的,但它不再是POCO.