Apworks Framework Combat (V): Easymemo domain model design

Source: Internet
Author: User

In the previous session, we have created a new aggregate root object account and have been able to begin designing the domain model. In this talk, we will focus on the analysis and design of the Easymemo domain model and introduce the features of the Visual Studio Ultimate (Ultimate Edition) version, described in Visual Studio 2013 Ultimate How to use architectural modeling tools for domain model design and automate the generation of code that supports Apworks frameworks.

Defining context

Since the functionality required for Easymemo is very simple, it is easy to peel out two defining contexts from the domain concept: User account contexts and user note contexts. The former mainly describes the domain models associated with user accounts, such as accounts, roles, permissions, authorizations, etc., while the latter focuses more on the parts related to the notes. In practice, since Easymemo's business is very simple, it is not necessary to have a strict contextual distinction, which introduces defining context differentiation, primarily for the purpose of laying the groundwork for the introduction of visual Studio Ultimate Architecture tools later.

User account domain Model

The user account domain model contains concepts and objects related to the account and its permissions, as well as the relationships and behaviors between them. In Easymemo, we will take role-based access control (roles Based access Control,rbac), which is also a more mainstream way to authenticate and authorize permissions. Of course, it has some drawbacks, such as the Resource Access authorization (Privilege) type is not easy to expand, in the general application system, more inclined to the access authorization type fixed. For example, an app provides only a limited number of authorization types such as "System Management", "Account Add", "account management" and so on, and these types of authorization cannot be changed dynamically throughout the process. To add a new authorization type, you need to change the source code and regenerate it, or slightly better, modify the fixed value in the database. Anyway, RBAC is good or bad, here is not much tangled, in this case, we still try to implement a simple RBAC model it.

The most significant domain object in the user account domain model is the account, in which we have added this object to the Easymemo.domain class library. Since it is role-based access control, "role" is also a domain object, or, more precisely, an aggregate root. The question now is, what is the relationship between account aggregation and role aggregation?

It depends on how we understand the problem, if the "account" must be dependent on the "role" (i.e. there is no account that does not belong to any role), then the "role" aggregation can refer to "account" aggregation, because the latter's life cycle is shorter than the former (if the role aggregation life cycle ends, Then the account does not have the meaning of existence, conversely, the existence of "role" need to rely on "account"? Apparently not in most cases. Perhaps the relationship between the two is more reasonable, but there is a need to introduce another domain concept that describes the "subordinate" relationship between "account" and "role". This association can be either an entity or a value object whose life cycle depends on the aggregation in which it resides. The user account domain model designed in this article will not introduce such an associated entity, which is also to make things easier. In any case, this logical affiliation between "account" and "role" should be easy to understand.

The next question is how to set permissions for the role. Basically, this type of requirement can be summed up by "some sort of role has some control over a certain type of authorization." So how do you understand this sentence?

    1. A role: The concept of "role" in the domain model
    2. Some type of authorization: Represents a taxonomy for resource operations in the domain model. For example, "System Management", "Account Add", "Publish article" and so on, all belong to one kind of authorization type. In principle, authorization types can be combined, such as visitors with "system administration" authorization, and of course access to resources that are tagged by the "account Add" authorization
    3. Some control ability: that is, the role of the authorization type permissions, the simplest is "allow" and "deny" two

By concatenating the above understanding, we can get a description like "(System administrator) has access to (allowed) for the resource labeled (published article)." Then, if an account has a system administrator, it can publish articles. This part of the concept can be expressed simply by using the following model:

In the following sections, we will use the visual Studio Ultimate (flagship) modeling feature to refine this model.

User Note Domain model

User Note Domain model is very simple, we intend to include only a "sticky note" object, it defines the basic properties of the note and some simple business logic, here is not much to explain, in the follow-up practice slowly introduced it.

Domain model design and automated code generation using Visual Studio Ultimate (flagship edition)

First of all, this section needs to rely on Visual Studio Ultimate (flagship edition), and other versions of Visual Studio 2013 will not be able to complete what is demonstrated in this section. If you are using a VS2013 that is not the ultimate version, you can skip the reading of this section and, of course, read on to learn how the architecture tools provided by the flagship version are used.

Domain-specific languages are now a trend, and they are a common language for communication that is supported by domain-driven design. Model design with tools and automated generation of code not only makes it easier to design discussions with your team in a graphical manner, but also speeds up development and greatly reduces the chance of error. Next, let's take a look at how to implement the domain model design and automated code generation in Visual Studio Ultimate (The Ultimate Edition), combined with Apworks's modeling plug-ins.

Prerequisite

To rehearse what is discussed in this section, before you begin, you need to verify that your system meets the following prerequisites:

    1. Install Visual Studio Ultimate (flagship edition)
    2. Install the Apworks modeling plugin: Please click here to download the Apworks modeling plugin. In the unpacked compressed package, there are two versions of VS2013 and VS2015, it is strongly recommended to use VS2013, because there are some flaws in the VS2015 Automation code generation, the code can not be generated properly at present. This is a problem with visual Studio 2015 and is currently pending
    3. Download Apworks custom type generate template T4 file and save on local standby: Please "click here" to download

The Apworks modeling plug-in contains extensions to the Visual Studio Architecture Tools UML Language, with two additional stereotype, aggregate root and entity respectively. When automating code generation, the T4 engine decides to produce different code structures based on the different stereotype annotations.

Create a new modeling project

Right-click on the Easymemo solution and choose Add, New Project menu. In the Add New Project dialog box that pops up, select Modeling Project, named Easymemo.design, and then click OK:

Environment settings and configuration

Open UML Model Explorer, right-click on the Easymemo.design model and select Properties:

In the Properties tool window, expand the Common node, open the drop-down list on the Profiles project, and Tick both C # profile and Apworks Entity profile:

On the main menu of the visual Studio IDE, click the new Diagram menu, architecture, and the Add New Diagram dialog box opens.

In the Add New Diagram dialog box, select UML Class diagram, enter Model.classdiagram in the Name field, select Easymemo.design in the "Add to Modeling Project" column, and then click the "OK" button:

At this point, Visual Studio automatically opens the design interface of the model class diagram for the user to design the class diagram. Until then, we still need to configure the environment so that we can generate the code correctly in the next steps. First, open the directory where the Easymemo.design project is located in the file system, and then extract the downloaded Apworks custom type generation T4 template to the Templates subdirectory:

For future ease of operation, it is highly recommended that you display all files on the Easymemo.design project in Solution Explorer and include this Templates folder "in the project":

On the main menu of the visual Studio IDE, click the "Architecture" –> "Configure Default code Generation Settings" menu to open the Text Template bindings dialog box:

In the Text Template bindings dialog box, set the settings for Classtemplate, Enumtemplate, Interfacetemplate, and Structtemplate, in turn:

    1. Template file Path (*.T4) Select the appropriate file in the Templates folder under the Easymemo.design file system directory, such as CLASSTEMPLATE.T4, ENUMTEMPLATE.T4, Interfacetemplate.t4 and Structtemplate.t4
    2. We intend to generate C # code under the Model folder of the Easymemo.domain project, so enter "Model" directly in the target file directory and set the EasyMemo.Domain.csproj project in the project path

Click the OK button to close the dialog box.

Start using

OK, now we can start using the Class Diagram designer to design our domain model. Open the Toolbox, select the Class tool, add a class on the class Diagram designer, and rename to Aggregateroot:

Click the class, and on the Properties page, in the Stereotypes drop-down list, tick the "C # class" and "aggregate root" two Stereotypes:

Continue to expand the C # class node under the Stereotypes node, set the Is Partial property to True, and in the inheritance group, set the is Abstract property to true:

Right-click on the Aggregateroot class, choose the Add Property menu item, add an attribute named ID, type GUID, and add a name called isdeleted in the same way, type nullable<bool> of the feature. After adding these two features, the Aggregateroot class is as follows:

Now, let's try code generation. Right-click in a blank area of the Class Diagram designer and select Generate code:

After completing the "Code Generation" Progress window, you will find that under our Easymemo.domain project, there is a more model directory under which the AggregateRoot.cs file is generated:

Double-click to open this file, you can see the generated source code as follows:

The following points can be followed:

    1. The Aggregateroot class is abstract because before we set the "is abstract" property to True
    2. The Aggregateroot class is a partial class because we previously set the is partial property to True in the C # class stereotype
    3. The Aggregateroot class implements the Apworks.iaggregateroot interface because we have applied "aggregate root" to it stereotype
    4. All attributes (attributes) are virtual implementations (virtual), because the "is Leaf" property value for each attribute is false by default: This can be helpful in the next step of using the Entity Framework's lazy load feature. Of course, it is said that the entity Framework 7 has canceled the lazy load function

You may find that this class does not contain namespaces? Yes, to set the namespace of the generated code, you also need to make some changes to the class diagram:

    1. Open the Class Diagram designer, and in the Toolbox, locate the Package tool and add a package to the class Diagram designer
    2. Select Package 1, in the "Stereotypes" drop-down list of "properties", tick "C # namespace"


    3. Set the "Name" property of package 1 packages to EasyMemo.Domain.Model
    4. To drag the Aggregateroot class into the EasyMemo.Domain.Model package, our class diagram looks like this:

OK, generate the code again, and you can see that the generated code already contains the namespace definition:

Support for defining the context

The architecture modeling system for Visual Studio Ultimate is model-based, which means that even if you add more than one class diagram to your modeling project, these class diagrams are a common model. For example, we can create a new class diagram called accounts in the Easymemo.design project, represent the user account domain model, and then design the accounts class in this class diagram in a similar way:

Since the account class itself should inherit from the Aggregateroot class, we can find the definition of the Aggregateroot class directly from the UML Model Explorer and then add it to the Accounts Class Designer in a drag-and-drop manner.

From the UML toolbox, select the Inheritance tool, and then drag the mouse from the account class to the Aggregateroot class to indicate that the former inherits from the latter. After setting up this kind of relationship, our class diagram is as follows:

OK, generate the code again, you can see that the new Account.cs file content is as follows:

Complete our domain model

At this point, we have been able to use architecture and modeling tools in visual Studio Ultimate to graphically design the domain model, as well as the generation of automated code. Now, let's complete the Easymemo domain model together.

Anemia model???

Yes, the domain model that is designed through the class Diagram designer does not contain any method definitions. In fact, there is no way to write the source code of the methods in the class diagram. Remember when we set "is Partial" to true on the stereotype "C # class"? This allows us to add our own business logic to existing types without changing the autogenerated code: just use the features of the C # middle category!

Summarize

In this paper, we first briefly introduce the definition context and domain model of easymemo, and detail the design of the domain model using the Architecture modeling tools and Apworks Modeling extensions in Visual Studio Ultimate (Ultimate Edition), and implement the Code automation generation. Next I will focus on warehousing implementation based on the Entity Framework.

Source code Download

Please "Click here" to download the source code for Easymemo as of this article. If your visual Studio 2013 is not the ultimate version, you can also open the EasyMemo.sln solution normally, but you cannot open the Easymemo.design project. But that doesn't inconvenience you using the entire solution, you just have to remove the Easymemo.design project from the solution.

Apworks Framework Combat (V): Easymemo domain model design

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.