. Net ef core design code conversion (Part 1), efcore
I. Preface
The. net core 2.0 official version has been released for a few months. After research, I decided to transfer the project. If you are a newbie, you can read some official introductions first.
Transport: https://docs.microsoft.com/zh-cn/dotnet/core/
I encountered some pitfalls in the design model of the domain, so I would like to share some of my solutions with you.
OK. The general structure is as follows:
Compare the textbook architecture.
2. Domain Layer
Domain entity
Value Object
Protocol Interface
Work Unit Interface
Warehousing Interface
Aggregation and division, we first create a simple user entity
Iii. storage layer
Reference Microsoft. EntityFrameworkCore. Sqlite
Work Unit ISql Interface
Work Unit IQueryableUnitOfWork Interface
Next we will focus on the changes to the constructor:
OnModelCreating changes:
Previous settings
At present, ef core does not provide such convenient encapsulation. You need to implement it yourself.
Database Table Name Change:
public static void SetSimpleUnderscoreTableNameConvention(this ModelBuilder modelBuilder){ foreach (IMutableEntityType entity in modelBuilder.Model.GetEntityTypes()) { Regex underscoreRegex = new Regex(@"((?<=.)[A-Z][a-zA-Z]*)|((?<=[a-zA-Z])\d+)"); entity.Relational().TableName = underscoreRegex.Replace(entity.DisplayName(), @"$1$2").ToLower(); }}
Disable cascading deletion of all primary and foreign key relationships
public static void SetOneToManyCascadeDeleteConvention(this ModelBuilder modelBuilder){ foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys())) { relationship.DeleteBehavior = 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>{ public override void Map(EntityTypeBuilder<User> builder) { builder.HasKey(u => u.Id); }}
When used, it is added in this way, and there is no sense of violation.
The SQL query method has changed.
Pay attention to the type of constructor when implementing User table warehousing. This will be involved in. net core dependency injection.
Iv. Application Layer
The Application Layer does not change, so you can directly paste the code
V. distributed service layer
. Net core comes with a simple Ioc, so the previously used framework similar to Unity can be removed.
How does Configuration come from this?
. Net core is now recommended to use Json and other configuration files, which are very nice and powerful to use.
Then we need to ensure that the database does not exist when it is created correctly
UserController code:
Vi. Presentation Layer
A simple look at the WPF Interface
Background code: