Based on ASP. 2.0 WebAPI Background Framework (1)-Dependency injection three-layer frame construction

Source: Internet
Author: User

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.

    • Microsoft Dad Official Documentation: in ASP. NET Core Dependency Injection
    • Full understanding of ASP. NET Core Dependency Injection

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

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.