First, preface
The official version of. NET Core 2.0 has been released for months, after research, decided to transfer the project, the Novice can first read some official introduction
Portal: Https://docs.microsoft.com/zh-cn/dotnet/core/
Because in the domain design model encountered some pits, so we share some of their own solutions.
OK, the dry goods directly, the approximate structure is as follows:
Compare the textbook-style architecture.
Second, the field layer
Domain entity
Value Object
Protocol interface
Work Cell interface
Warehousing interface
Aggregation and division, we first set up a simple user entity
Third, storage layer
Reference Microsoft.EntityFrameworkCore.Sqlite
Working Unit ISQL interface
Working Unit Iqueryableunitofwork Interface
The next point is the change in the constructor:
Onmodelcreating Change:
Previous settings
Now EF Core does not provide such a convenient package, you need to implement
database table name change:
Public Static voidSetsimpleunderscoretablenameconvention ( ThisModelBuilder ModelBuilder) {foreach(Imutableentitytype entityinchmodelBuilder.Model.GetEntityTypes ()) {Regex Underscoreregex=NewRegex (@"((? <=.) [A-z] [a-za-z]*) | ((? <=[a-za-z]) \d+)"); Entity. Relational (). TableName= Underscoreregex.replace (entity. DisplayName (),@"$1$2"). ToLower (); }}
Turn off cascade deletions for all primary foreign key relationships
Public Static void Setonetomanycascadedeleteconvention ( this ModelBuilder ModelBuilder) { foreach ( var in ModelBuilder.Model.GetEntityTypes (). SelectMany (E = E.getforeignkeys ())) { = deletebehavior.restrict; }}
Fluent API Configuration
public abstract class entitytypeconfiguration<tentity> where TEntity: class { public abstract void Map (entitytypebuilder<tentity> builder);}
public static void addconfiguration<tentity> (this ModelBuilder ModelBuilder, entitytypeconfiguration<tentity> configuration) where TEntity: class {configuration. Map (modelbuilder.entity <tentity> ());}
Public class Userentitytypeconfiguration:entitytypeconfiguration<user>{ publicoverride void Map (entitytypebuilder<user> builder) { = = u.id);} }
The use of this time to add, no vainly disobey sense.
The way SQL queries change a bit
When user table warehousing is implemented, note the type of constructor, which is involved when. NET core dependency injection.
Iv. Application Layer
No change in application layer, direct code placement
V. Distributed Service Tiers
The. NET core comes with a simple IOC, so frameworks like unity that we used to get rid of
How did the configuration come about?
. NET core now recommends using a configuration file such as JSON, which is very cool and very powerful.
Then we need to make sure that the storage database does not exist when it is created correctly
Usercontroller Code:
Vi.: Presentation layer
WPF interface easy to get a bit
Background code:
. NET EF core domain Design Code transformation (previous)