基於 ASP.NET Core 2.0 WebAPI 後台架構搭建(1) - 依賴注入三層架構搭建

來源:互聯網
上載者:User

標籤:上下文   gif   微軟   資料庫   解決   mvc   open      SQ   

  概述  

  本文章描述如何搭建 ASP.NET Core 2.0 WebAPI 依賴注入三層架構,為什麼要加入依賴,並不是為了提供者效能,而是為了項目間解耦,項目之間能夠更加獨立。

  • 微軟爸爸官方說明文檔:在 ASP.NET Core 依賴注入
  • 全面理解 ASP.NET Core 依賴注入

 

  步驟

  1. 建立解決方案,添加一個ASP.NET Core WebApi應用

 

 

  2. 添加四個.Net Core類庫:Entity、BLL、DAL、Common

 

  3. 按照以下進行解決方案布局

 

  4. 添加DAL層檔案

  • 在 DAL -> Interfaces 檔案夾下,添加介面 IDalService.cs 。這裡的資料庫上下文依賴注入是為目錄作準備的,先添加上。
    public interface IDalService<T> where T : class, new()    {        /// <summary>        /// 新增實體        /// </summary>        /// <param name="entity">實體</param>        /// <returns></returns>        T AddEntity(T entity);        /// <summary>        /// 擷取集合        /// </summary>        /// <param name="where">linq語句</param>        /// <returns></returns>        IQueryable<T> GetEntities(Expression<Func<T, bool>> where);        /// <summary>        /// 儲存資料        /// </summary>        /// <returns></returns>        int SaveChanges();    }
IDalService

 

 

  •  在 Dal -> Implements 檔案夾下,添加基類 DalService.cs。
public class DalService<T> : IDisposable, IDalService<T> where T : class, new()    {        private readonly MyContext _context;        /// <summary>        /// 獲得資料庫上下文        /// </summary>        /// <param name="context">依賴注入的資料庫上下文</param>        public DalService(MyContext context)        {            _context = context;        }        public T AddEntity(T entity)        {            _context.Set<T>().Add(entity);            return entity;        }        public IQueryable<T> GetEntities(Expression<Func<T, bool>> where)        {            return _context.Set<T>().Where(where);        }        public int SaveChanges()        {            return _context.SaveChanges();        }        public void Dispose()        {            _context.Dispose();        }    }
DalService

 

 

   注意,這裡只是簡單的三個方法:添加、尋找、儲存資料庫,這三個已經足夠做示範,如有需要,請另外添加,如刪除、更新等。

  • 在 Entity 下添加資料庫上下文 MyContext.cs
    public class MyContext : DbContext    {        public MyContext() : base()        {        }        public MyContext(DbContextOptions<MyContext> options)                    : base(options)        {        }        override protected void OnConfiguring(DbContextOptionsBuilder optionsBuilder)        {            base.OnConfiguring(optionsBuilder);        }    }
MyContext

 

 

  5. 添加BLL層檔案

  • 在 BLL -> Interfaces 檔案夾下,添加介面 IBllService.cs
    public interface IBllService<T> where T : class, new()    {        T AddEntity(T entity, bool isSave);        IQueryable<T> GetEntities(Expression<Func<T, bool>> where);        int SaveChanges();    }
IBllService

 

  • 在 BLL -> Implements 檔案夾下,添加基類 BllService.cs

 

    /// <summary>    /// 資料邏輯層:BLL    /// </summary>    /// <typeparam name="T"></typeparam>    public class BllService<T> : IBllService<T> where T : class, new()    {        /// <summary>        /// 資料庫服務        /// </summary>        protected IDalService<T> _dal;        public BllService(IDalService<T> dal)        {            _dal = dal;        }        public T AddEntity(T entity, bool isSave)        {            entity = _dal.AddEntity(entity);            if (isSave)            {                if (SaveChanges() > 0)                    return null;            }            return entity;        }        public IQueryable<T> GetEntities(Expression<Func<T, bool>> where)        {            return _dal.GetEntities(where);        }        public int SaveChanges()        {            return _dal.SaveChanges();        }    }
BllService

 

 

  6. 最後,在 Startup.cs 中,添加依賴映射。

        public void ConfigureServices(IServiceCollection services)        {            DIRegister(services);            services.AddMvc();        }        // 配置依賴注入映射關係        public void DIRegister(IServiceCollection services)        {            services.AddTransient(typeof(IDalService<>), typeof(DalService<>));        }
Startup

 

   所有步驟完成後,解決方案目錄如下

  

 

  到此為止,三層架構已經全部完成,是不是覺得很簡單呢。下一章,我們將基於這個三層架構,使用MySQL資料庫,進行基於EF Core的DbFirst開發。

 

基於 ASP.NET Core 2.0 WebAPI 後台架構搭建(1) - 依賴注入三層架構搭建

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.