ASP.NET MVC 中 Autofac依賴注入DI 控制反轉IOC 了解一下


先簡單了解一這個幾個 名詞的意思。

控制反轉(IOC) 依賴注入(DI) 並不是某種技術。 而是一種思想。一種面向對象編程法則

 

什么是控制反轉(IOC)?  什么是依賴注入(DI)

可以點擊下面鏈接 理解的比較詳細 

https://blog.csdn.net/PacosonSWJTU/article/details/52786216

https://www.cnblogs.com/Mr-Rocker/p/7721824.html

 

控制反轉(IOC)

在之前傳統應用程序 我們都是在類內部主動實例化依賴對象,從而導致類與類之間高耦合,難於測試

可以看到下圖類與類之間 依賴關系很緊密

(圖是我盜的。感謝做這個圖的大佬)

 

IOC 就是一種容器  把創建和查找依賴對象的控制權交給了容器,由容器進行注入組合對象,所以對象與對象之間是 松散耦合,這樣也方便測試,利於功能復用,更重要的是使得程序的整個體系結構變得非常靈活。

IOC很好的體現了面向對象設計法則之一—— 好萊塢法則:“別找我們,我們找你”;即由IoC容器幫對象找相應的依賴對象並注入,而不是由對象主動去找。

 

依賴注入(DI) 

 依賴注入的目的並非為軟件系統帶來更多功能,而是為了提升組件重用的頻率,並為系統搭建一個靈活、可擴展的平台

依賴誰?   依賴於IOC容器

注入誰 ?   由IoC容器注入應用程序某個對象,應用程序依賴的對象

 被依賴者並不是有依賴者主動去初始化     而是有提供資源的外部創建者決定

 兩者關系也就是   被注入對象依賴IoC容器配置依賴對象

 

說到DI IOC 就有必要了解下 現在用的最多的AutoFac

 

首先在項目中引入 AutoFac    如果你是WebAPI   你可以輸入 AutoFac.WebApi2 

 

 

 

我定義了 兩個倉儲類   IRepositoryBase  和 RepositoryBase  主要用於操作數據庫的各種方法

  public interface IRepositoryBase<T> where T:class
    {

        IQueryable<T> FindAll();

        T FindSingle(Expression<Func<T, bool>> exp = null);

        T Find(int id);

        IQueryable<T> Find(Expression<Func<T, bool>> exp = null);

        /// <summary>
        /// Linq表達式樹查詢分頁
        /// </summary>
        /// <returns></returns>
        IQueryable<T> Find(int pageindex = 1, int pagesize = 10, Expression<Func<T, bool>> exp = null);

        /// <summary>
        /// 得到受影響條數
        /// </summary>
        /// <returns></returns>
        int GetCount(Expression<Func<T, bool>> exp = null);

        void Add(T entity);

        void AddBatch(T[] entitys);

        void AddBatch(List<T> entitys);

        /// <summary>
        /// 更新實體所有屬性
        /// </summary>
        /// <returns></returns>
        void Update(T entity);

        void Delete(T entity);


        /// <summary>
        /// 按指定的ID進行批量更新
        /// </summary>
        void Update(Expression<Func<T, object>> identityExp,T entity);

        T Update(T entity,int id);

        /// <summary>
        /// 批量刪除
        /// </summary>
        void Delete(Expression<Func<T, bool>> exp);

        void Save();

        int ExecuteSql(string sql);
    }

 

繼承接口並實現

public class RepositoryBase<T> : IRepositoryBase<T> where T:class
    {
        protected OpenSPDBContext openSPDBContext = new OpenSPDBContext();

         public void Add(T entity)
        {
            openSPDBContext.Set<T>().Add(entity);
            Save();
        }

        public void AddBatch(T[] entitys)
        {
            openSPDBContext.Set<T>().AddRange(entitys);
            Save();
        }

        public void Delete(Expression<Func<T, bool>> exp)
        {
           var entitys= openSPDBContext.Set<T>().Where(exp);
            openSPDBContext.Set<T>().RemoveRange(entitys);
        }

        public void Delete(T entity)
        {
            openSPDBContext.Set<T>().Remove(entity);
            Save();
        }

        public int ExecuteSql(string sql)
        {
            return openSPDBContext.Database.ExecuteSqlCommand(sql);
        }

        public IQueryable<T> Find(Expression<Func<T, bool>> exp = null)
        {
            return Filter(exp);
        }

        public T Find(int id)
        {
          return openSPDBContext.Set<T>().Find(id);
        }

        public IQueryable<T> Find(int pageindex, int pagesize, Expression<Func<T, bool>> exp = null)
        {

            return Filter(exp).Skip(pagesize * (pageindex - 1)).Take(pagesize);
        }


        public IQueryable<T> FindAll()
        {
            return openSPDBContext.Set<T>();
        }

        public T FindSingle(Expression<Func<T, bool>> exp = null)
        {
            return openSPDBContext.Set<T>().AsNoTracking().FirstOrDefault(exp);
        }

        public int GetCount(Expression<Func<T, bool>> exp = null)
        {
            return Filter(exp).Count();
        }

        public void Update(T entity)
        {
            openSPDBContext.Entry(entity).State = EntityState.Modified;
            Save();
        }

        /// <summary>
        /// 按指定id更新實體,會更新整個實體
        /// </summary>
        /// <param name="identityExp">The identity exp.</param>
        /// <param name="entity">The entity.</param>
        public void Update(Expression<Func<T, object>> identityExp, T entity)
        {
            openSPDBContext.Set<T>().AddOrUpdate(identityExp, entity);
        }

        public IQueryable<T> Filter(Expression<Func<T,bool>> exp)
        {
            var dbset = openSPDBContext.Set<T>().AsQueryable();
            if (exp != null)
            dbset = dbset.Where(exp);

            return dbset;

        }

        public void Save()
        {
            try
            {
                openSPDBContext.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                throw new Exception(e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage);
                throw;
            }
        }

        public void AddBatch(List<T> entitys)
        {
            openSPDBContext.Set<T>().AddRange(entitys);
        }

        public T Update(T entity, int id)
        {
            openSPDBContext.Entry(entity).State = EntityState.Modified;
            Save();
            return openSPDBContext.Set<T>().Find(id);
        }   
    }

 

然后 在全局類中注冊依賴配置

 

我新建了個邏輯處理層 Server  用來方便控制調用   這里我依賴了IRepositoryBase<T>

public class ShopingServer : IShopingServer
    {
        private IRepositoryBase<ShopingInfo> _shopingRepository;
        public ShopingServer(IRepositoryBase<ShopingInfo> shopingRepository)
        {
            _shopingRepository = shopingRepository;
        }

        public IQueryable<ShopingInfo> GetAll()
        {
           var ss = _shopingRepository.FindAll();
            return ss;
        }
    }

 

然后控制器  注入  (這里 我依賴了IRepositoryBase  IShopingServer)  IShopingServer 是我定義的服務類接口 這里我就不貼出來了

我們 分別調用一下 IRepositoryBase   IShopingServer  里面的一個方法 測試一下

 

可以看到 已經注入成功了    小菜也有夢想。 每天一點點 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM