Having trouble putting real-world logic into the DDD domain layer

前端 未结 3 2012
耶瑟儿~
耶瑟儿~ 2020-12-13 05:01

Despite having studied Domain Driven Design for a long time now there are still some basics that I simply figure out.

It seems that every time I try to

相关标签:
3条回答
  • 2020-12-13 05:31

    Dimitry's answer points out some good things to look for. Often/easily you find yourself in your scenario, with a data shoveling from db up to GUI through different layers.

    I have been inspired by Jimmy Nilsson's simple advice "Value objects, Value objects and more Value objects". Often people tend to focus to much on Nouns and model them as entity. Naturally you often having trouble in finding DDD behavior. Verbs are easier to associate with behavior. A good thing is to make these Verbs appear in your domain as Value objects.

    Some guidance I use for my self when trying to develop the domain (must say that it takes time to construct a rich domain, often several refactoring iterations...) :

    • Minimize properties (get/set)
    • Use value objects as much as you can
    • Expose as little you can. Make you domain aggregates methods intuitive.

    Don't forget that your Domain can be rich by doing Validation. It's only your domain that knows how to conduct a purchase, and what's required.

    Your domain should also be responsible for validation when your entities make a transition from one state two another state (work flow validations).

    I'll give you some examples: Here is a article I wrote on my blog regarding your issue about anemic Domain http://magnusbackeus.wordpress.com/2011/05/31/preventing-anemic-domain-model-where-is-my-model-behaviour/

    I can also really recommend Jimmy Bogard's blog article about entity validations and using Validator pattern together with extension methods. It gives you the freedom to validate infrastructural things without making your domain dirty: http://lostechies.com/jimmybogard/2007/10/24/entity-validation-with-visitors-and-extension-methods/

    I use Udi's Domain Events with great success. You can also make them asynchronous if you believe your service can fail. You also wrap it in a transaction (using NServiceBus framework).

    In your first example (just brainstorming now to get our minds thinking more of value objects).

    1. Your MusicService.AddSubscriber(User newUser) application service get a call from a presenter/controller/WCF with a new User. The service already got IUserRepository and IMusicServiceRepository injected into ctor.
    2. The music service "Spotify" is loaded through IMusicServiceRepository
    3. entity musicService.SignUp(MusicServiceSubscriber newSubsriber) method takes a Value object MusicServiceSubscriber. This Value object must take User and other mandatory objects in ctor (value objects are immutable). Here you can also place logic/behavior like handle subscriptionId's etc.
    4. What SignUp method also does, it fires a Domain Event NewSubscriberAddedToMusicService. It get caught by EventHandler HandleNewSubscriberAddedToMusicServiceEvent which got IFileService and IEmailService injected into it's ctor. This handler's implementation is located in Application Service layer BUT the event is controlled by Domain and MusicService.SignUp. This means the Domain is in control. Eventhandler creates file and sends email.

    You can persist the user through eventhandler OR make the MusicService.AddSubscriber(...) method to this. Both will do this through IUserRepository but It's a matter of taste and perhaps how it will reflect the actual domain.

    Finally... I hope you grasp something of the above... anyhow. Most important is to start adding "Verbs" methods to entitys and making the collaborate. You can also have object in your domain that are not persisted, they are only there for mediate between several domain entities and can host algorithms etc.

    0 讨论(0)
  • 2020-12-13 05:34

    Big part of you requests are related to object oriented design and responsibility assignment, you can think of GRASP Patterns and This, you can benefit from object oriented design books, recommend the following

    Applying UML and Patterns

    0 讨论(0)
  • 2020-12-13 05:44

    A user signs up for a service. The user is persisted in the database, a file is generated and saved (needed for the user account), and a confirmation email is sent.

    You can apply Dependency Inversion Principle here. Define a domain interface like this:

    void ICanSendConfirmationEmail(EmailAddress address, ...)
    

    or

    void ICanNotifyUserOfSuccessfulRegistration(EmailAddress address, ...)
    

    Interface can be used by other domain classes. Implement this interface in infrastructure layer, using real SMTP classes. Inject this implementation on application startup. This way you stated business intent in domain code and your domain logic does not have direct reference to SMTP infrastructure. The key here is the name of the interface, it should be based on Ubiquitous Language.

    A song is purchased from a digital music store. The shopping cart is emptied. The purchase is persisted. The payment service is called. An email confirmation is sent. Ok, this might be related to the first example. The question here is, who is responsible for orchestrating this transaction?

    Use OOP best practices to assign responsibilities (GRASP and SOLID). Unit testing and refactoring will give you a design feedback. Orchestration itself can be part of thin Application Layer. From DDD Layered Architecture:

    Application Layer: Defines the jobs the software is supposed to do and directs the expressive domain objects to work out problems. The tasks this layer is responsible for are meaningful to the business or necessary for interaction with the application layers of other systems.

    This layer is kept thin. It does not contain business rules or knowledge, but only coordinates tasks and delegates work to collaborations of domain objects in the next layer down. It does not have state reflecting the business situation, but it can have state that reflects the progress of a task for the user or the program.

    0 讨论(0)
提交回复
热议问题