. NET EF core domain Design Code transformation (previous)

Source: Internet
Author: User

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)

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.