使用传统 ado.net 对聚合根进行 CRUD

发布于 2024-08-02 13:32:32 字数 47 浏览 3 评论 0原文

谁能向我展示使用传统 ado.net 聚合根的简单 CRUD 语句? 提前致谢!

Can anyone show me simple CRUD statements for aggregate root using traditional ado.net?
Thanks in advance!

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

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

发布评论

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

评论(1

酒中人 2024-08-09 13:32:32

(这是在假设使用 GUID 或某些非数据库生成的主键的情况下编写的)
此外,许多锅炉代码(例如连接管理等)应该移动到存储库的基类中。
如果 Order 是聚合根,则可能应该将 OrderLineRepo 设为程序集私有,

public class OrderRepository : Repository

{
    public void Save(Order order)
    {
        if(order.IsDirty)
        {
                    //sets up connection if required, command and sql
            ICommand command = BuildCommandForSave(order);  
            command.Execute();
            OrderLineRepository orderLineRepo = GetOrderLineRepo();
            foreach(OrderLine line in order.OrderLines)
            {
                orderLineRepo.Save(line);
            }
        }
    }
}

但是我要强调的是,这实际上是一个简单的幼稚实现,并且如果按照要求执行 DDD,我个人会使用像 nHibernate 这样的 ORM 来实现持久性对于一个经过良好测试的持久层来说,这是不平凡的

此外,这还假设 IsDirty 函数考虑了子项 - 我们还需要一种方法来查看订单是否是新的/编辑的,而不仅仅是脏的

(This is written on the assumption that a GUID or some non-database generated primary key is used)
Also alot of boiler code such as connection management etc... should be moved to a base class for Repository.
If Order is the aggregate root, one possibly should make OrderLineRepo private to the assembly

public class OrderRepository : Repository

{
    public void Save(Order order)
    {
        if(order.IsDirty)
        {
                    //sets up connection if required, command and sql
            ICommand command = BuildCommandForSave(order);  
            command.Execute();
            OrderLineRepository orderLineRepo = GetOrderLineRepo();
            foreach(OrderLine line in order.OrderLines)
            {
                orderLineRepo.Save(line);
            }
        }
    }
}

However I'd stress that this is really a simple naive implementation, and that I'd personally utilize an ORM like nHibernate for my persistence if doing DDD as the requirements for a good well tested persistence layer are non-trivial

Also this assumes that the IsDirty function takes children into account - we would also require a means to see if the order is new/edited, not just dirty

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