Domain Driven Design Learning notes (one event bus)

Source: Internet
Author: User
Tags eventbus

    • What is a domain-driven design?

Eric Evans, a leading modeling expert in 2004, published his most influential book: Domain-driven design:tackling Complexity in the heart of software ( Domain-driven design: The core complexity of software, the concept of domain-driven design (DDD) is presented in the book.

Domain driven design is in fact an extension and extension of the Ooad, DDD is based on object-oriented analysis and design technology, the technical architecture of the hierarchical planning, at the same time, each class is divided into policy and type.

Domain model is the core of domain-driven. Using DDD's design philosophy, business logic is no longer concentrated on several large classes, but rather consists of a large number of relatively small domain objects (classes) that have their own state and behavior, each of which is a relatively complete independent body and is mapped to a business object in the real world. The domain model is made up of so many fine-grained classes. The domain-driven design ensures the maintainability, extensibility and reusability of the system, and has inherent advantages in dealing with complex business logic.

Excerpt from: http://kb.cnblogs.com/page/112298/

    • Types of domain models?
    1. Blood loss Model: The model contains only the definition and Getter/setter method of the data, and the business logic and application logic are placed in the service layer. This kind is called Pojo in Java and is called Poco in. Net.
    2. anemia Model: The anemia model contains some business logic, but does not include business logic that relies on persistence layers . This part of the business logic that relies on the persistence layer will be placed in the service layer. It can be seen that the domain objects in the anemia model are not dependent on the persistence layer.
    3. Congestion Model: The Congestion model contains all the business logic, including business logic that relies on the persistence layer . Therefore, the domain layer using the congestion model is dependent on the persistence layer, and the simple representation is the domain layer <-> persistence layer, the service layer, UI layer.
    4. blood-swelling model: The model of blood-swelling is to put other application logic (such as authorization, transaction, etc.) into the domain model that the business logic does not want to close. I feel the blood model is a different kind of blood loss model, because the service layer disappears, the domain layer dries the service layer, in the end, nothing has changed.
    • Domain Events and event bus

    • Explorer Object (event bus) Eventbus

The main responsibility is to manage all events of the domain model, through dictionary<type,list<object>> _diceventhandler=new Dictionary<type, List <object>> (), mainly including

    • Subscribe Subscription-related domain object events
    • Publish triggers all domain object event handles on the event bus
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacemyddd{/// <summary>    ///Event Bus/// </summary>     Public classEventbus {/// <summary>        ///Event Bus Object/// </summary>        Private StaticEventbus _eventbus=NULL; /// <summary>        ///domain model event handle dictionary, used to store a handle to the domain model/// </summary>        Private Staticdictionary<type,list<Object>> _diceventhandler=NewDictionary<type, list<Object>>(); /// <summary>        ///when attaching a domain model to handle a handle, lock/// </summary>        Private ReadOnly Object_syncobject =New Object(); /// <summary>        ///Single Event Bus/// </summary>         Public StaticEventbus Instance {Get            {                return_eventbus?? (_eventbus=NewEventbus ()); }        }        Private ReadOnlyfunc<Object,Object,BOOL> _eventhandlerequals = (O1, O2)        {            return true;        }; #regionSubscribe to Events Public voidSubscribe<tevent> (ieventhandler<tevent> EventHandler)wheretevent:ievent {//Sync Lock            Lock(_syncobject) {//get the type of domain model                varEventType =typeof(tevent); //If this realm type has been registered in the event bus                if(_diceventhandler.containskey (EventType)) {varHandlers =_diceventhandler[eventtype]; if(Handlers! =NULL) {handlers.                    ADD (EventHandler); }                    Else{handlers=Newlist<Object>{EventHandler}; }                }                Else{_diceventhandler.add (EventType,Newlist<Object>{eventHandler}); }            }        }        #endregion        #regionAnnouncing events Public voidPublish<tevent> (Tevent tevent, Action<tevent,BOOL, Exception> Callback)wheretevent:ievent {varEventType =typeof(tevent); if(_diceventhandler.containskey (EventType) && _diceventhandler[eventtype]! =NULL&&_diceventhandler[eventtype]. Count>0)            {                varHandlers =_diceventhandler[eventtype]; Try                {                    foreach(varHandlerinchhandlers) {                        varEventHandler = Handler asIeventhandler<tevent>;                        Eventhandler.handle (tevent); Callback (Tevent,true,NULL); }                }                Catch(Exception ex) {callback (tevent,false, ex); }            }            Else{callback (tevent,false,NULL); }        }        #endregion    }}

    • Domain Object

Domain objects are tagged by implementing the IEvent interface, which flags this domain object to subscribe to the event bus for domain object events related to itself.

User Registration Domain Object

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacemyddd{/// <summary>    ///Domain Model Objects/// </summary>     Public classusergenerator:ievent {/// <summary>        ///User ID/// </summary>         PublicGuid UserId {Get;Set; } }}
    • Domain Object Event handle

The domain object event handle is the notification that Eventbus triggers all event handles when the domain object changes

        public void publish<tevent> (Tevent tevent, action<tevent, BOOL, exception> callback)  where tevent : IEvent
User Registration event handle for sending email
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacemyddd{/// <summary>    ///     /// </summary>     Public classUseraddedeventhandlersendemail:ieventhandler<usergenerator>    {         Public voidHandle (Usergenerator tevent) {Console.WriteLine (string. Format ("the message for {0} has been sent", Tevent.userid)); }    }}



Example: When the user registration is completed, notify Eventbus, by Eventbus trigger the corresponding action, send email or other actions.
Such benefits user registration is the main business, others belong to the expansion, increase or decrease demand, only need to subscribe to Eventbus or cancel the event.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacemyddd{classProgram {Static voidMain (string[] args) {EventBus.Instance.Subscribe (NewUseraddedeventhandlersendemail ()); varUsergenerator =NewUsergenerator{userid =guid.newguid ()}; Console.WriteLine ("{0} registered successfully", Usergenerator.userid);            EventBus.Instance.Publish (Usergenerator, CallBack);        Console.readkey (); }         Public Static voidCallBack (Usergenerator Usergenerator,BOOLresult, Exception ex) {        }    }}

Domain Driven Design Learning notes (one event bus)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.