相关疑难解决方法(0)

我在哪里可以找到一些DDD的好例子?

我正在学习领域驱动设计,但是有些实际问题令我感到困惑,我认为看到一些好的样本可能会被清除.

有没有人知道一些很好的工作代码示例,它们可以很好地建模基本的DDD概念?

特别感兴趣

  • 说明性域模型
  • 使用域/应用程序服务
  • 价值对象
  • 聚合根

domain-driven-design

378
推荐指数
7
解决办法
15万
查看次数

为每个对象创建通用存储库与特定存储库的优势?

我们正在开发ASP.NET MVC应用程序,现在正在构建存储库/服务类.我想知道创建一个所有存储库实现的通用IRepository接口是否有任何重大优势,而每个存储库都有自己独特的接口和方法集.

例如:一个通用的IRepository接口可能看起来像(取自这个答案):

public interface IRepository : IDisposable
{
    T[] GetAll<T>();
    T[] GetAll<T>(Expression<Func<T, bool>> filter);
    T GetSingle<T>(Expression<Func<T, bool>> filter);
    T GetSingle<T>(Expression<Func<T, bool>> filter, List<Expression<Func<T, object>>> subSelectors);
    void Delete<T>(T entity);
    void Add<T>(T entity);
    int SaveChanges();
    DbTransaction BeginTransaction();
}
Run Code Online (Sandbox Code Playgroud)

每个Repository都会实现此接口,例如:

  • CustomerRepository:IRepository
  • ProductRepository:IRepository
  • 等等

我们在之前的项目中遵循的备选方案是:

public interface IInvoiceRepository : IDisposable
{
    EntityCollection<InvoiceEntity> GetAllInvoices(int accountId);
    EntityCollection<InvoiceEntity> GetAllInvoices(DateTime theDate);
    InvoiceEntity GetSingleInvoice(int id, bool doFetchRelated);
    InvoiceEntity GetSingleInvoice(DateTime invoiceDate, int accountId); //unique
    InvoiceEntity CreateInvoice();
    InvoiceLineEntity CreateInvoiceLine();
    void SaveChanges(InvoiceEntity); //handles inserts or updates
    void DeleteInvoice(InvoiceEntity);
    void …
Run Code Online (Sandbox Code Playgroud)

architecture asp.net asp.net-mvc

131
推荐指数
4
解决办法
3万
查看次数

何时使用CQRS设计模式?

我和我的团队一直在讨论使用CQRS(Command Query Responsibility Segregation)设计模式,我们仍在努力评估使用它的优缺点.根据:http://martinfowler.com/bliki/CQRS.html

我们还没有看到CQRS在该领域的足够用途,但仍然有信心我们理解其优缺点

那么你们怎么想?问题什么时候需要使用CQRS呢?

architecture design-patterns cqrs

53
推荐指数
8
解决办法
3万
查看次数

在域驱动设计中,在域对象中调用其他对象的repostiories会违反DDD吗?

我正在重构一个正在结束的项目的代码,最后我把很多业务逻辑放在服务类而不是域对象中.此时,大多数域对象仅是数据容器.我决定在服务对象中编写大部分业务逻辑,然后将所有内容重构为更好,更可重用和更易读的形状.这样我就可以决定应该将哪些代码放入域对象中,以及将哪些代码分解为自己的新对象,以及应该在服务类中保留哪些代码.所以我有一些代码:

public decimal CaculateBatchTotal(VendorApplicationBatch batch)
{
     IList<VendorApplication> applications = AppRepo.GetByBatchId(batch.Id);

     if (applications == null || applications.Count == 0)
          throw new ArgumentException("There were no applications for this batch, that shouldn't be possible");
     decimal total = 0m;
     foreach (VendorApplication app in applications)
          total += app.Amount;
     return total;
}
Run Code Online (Sandbox Code Playgroud)

这段代码似乎是对域对象的一个​​很好的补充,因为它只有输入参数才是域对象本身.似乎是一些重构的完美候选人.但唯一的问题是该对象调用另一个对象的存储库.这让我想把它留在服务类中.

我的问题是这样的:

