可以从 POCO 调用数据层/存储库吗?

发布于 2024-11-09 08:17:40 字数 2244 浏览 0 评论 0原文

我试图弄清楚如何为我的购物车设置一个干净的架构,而不会过度构建它或最终导致贫乏的域模型。现在我只想使用标准的 ADO 数据库逻辑,而不使用任何 ORM 框架。 (我也在学习 EF4.1,但还不够好,无法在生产中使用)

理想情况下,我只会为每个业务对象/实体提供一个 POCO 以及一个用于处理持久性的存储库/数据类。为了简单起见,我正在考虑将 POCO 与数据层紧密耦合,并且它将返回 POCO。如果我也将 DTO 添加到组合中,那么最终每个区域(gc、订单、项目、付款等)都会有 5-6 个类文件,这对于一个简单的应用程序来说似乎太多了。我以后随时可以完善。

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

数据层/存储库应该是静态的吗?我应该只通过 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);

I am trying to figure out how to setup a clean architecture for my shopping cart without over architecting it or ending up with a anemic domain model. Right now I just want to use standard ADO database logic without any ORM framework. (I am also learning EF4.1, but not good enough yet to use in production)

Ideally I would just have a POCO for each Business object/entity and a repository/data class that will handle persistence. To keep things simple, I am thinking to tightly couple the POCOs to the data layer and it will return POCOs. If I add DTO to the mix as well, then I end up having 5-6 class files for each area (gc, order, items, payments, etc) which just seems like too much for a simple app. I can always refine later.

The first class I am working on is for gift certificates. One of the methods is to create a new GC. In this method I will need to query the database to ensure the new code does not already exist in the system. Is it ok to just call the data layer/repo inside this method?

Should the data layer/repo be static? Should I only expose it through the POCO itself?

Should I drop the data layer altogether and just have the data calls directly in my POCOs (active record style)?

I need a simple architecture which will allow me some separation of concerns without over complicating things. The database provider and table structures will not change for at least the next few years.

Here is some code.. just need to figure out where the parts go.

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

    }
}

Calling code:

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧故 2024-11-16 08:17:40

根据定义,POCO 是持久性无知的,即它不知道这一点以及如何持久化。

如果您将业务对象耦合到存储库或持久层,根据您的情况,这可能完全没问题,但它不再是 POCO。

By definition a POCO is persistance ignorant, i.e. it doesn't know that and how it is being persisted.

If you couple your business objects to your repositories or persistance layer, that might be perfectly fine depending on your circumstances, but it wouldn't be POCOs anymore.

愁以何悠 2024-11-16 08:17:40

有几种系统/意识形态,但他们都同意持久性不是业务类别的一部分。建立一个单独的数据访问层来存储和存储数据。检索您的 POCO。

There are several systems/ideologies out there, but they all agree that Persistence is not part of the business class. Make a separate Data Access Layer for storing & retrieving your POCOs.

浸婚纱 2024-11-16 08:17:40

通过将 POCO(数据表示)与存储库对象(数据如何存储/检索的表示)分开,您将来将能够更轻松地从 ADO 切换到更高级的 ORM 系统 - 这些代码更改将是隔离到一处。

更好的是,定义一个 IRepository 接口并让您的 ADO 存储库类实现它。然后,您的应用程序应该仅与 IRepository 一起使用,从而使将来测试或切换存储库实现变得更加容易。

By keeping the POCOs (representations of data) separate from the repository objects (representations of how data gets stored/retrieved) you will be able to much more easily switch from ADO to a more advanced ORM system in the future - those code changes will be isolated to one place.

Better yet, define an IRepository interface and have your ADO repostiory class implement it. Then, your app should work only with IRepository thus making it even easier to test or switch repository implementations in the future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文