Design of data layer of CRUD full-stack programming architecture Codefirst
All this time, when we write applications, we start by creating databases.
Finally, after the ORM supports Codefirst, we can model it first.
The model to create the database, and based on Codefirst can be implemented conveniently
Implementation of database migration. There are several techniques for using Codefirst,
Taking EntityFramework as an example, the following improvements have been made in conjunction with my design
1. Identification of the model
Create a base class named entity that has only one ID field of type long.
All models that need to be mapped to a database inherit from entity,
+ View Code
2. Mapping of Models
Choose Fluntapi as the configuration (you can keep the model class neat and independent of the specific ORM)
Create a new configuration base class that inherits from Entitytypeconfiguration, and adds a generic constraint,
The table name is configured in the constructor (consistent with the class name), and the ID is used as the primary key, and is set to be generated by the program.
If it is a general single table, with the characteristics of the System.ComponentModel.DataAnnotations
You can complete the database field length and so on limit
+ View Code 3. Model generation
Overrides the Onmodelcreating method of DbContext, which reflects all types of assemblies that need to be mapped,
Then, find the corresponding configuration, and if not, construct the configuration base class to complete the model creation.
The EF defaults to creating or validating the model at the first visit,
The configuration of the model is cached in global static data. If the corresponding model class has
+ View Code
Repository
There are a lot of articles about repository that are not repeated here.
I use the interface programming here, all using constructors to inject.
I need to inject a default repository implementation for each type of curdservice.
+ View Code
Some other technologies
Unitofwork (unit of work)
There are a lot of articles on the internet, simply put, put several database operations together as a transaction commit
EF uses the Dbcontext.savechanges () method equivalent to the Unitofwork.commit () method, thanks to the EF design
This part of the design is mainly for reference Nlayerapp.
If there is no EF, I would recommend that you make a delegate for each SQL command that has an additional type of redaction, and then commit when left.
Use transaction submission. The code is as follows
+ View Code
Specification (Statute)
There are also a lot of articles on the Internet, I just use him as a query entity.
If you use the traditional ADO method, direct the expression to repository.
will cause parsing expressions to be particularly complex, and with specification, the equivalent of a query
The where part of the statement is taken over by it, so that decoupling and repository and specific ORM dependencies
This part of the design mainly for reference Nlayerapp
Multi-sorting
In pagination we often encounter multiple queries, the core is the construction of expressions, equivalent to the construction
The part of an order in an SQL statement, and it also supports multi-sorting in memory
This part of the design is mainly used for reference to the multi-sort design of Apworks
+ View Code
Ps:
More fragmented, if there is any problem can be in the following message to me.
Where the model creation code has a _context. This belongs to the next series of content.
It is mainly used for modular business vertical sub-Library.
The focus is to look at the design ideas, the code is just to give a demo, the general copy is compiled.
The end of the article series will emit a complete design code and a simple example.
Category: Crud full-stack programming architecture
Data-tier full-stack programming architecture