  1. 你会把这段代码放在哪里?
  2. 你会打破这个功能吗?
  3. 那些遵循严格的领域驱动设计的人会把它放在哪里?
  4. 为什么?

谢谢你的时间.

编辑注意:不能在这个上使用ORM,所以我不能使用延迟加载解决方案.

编辑注2:我不能改变构造函数来接受参数,因为将要使用反射(不是我的想法)实现域对象的方式.

编辑注3:我不相信批处理对象应该只能合计任何应用程序列表,看起来它应该只能处理该特定批处理中的应用程序.否则,将函数保留在服务类中对我来说更有意义.

c# service design-patterns domain-driven-design

8
推荐指数
2
解决办法
1579
查看次数

重构访问遗留系统中存储库的域逻辑

我正在使用具有贫血域模型的遗留系统.

域名具有以下实体classses: ,Car,CarType,.CarComponentCarComponentType

对于其中的每一个,都有一个单独的存储库.还有许多服务可以访问这些存储库并且基本上包含所有逻辑.

我需要实现一个方法来确定CarComponentType供应商是否可以停止使用.逻辑如下:只有当前没有现有汽车的组件才能停止组件.

最初,我在服务类中实现了它.

public boolean canBeDiscontinued(CarComponentType carComponentType) {
    List<Car> cars = carRepository.getCarsWithComponent(carComponentType);
    return cars.isEmpty();
}
Run Code Online (Sandbox Code Playgroud)

这有效 - 但是这个逻辑在代码中的其他几个地方使用.它可能会增长,它看起来像是适合类的东西CarComponentType:

public boolean canBeDiscontinued() {
    List<Car> cars = carRepository.getCarsWithComponent(this);
    return cars.isEmpty();   
}
Run Code Online (Sandbox Code Playgroud)

但是,我不能把它放在那里,因为它需要访问存储库(据我所知,它是一个非常严重的反模式,实体要知道数据访问层).加载组件类型时,我无法加载该类型的所有汽车,因为这可能是数千个对象.我们没有使用任何ORM,所以制作一个懒惰的加载集合不仅体积大,而且非常容易出错.

像我第一次在服务类中实际使用此方法更合适吗?这不重要吗?还有另一种选择吗?我应该从另一个起点开始重构吗?

还有一个类似的问题在这里.但是我的问题与Java有关,所以我不认为这个解决方案适用于我的情况.此外,提前抱歉使用汽车和组件作为我的域模型.:)

java refactoring domain-driven-design repository

7
推荐指数
2
解决办法
820
查看次数

使实体类关闭以进行更改

我有一个数据库关系,如下所示.域对象是基于LINQ to SQL ORM创建的.

付款包括现金付款和礼品券付款.假设购买总额为550.可以按以下组件支付

1 Gift Coupon Valued 300

1 Gift Coupon Valued 200

I Cash Currency Valued 50
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我正在使用ORM的"InsertOnSubmit"功能插入新的付款记录.以下代码工作正常.但是,如果我公司使用信用卡引入新的支付组件,我需要更改我的"付款"域类.如何使支付类打开以进行扩展并关闭仍在使用ORM的更改

注意:Payment类具有行为(例如GetTotalAmountCollected).我正在努力使"付款"类满足OCP.

注意:优惠券类型有特定的行为.优惠券发行日期是否小于2000年1月1日,它不应用于计算总金额(即,CouponValue应为零).请参阅使用策略模式重构代码.

注意:我使用的是.Net 4.0

参考:

  1. 将ObjectContext.AddObject与Entity Framework一起使用时出错
  2. 使用策略模式重构代码
  3. 喜欢构成而不是继承?
  4. 代码优先与模型/数据库优先
  5. 使用Unity的策略模式和依赖注入
  6. 委托与OOP的C#策略设计模式
  7. 如何在C#中使用策略模式?
  8. 继承与EF代码第一:第2部分-每个类型表(TPT) http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first -ctp5部分-2-表每类型tpt.aspx

C#代码:

public class PaymentAppService
{
    public RepositoryLayer.ILijosPaymentRepository Repository { get; set; }

    public void MakePayment()
    {
        DBML_Project.Payment paymentEntity = new DBML_Project.Payment();
        paymentEntity.PaymentID = 1;
        paymentEntity.PaymentType = "PurchaseP";

        DBML_Project.CashPayment cashObj = new …
Run Code Online (Sandbox Code Playgroud)

.net c# domain-driven-design entity-framework linq-to-sql

7
推荐指数
1
解决办法
851
查看次数

以下哪些例子代表正确使用DDD?

我已经和DDD合作了几个月了,我遇到了一些我不确定的事情.

采取添加的简单的例子Product,以一个Order对象.从我们的Controller,我们int通过UI传递,代表Product数据库中的一个.以下哪两个例子是正确的(如果它们都错了,请告诉我)?

示例一:

public class OrderController
{ 
    // Injected Repositories
    private readonly IProductRepository _productRepository; 

    // Called by UI
    public void AddProduct(int productId)
    {
        Order order = ...; // Persisted Order
        Product product = _productRepository.GetProduct(productId);
        order.AddProduct(product);
    }
}
Run Code Online (Sandbox Code Playgroud)

Controller实例化产品本身并通过以下方法添加它:

void AddProduct(Product product)
{
    productList.Add(product);
}
Run Code Online (Sandbox Code Playgroud)

示例二:

public class OrderController
{ 
    // Injected Repositories
    private readonly IProductRepository _productRepository; 

    // Called by UI
    public void AddProduct(int productId)
    {
        Order order = ...; // Persisted Order …
Run Code Online (Sandbox Code Playgroud)

c# design-patterns domain-driven-design

5
推荐指数
1
解决办法
500
查看次数