Overview
This article describes how to build an ASP. 2.0 WebAPI Dependency Injection Three-tier architecture, why join dependencies, not to provide program performance, but to decouple between projects, to be more independent between projects.
Steps
1. Create a new solution and add an ASP. WEBAPI application
2. Add four. Net Core class Libraries: Entity, BLL, DAL, Common
3. Follow the solution layout below
4. Add a DAL layer file
- Under the DAL---Interfaces folder, add the interface IDalService.cs. Here the database context Dependency injection is prepared for the directory, which is added first.
Public InterfaceIdalservice<t>whereT:class,New() { /// <summary> ///New Entity/// </summary> /// <param name= "entity" >Entity</param> /// <returns></returns>T addentity (t entity); /// <summary> ///Get Collection/// </summary> /// <param name= "where" >LINQ Statements</param> /// <returns></returns>Iqueryable<t> getentities (Expression<func<t,BOOL>>where); /// <summary> ///Save Data/// </summary> /// <returns></returns> intSaveChanges (); }
Idalservice
- Under the Dal---Implements folder, add the base class DalService.cs.
Public classDalservice<t>: IDisposable, idalservice<t>whereT:class,New() { Private ReadOnlyMycontext _context; /// <summary> ///get the database context/// </summary> /// <param name= "context" >database context for Dependency injection</param> PublicDalservice (Mycontext context) {_context=context; } PublicT addentity (t entity) {_context. Set<T>(). ADD (entity); returnentity; } PublicIqueryable<t> getentities (Expression<func<t,BOOL>>where) { return_context. Set<t> (). Where (where); } Public intSaveChanges () {return_context. SaveChanges (); } Public voidDispose () {_context. Dispose (); } }
Dalservice
Note that this is just a simple three ways: Add, find, save the database, these three are enough to do a demo, if necessary, please add additional, such as delete, UPDATE, etc.
- Add a database context under Entity MyContext.cs
public class Mycontext:dbcontext { public mycontext (): base () {} publ IC mycontext (dbcontextoptions<mycontext> options): base (options) {} override protected void onconfiguring (Dbcontextoptionsbuilder optionsbuilder) {
base
Mycontext
5. Add a BLL layer file
- Under the BLL-Interfaces folder, add the interface IBllService.cs
Public Interface where class New () { bool issave); IQueryableboolwhere); int SaveChanges (); }
Ibllservice
- Under the BLL-Implements folder, add the base class BllService.cs
/// <summary> ///Data Logic layer: BLL/// </summary> /// <typeparam name= "T" ></typeparam> Public classBllservice<t>: ibllservice<t>whereT:class,New() { /// <summary> ///Database Services/// </summary> protectedIdalservice<t>_dal; PublicBllservice (idalservice<t>dal) {_dal=dal; } PublicT addentity (t entity,BOOLIssave) {Entity=_dal. Addentity (entity); if(issave) {if(SaveChanges () >0) return NULL; } returnentity; } PublicIqueryable<t> getentities (Expression<func<t,BOOL>>where) { return_dal. Getentities (where); } Public intSaveChanges () {return_dal. SaveChanges (); } }
Bllservice
6. Finally, in Startup.cs, add the dependency mappings.
Public void configureservices (iservicecollection services) { diregister (services); Services. Addmvc (); } // Configuring Dependency Injection mapping Relationships Public void Diregister (iservicecollection services) { services. AddTransient (typeoftypeof(dalservice<>)); }
Startup
After all the steps are complete, the solution directory is as follows
So far, the three-tier architecture has all been completed. In the next chapter, we will use the MySQL database, based on this three-tier framework, to develop dbfirst based on EF core.
Based on ASP. 2.0 WebAPI Background Framework (1)-Dependency injection three-layer frame construction