Design problems discovered by unit tests

Source: Internet
Author: User

Problem Background: The company's internal business approval system is designed and developed using DDD. The architecture layer is probably the classic DDD architecture. The application layer uses automapper to map aggregation and DTO, and the general model of aggregation is as follows:

  

Problem locating: The problem occurs at the application layer. When the approval form aggregation is generated through ing, the lowest layer of the approval form aggregation (such as the business relationship details) is used for its upper-layer objects (such as medium business information) the unique identifier of the link is not consistent with the expectation. It is still named because of my paranoia. It will be a bit problematic.

        public AllographRequisitionData Save(AllographRequisitionData requisitionData)        {            DataObjectConvert convert = new DataObjectConvert();            AllographRequisition requisition = convert.DTOToEntity(requisitionData);            RequisitionService requisitionService = new RequisitionService();            requisitionService.Create(requisition);            return requisitionData;        }

The above code is the code that failed the unit test. The returned value of this method should be whether the execution was successful. However, to check the problem, the DTO has not been changed yet, the requisition aggregate instance requisition fails to be checked. The dtotoentity () method is applicable to automapper ing.

Cause: the internal relationship of aggregation is implemented by ID. Because the aggregation model is simple at first, the Assembly logic can be maintained internally by the aggregation, that is, the relationship between the internal entities of the aggregation. However, when the application layer maps the aggregate entity to the DTO, the IDs created by the aggregate itself are overwritten by the IDS carried by the DTO in the test case, which leads to the intrusion of the internal consistency of the aggregation by the application layer.

There are two solutions to the problem. This method is very simple. Most programmers may be unable to find out due to experience limitations or are reluctant to analyze the deeper causes of the problem, this method will be selected, as long as the ID is ignored during the ing, as long as you add a code to the ing, you can solve the problem, this method implicitly coupled the business and ing.

.ForMember(target => target.RequisitionContentID, opt => opt.Ignore())

Well, the above is just an introduction. What I want to say is that this problem should not exist. In the process of searching for this problem, I debugged two parts in the SAVE method unit test posted above: one is that when the aggregate root constructor generates the relationship between various objects within the aggregation, that is, the part that generates IDs and distributes them to the entity instances in the aggregation. The other part is the part that maps DTO to the aggregation.

However, it is obvious that I have encountered a problem with the function of maintaining internal relations in an aggregation, but I have to worry about another one that should have nothing to do with. This has already explained the problem; in addition, the ing method is only responsible for ing and should not maintain the aggregation logical relationship.

Obviously, the aggregation Service is responsible for assembling and aggregating and maintaining the relationship between various parts of the connection aggregation is not suitable here, so we have to maintain it by means of aggregation. There are two ways to create aggregation in DDD, and the other is the factory. In fact, the result mapped by DTO is only the data that should be held by aggregation, but it is not really a complete aggregation, so although the requisition instance has been generated, the aggregation is not completely formed. A factory generally has two locations: An aggregation root and a domain service. In this case, the factory can be called by the warehouse reconstruction process after the factory, and the factory is placed in the field service.

The return of the following code does not work at the moment.

        public bool Create<TRequisition, TRequisitionContent, TBusinessContent>(RequisitionBase requisition)            where TRequisition : RequisitionBase            where TRequisitionContent : RequisitionContentBase            where TBusinessContent : BusinessContentBase        {            bool isNotNull;            ValidateHelper.IsNotNull(requisition, out isNotNull);            RequisitionFactory build = new RequisitionFactory();            build.BuildRequisitionID(requisition);            RequistionRepository repository = new RequistionRepository();            repository.Add<TRequisition, TRequisitionContent, TBusinessContent>(requisition as TRequisition);            return true;        }

If so, you can avoid the above problems, although it is impossible to do all aspects of the design, up and down (do not know why I think of the day and night), from start to end all one-time considerations to improve, modifications, refactoring, and iterations will certainly exist, but I seldom write project code, I don't know how many design problems are difficult for programmers to pay attention to in the past, but there are major risks. Therefore, we must do a regression test on the design, if conditions are met, you can work with the programmer to develop the program. Then, you should carefully and carefully consider all the design parts as much as possible, and estimate the extension and possible use methods, perform some tests if necessary.

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.