Domain-driven design case: Tiny Library: Application Service Layer

Source: Internet
Author: User

Tiny library uses the application service layer to provide services to the user interface layer. The specific implementation is Microsoft WCF services. In the tiny library solution, the tinylibrary. Services project provides the WCF Service for the entire system. According to the traditional hierarchical method of application systems, the tinylibrary. Services project is located on the domain model layer and under the user interface layer. It is the interaction interface between the UI and domain. Tinylibrary. in the implementation of services, the content related to DDD is mainly the data transmission object (DTO). As for how to compile and implement the WCF Service, that is. NET technology issues, this article will not do too much discussion.

 

Data transmission object (DTO)

In tinylibrary. Services, there is a special type of object, which is called a data transmission object. According to Fowler's description in poeaa, DTO is used for data exchange between processes. Because DTO is an object, it can contain a lot of data information. Therefore, dto can effectively reduce the number of data exchanges between processes, thus improving network transmission efficiency to a certain extent.

Because DTO needs to span process boundaries (in our case, we need to span network boundaries), DTO can be serialized/deserialized, it cannot contain context-related information (for example, Windows handle ). Because of this, the Data Types in DTO are very simple, they can be the original data type (primitive data types) or other DTO.

Some friends still have some difficulties in understanding DTO when reading Fowler's poeaa. Here I will write down my understanding for your reference.

    1. Domain Model objects are not responsible for any data transmission function, because if you use domain model objects for data transmission, they must be at another level (or in another process) when a copy of a domain model object is generated, a data contract may be generated between the server and the client, which violates the principle of high cohesion and low coupling between layers.
    2. DTO is simple, original, and context-independent during running. This is to meet the serialization/deserialization needs. Therefore, the data contained by DTO is not the original data type, or other DTO
    3. The requirement of the client determines the design of DTO. Therefore, DTO and domain model objects do not have a one-to-one relationship. On the contrary, DTO exist to meet client requirements.

In the case of tiny library, I used the data contracts of WCF as the DTO Implementation standard, because it not only can design a reasonable DTO structure based on client requirements, in addition, you can use datacontractserializer of WCF to implement DTO serialization/deserialization. In addition, WCF provides technical support and powerful support for communication between servers and clients.

Open the tinylibrary. Services project. We can see a generic interface of idataobject, which is defined as follows:

Hide row number CopyCode ? Idataobject Definition
    1.   Public interface   idataobject  
           
            where  tentity: 
            ientity  
          
    2. {
    3.   void  fromentity (tentity entity ); 
    4.  tentity toentity (); 
    5. }
    6.  

We can require all DTO to implement this interface so that it can convert an object to DTO or convert a DTO an object. In the case of tiny library, I didn't force all DTO to implement this interface (that is, tiny library itself does not require DTO to implement this interface). I introduced this interface for the purpose, I just want to explain that we can do this: design a reasonable framework for DTO in our system so that DTO can obtain more powerful functions. Another purpose of this interface is to facilitate programming: to implement a DTO based on an object, you only need to implement the idataobject interface. Visual Studio will automatically generate a method stubs ), you do not need to write code manually.

Please note tinylibrary. services. dataobjects. registrationdata is very different from bookdata and readerdata. It is not a reflection of registration ing entities. In other words, it contains all State attributes, it does not exactly correspond to the attributes contained in the registration entity. For example, the DTO registrationdata contains the information of the title (booktitle) and the ISBN (bookisbn), which are all from the associated entity of Registry: Book. Registrationdata is designed to cater to the user interface, because on the UI, We need to list his/her borrowing information for a reader, and registrationdata contains all the required information.

 

Application Layer responsibilities

Series of "Entity Framework-driven design practices"ArticleAccording to DDD, application systems are divided into four layers: presentation layer, application layer, domain layer, and infrastructure layer. In the case of tiny library, tinylibrary. services acts as the role of the application layer. It is not responsible for processing any business logic, but provides an appropriate runtime environment for the correct execution of business logic at a higher level, it also plays a role in task coordination (for example, service calling of transaction processing and infrastructure layer ).

Hide row number Copy code ? Implementation of the "return to book" Operation in the WCF Service
  1. Public voidReturn (StringReaderusername,GuidBookid)
  2. {
  3. Try
  4. {
  5. Using(IrepositorytransactioncontextCTX =Objectcontainer
  6. . Instance
  7. . Getservice <Irepositorytransactioncontext> ())
  8. {
  9. Irepository<Book> Bookrepository = CTX. getrepository <Book> ();
  10. Irepository<Reader> Readerrepository = CTX. getrepository <Reader> ();
  11. ReaderReader = readerrepository. Find (Specification<Reader>. Eval (r => r. username. Equals (readerusername )));
  12. BookBook = bookrepository. getbykey (bookid );
  13. Reader. Return (book );
  14. CTX. Commit ();
  15. }
  16. }
  17. Catch
  18. {
  19. Throw;
  20. }
  21. }

The code above shows the specific implementation of the "return to book" operation. We can see that the WCF services at the application layer only coordinates warehouse operations and transaction processing, and the business logic is implemented by the reader. Return method:

Hide row number Copy code ? Return Method of tinylibrary. domain. Reader
  1. Public voidReturn (BookBook)
  2. {
  3. If(! Book. Lent)
  4. Throw newInvalidoperationexception("The book has not been lent .");
  5. VaRQ =FromRIn this. Registrations
  6. WhereR. Book. Id. Equals (book. ID )&&
  7. R. registrationstatus =Registrationstatus. Normal
  8. SelectR;
  9. If(Q. Count ()> 0)
  10. {
  11. VaRReg = Q. First ();
  12. If(Reg. Expired)
  13. {
  14. // Todo: reader shocould pay for the expiration.
  15. }
  16. Reg. returndate =Datetime. Now;
  17. Reg. registrationstatus =Registrationstatus. Returned;
  18. Book. Lent =False;
  19. }
  20. Else
  21. Throw newInvalidoperationexception(String. Format ("Reader {0} didn't borrow this book .",
  22. This. Name ));
  23. }

 

Configuration File

Tinylibrary. Services is the service provider of the entire case. Therefore, the configuration and initialization of the entire system server should be executed when tinylibrary. Services is started. Therefore, the server-based system configuration should be written in the app. config of the tinylibrary. Services Project. These include: apworks configuration, Unity (or castle Windsor) configuration, WCF services configuration, and database connection string settings used by Entity Framework.

From the perspective of practice, in the application system based on the cqrs architecture model, the initialization and configuration logic of each component should be located in the global of WCF services. in the asax file, so that all components can be successfully initialized when the WCF Service Application is started. I will further describe this in future cqrs cases.

 

So far, the tiny library's server section has been basically introduced. Let's review the content, including Domain Modeling, warehousing implementation, and application service layer. Next, we will briefly introduce the design and development of the tiny library web interface. Since the focus of this series of articles is not to discuss the specific implementation of a technology, it will not involve too much details about ASP. net mvc in the next lecture.

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.