Topic: applying domain-driven development)
Domain Model is the "heart" of the MVC program. Everything else, including controllers and views, is only a way to interact with the domain model. asp. net MVC does not limit the use of domain model technology, we can freely choose. net Framework interaction technology, and there are many such choices. In addition, ASP. net mvc provides us with the basic architecture and conventions to help the classes in the domain model communicate with controllers and views, as well as the relationship with MVC itself.
They have three key functions:
A. Model binding: it is a basic convention that automatically uses data submitted by HTML forms to organize domain objects.
B. model metadata: Describes the meaning of model classes for. net. For example, it is to give a person an easy-to-understand expression of the attribute or some prompts when the attribute is displayed. Asp.net MVC can automatically identify and reasonably present model classes to views.
C. validation: rules that can be executed when the model is bound and can be applied as element data.
If you have the same understanding of model binding, you do not have to worry about it. Do not give up learning MVC, this is because the subsequent chapters of the book will be explained in detail. Now let's put the implementation of ASP. net mvc aside and simply think about Domain Modelling. Next we will use. NET and SQL Server to create a simple domain model for the bidding program.
1. Create a simple domain model
The following is a class diagram of the auction program to be created
The above model contains a set of members. Each member has a bids set, and each bid corresponds to an item. Each item can have multiple bids from different members.
The key to implementing our own domain model as an independent component is the language and terminology we choose. This is not our programming language, but a general language for Domain Modeling. This language is known to developers and professionals in the field. This is mainly for smooth communication between the two, which is crucial. When experts in the field do not understand some modeling concepts, we should reach a consensus on the terms used. This consensus is to create a common language and run through the modeling process in the entire field. This has many advantages.
1. FirstDevelopers tend to use programming languages, such as class names and databases. Business experts do not understand this, and they do not need to understand it. It is very dangerous for business experts to know some technical knowledge, because they will constantly filter their needs based on their understanding of the technology, this means that the demand will be changed frequently, so that developers do not know what the real requirements of business experts are. The method of creating a common language can help us avoid the need for over-generalization in an application. Programmers tend to create a practical business model rather than a specific business requirement.
2.The connection between a common language and a domain model should not be superficial, but should be suggested to the domain-driven design experts: any changes to the general language will lead to changes to the model. If we do not synchronize modeling with the business field, we need to build an intermediate language for ing from model to domain. In the long run, this approach will lead to a disaster. To this end, we will create a special human with two languages, and then they will start to filter the needs, which is based on their incomplete understanding of both languages, of course, such consequences can be imagined.
2. Aggregation and Simplification
The above figure provides a good starting point for modeling.
However, the model shown above does not provide any useful help for implementing the model using C # and SQL Server, and many problems may plague us:
1. If we load a member into the memory, should we load its bids and related items into the memory?
2. If we do this, do we need to load the other bids of this item into the memory, and load the members of these bids into the memory together?
3. Should I delete an object when we delete it? If so, what else?
4. If we choose to use document storage instead of relational databases for persistence, which object set should be presented to the same document?
We do not know how to answer all the above questions, and our domain model does not give us any answers.
The DDD method to answer these questions is to allocate domain objects to the group, which is called aggregates ).
The following shows how to aggregate the Domain Models in our bidding program:
An aggregated entity group combines several domain objects (together), and a root entity is used to identify the entire aggregation, it acts as a "boss" in verification and persistence operations. When data changes, I regard aggregation as a unit for overall handling. Therefore, we need to create an aggregation of meaningful relationships in the context of the domain model, create logical operations that are consistent with the business process. That is to say, we need to create aggregation through grouping objects, and these objects can be changed as a group.
An important DDD rule is that objects out of the scope of an aggregation instance can only be persistently referenced by reference to the root entity, rather than the objects referenced in the aggregation. This rule reinforces the concept of treating objects in aggregation as a unit. In this example, both members and items are the root of aggregation, while bids can only be accessed in the context of the item as the root entity for their aggregation. Bids can reference members (root entity), but Members cannot reference bids (not root entity)
One advantage of aggregation is that it simplifies the relationship between objects and Domain Models. This usually helps us to understand the nature of the domain that requires modeling. Essentially, the relationship between the domain model and the object of the aggregation constraint makes the relationship closer to the existing one in the real world. The following is expressed in C #, as shown below:
public class Member {
public string LoginName { get; set; } // The unique key
public int ReputationPoints { get; set; }
}
public class Item {
public int ItemID { get; private set; } // The unique key
public string Title { get; set; }
public string Description { get; set; }
public DateTime AuctionEndDate { get; set; }
public IList<Bid> Bids { get; set; }
}
public class Bid {
public Member Member { get; set; }
public DateTime DatePlaced { get; set; }
public decimal BidAmount { get; set; }
}
From the code above, we can easily capture the nature of the unidirectional relationship between bids and members. Of course, we can also establish some other constraints. For example, bids are immutable, which is also practical. Application aggregation can help us build more useful and accurate domain models, and help us use C # To implement them skillfully.
In general, aggregation adds structure and accuracy to a domain model. This also makes application verification easy (the root entity is responsible for verifying the state of all objects in the aggregation), which is obviously a persistent unit. Since the essence of aggregation is the atomic unit of the domain model, they can also be applied to the unit of Transaction Management and the unit of cascading deletion from the database.
On the other hand, aggregation is often artificially limited. The concept of aggregates can be naturally obtained from document-based databases, but it is not a concept of sqlserver itself, nor is it a concept in most ORM tools. In order to implement them well, our team needs scientific and effective communication.
3. Defining Repositories)
In some cases, we need to add persistence to the domain model, which is usually done through relational, object-type, or document-type databases. Persistence is not part of our domain model. It is an independent focus, which means we cannot mix persistent code with the code that defines the domain model. To solve this problem, we usually define a repository (repositories)
Respositories is based on the object rendering at the database (You may choose file storage, etc.) level. Domain Models indirectly store and query databases by calling the methods defined in repositories, so that our model can be implemented independently of persistence. This Convention defines a separate data model for each aggregate. In our bidding program, we can create two repositories for members and for items respectively. Note that we do not need to create a repository for bids, because bids will be part of the persistence of the items party ).
The two repositories are defined as follows:
public class MembersRepository {
public void AddMember(Member member) { ... }
public Member FetchByLoginName(string loginName) { ... }
public void SubmitChanges() { ...}
}
public class ItemsRepository {
public void AddItem(Item item) { ...}
public Item FetchByID(int itemID) { ... }
public IList<Item> ListItems(int pageSize,int pageIndex) { ...}
public void SubmitChanges() { ... }
}
Note: repositories only applies to loading and saving data. They do not contain any other logic.
Today's notes are made here. I just learned MVC and took notes to consolidate and deepen my understanding. Of course, I am very happy to be able to help beginners who are just like me a little bit. There will certainly be something wrong with my notes. Please help me with your guidance. Thank you!
Wish the passing friends a smooth job!
Good night!