在Domain-Driven-Design中定义应用程序层

nul*_*ter 5 architecture design-patterns domain-driven-design

我有一个关于DDD的问题.我正在构建一个学习DDD的应用程序,我有一个关于分层的问题.我有一个像这样工作的应用程序:

UI层调用=>应用层 - >域层 - >数据库

以下是代码外观的一个小示例:

//****************UI LAYER************************
//Uses Ioc to get the service from the factory.
//This factory would be in the MyApp.Infrastructure.dll
IImplementationFactory factory = new ImplementationFactory();

//Interface and implementation for Shopping Cart service would be in MyApp.ApplicationLayer.dll
    IShoppingCartService service = factory.GetImplementationFactory<IShoppingCartService>();

    //This is the UI layer,
    //Calling into Application Layer
    //to get the shopping cart for a user.

    //Interface for IShoppingCart would be in MyApp.ApplicationLayer.dll
    //and implementation for IShoppingCart would be in MyApp.Model.
    IShoppingCart shoppingCart = service.GetShoppingCartByUserName(userName);

    //Show shopping cart information.
    //For example, items bought, price, taxes..etc
    ...

    //Pressed Purchase button, so even for when
    //button is pressed.
    //Uses Ioc to get the service from the factory again.
    IImplementationFactory factory = new ImplementationFactory();
    IShoppingCartService service = factory.GetImplementationFactory<IShoppingCartService>();
    service.Purchase(shoppingCart);


    //**********************Application Layer**********************
    public class ShoppingCartService : IShoppingCartService
    {
       public IShoppingCart GetShoppingCartByUserName(string userName)
       {
           //Uses Ioc to get the service from the factory.
           //This factory would be in the MyApp.Infrastructure.dll
           IImplementationFactory factory = new ImplementationFactory();

           //Interface for repository would be in MyApp.Infrastructure.dll
           //but implementation would by in MyApp.Model.dll
           IShoppingCartRepository repository = factory.GetImplementationFactory<IShoppingCartRepository>();

           IShoppingCart shoppingCart = repository.GetShoppingCartByUserName(username);
           //Do shopping cart logic like calculating taxes and stuff
           //I would put these in services but not sure?
           ...

           return shoppingCart;
       }

       public void Purchase(IShoppingCart shoppingCart)
       {
            //Do Purchase logic and calling out to repository
            ...
       }
    }
Run Code Online (Sandbox Code Playgroud)

我似乎把大部分业务规则都放在服务而不是模型中,我不确定这是否正确?另外,我不完全确定我的铺设是否正确?我在正确的地方有合适的部件吗?我的模型也应该离开我的域模型吗?一般来说,我是按照DDD这样做的吗?

谢谢!

Szy*_*ega 2

当服务(应用程序层)仅公开域对象上的命令而不是对象本身时,DDD 效果最佳。

在您的示例中,我将创建一个像Purchase(strng ProductName, int amount)这样的服务方法,该方法在内部将从存储库获取ShoppingCart(不必在此处使用接口)从存储库获取产品(按其名称)并调用购物车。添加产品(产品,数量)

不是,添加商品到购物车、计算总金额、总重量等业务逻辑都封装在模式中。