欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.Net Core基于EF6、Unitwork、Autofac實現(xiàn)Repository模式

 更新時間:2022年02月16日 11:42:04   作者:Run2948  
這篇文章介紹了ASP.Net Core基于EF6、Unitwork、Autofac實現(xiàn)Repository模式的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、實現(xiàn)的思路和結構圖

  • Repository的共同性

有一些公共的方法(增刪改查), 這些方法無關于Repository操作的是哪個實體類,可以把這些方法定義成接口IRepository,然后有個基類BaseRepository實現(xiàn)該接口的方法。常見的方法,比如Find, Filter, Delete, Create等

  • Repository的差異性

每個Repository類又會有一些差異性,應當允許它們能夠繼承BaseRepository之外,還能夠再擴展自己的一些方法。所以每個類都可以再定義一個自己特有的接口,定義一些屬于自己Repository的方法。

  • Repository的協(xié)同性

不同的Repository可能需要協(xié)同,Repository對數(shù)據(jù)的修改,需要在統(tǒng)一的保存.
最終實現(xiàn)的類結構圖如下:

類結構圖

二、Repository設計具體的實現(xiàn)代碼

IRepository接口定義了Repository共有的方法, BaseRepository實現(xiàn)了這些接口的方法。其它的Repository類再集成BaseRepository方法,就天然的得到了對數(shù)據(jù)操作的基本方法。

  • IRepository代碼
    public interface IRepository<TEntity> where TEntity : class
    {
        /// <summary>
        /// Gets all objects from database
        /// </summary>
        /// <returns></returns>
        IQueryable<TEntity> All();

        /// <summary>
        /// Gets objects from database by filter.
        /// </summary>
        /// <param name="predicate">Specified a filter</param>
        /// <returns></returns>
        IQueryable<TEntity> Filter(Expression<Func<TEntity, bool>> predicate);

        /// <summary>
        /// Gets objects from database with filtering and paging.
        /// </summary>
        /// <param name="filter">Specified a filter</param>
        /// <param name="total">Returns the total records count of the filter.</param>
        /// <param name="index">Specified the page index.</param>
        /// <param name="size">Specified the page size</param>
        /// <returns></returns>
        IQueryable<TEntity> Filter(Expression<Func<TEntity, bool>> filter, out int total, int index = 0, int size = 50);

        /// <summary>
        /// Gets the object(s) is exists in database by specified filter.
        /// </summary>
        /// <param name="predicate">Specified the filter expression</param>
        /// <returns></returns>
        bool Contains(Expression<Func<TEntity, bool>> predicate);

        /// <summary>
        /// Find object by keys.
        /// </summary>
        /// <param name="keys">Specified the search keys.</param>
        /// <returns></returns>
        TEntity Find(params object[] keys);

        /// <summary>
        /// Find object by specified expression.
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        TEntity Find(Expression<Func<TEntity, bool>> predicate);

        /// <summary>
        /// Create a new object to database.
        /// </summary>
        /// <param name="t">Specified a new object to create.</param>
        /// <returns></returns>
        void Create(TEntity t);

        /// <summary>
        /// Delete the object from database.
        /// </summary>
        /// <param name="t">Specified a existing object to delete.</param>
        void Delete(TEntity t);

        /// <summary>
        /// Delete objects from database by specified filter expression.
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        int Delete(Expression<Func<TEntity, bool>> predicate);

        /// <summary>
        /// Update object changes and save to database.
        /// </summary>
        /// <param name="t">Specified the object to save.</param>
        /// <returns></returns>
        void Update(TEntity t);

        /// <summary>
        /// Select Single Item by specified expression.
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        TEntity FirstOrDefault(Expression<Func<TEntity, bool>> expression);
    }
  • BaseRepository代碼
    public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class
    {
        protected readonly DbContext Context;

        public BaseRepository(DbContext context)
        {
            Context = context;
        }

        /// <summary>
        /// Gets all objects from database
        /// </summary>
        /// <returns></returns>
        public IQueryable<TEntity> All()
        {
            return Context.Set<TEntity>().AsQueryable();
        }

        /// <summary>
        /// Gets objects from database by filter.
        /// </summary>
        /// <param name="predicate">Specified a filter</param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> Filter(Expression<Func<TEntity, bool>> predicate)
        {
            return Context.Set<TEntity>().Where<TEntity>(predicate).AsQueryable<TEntity>();
        }

        /// <summary>
        /// Gets objects from database with filtering and paging.
        /// </summary>
        /// <param name="filter">Specified a filter</param>
        /// <param name="total">Returns the total records count of the filter.</param>
        /// <param name="index">Specified the page index.</param>
        /// <param name="size">Specified the page size</param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> Filter(Expression<Func<TEntity, bool>> filter, out int total, int index = 0,
            int size = 50)
        {
            var skipCount = index * size;
            var resetSet = filter != null
                ? Context.Set<TEntity>().Where<TEntity>(filter).AsQueryable()
                : Context.Set<TEntity>().AsQueryable();
            resetSet = skipCount == 0 ? resetSet.Take(size) : resetSet.Skip(skipCount).Take(size);
            total = resetSet.Count();
            return resetSet.AsQueryable();
        }

        /// <summary>
        /// Gets the object(s) is exists in database by specified filter.
        /// </summary>
        /// <param name="predicate">Specified the filter expression</param>
        /// <returns></returns>
        public bool Contains(Expression<Func<TEntity, bool>> predicate)
        {
            return Context.Set<TEntity>().Any(predicate);
        }

        /// <summary>
        /// Find object by keys.
        /// </summary>
        /// <param name="keys">Specified the search keys.</param>
        /// <returns></returns>
        public virtual TEntity Find(params object[] keys)
        {
            return Context.Set<TEntity>().Find(keys);
        }

        /// <summary>
        /// Find object by specified expression.
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public virtual TEntity Find(Expression<Func<TEntity, bool>> predicate)
        {
            return Context.Set<TEntity>().FirstOrDefault<TEntity>(predicate);
        }

        /// <summary>
        /// Create a new object to database.
        /// </summary>
        /// <param name="t">Specified a new object to create.</param>
        /// <returns></returns>
        public virtual void Create(TEntity t)
        {
            Context.Set<TEntity>().Add(t);
        }

        /// <summary>
        /// Delete the object from database.
        /// </summary>
        /// <param name="t">Specified a existing object to delete.</param>
        public virtual void Delete(TEntity t)
        {
            Context.Set<TEntity>().Remove(t);
        }

        /// <summary>
        /// Delete objects from database by specified filter expression.
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public virtual int Delete(Expression<Func<TEntity, bool>> predicate)
        {
            var objects = Filter(predicate);
            foreach (var obj in objects)
                Context.Set<TEntity>().Remove(obj);
            return Context.SaveChanges();
        }

        /// <summary>
        /// Update object changes and save to database.
        /// </summary>
        /// <param name="t">Specified the object to save.</param>
        /// <returns></returns>
        public virtual void Update(TEntity t)
        {
            try
            {
                var entry = Context.Entry(t);
                Context.Set<TEntity>().Attach(t);
                entry.State = EntityState.Modified;
            }
            catch (OptimisticConcurrencyException ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Select Single Item by specified expression.
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public TEntity FirstOrDefault(Expression<Func<TEntity, bool>> expression)
        {
            return All().FirstOrDefault(expression);
        }
    }

IUnitOfWork接口定義了方法獲取特定的Repository, 執(zhí)行存儲過程, SaveChange方法提交修改,統(tǒng)一更新數(shù)據(jù)。

  • IUnitOfWork接口代碼:
    public interface IUnitOfWork : IDisposable
    {
        DbContext DbContext { get; }
        TRepository GetRepository<TRepository>() where TRepository : class;
        void ExecuteProcedure(string procedureCommand, params object[] sqlParams);
        void ExecuteSql(string sql);
        List<T> SqlQuery<T>(string sql);
        void SaveChanges();
    }

UnitOfWork代碼, 代碼中使用到了Autofac中的IComponentContext來獲取Repository實例

    public class UnitOfWork : IUnitOfWork
    {
        private readonly IComponentContext _componentContext;
        protected readonly DbContext Context;

        public UnitOfWork(DbContext context, IComponentContext componentContext)
        {
            Context = context;
            _componentContext = componentContext;
        }

        public DbContext DbContext => Context;

        public TRepository GetRepository<TRepository>() where TRepository : class
        {
            return _componentContext.Resolve<TRepository>();
        }

        public void ExecuteProcedure(string procedureCommand, params object[] sqlParams)
        {
            Context.Database.ExecuteSqlCommand(procedureCommand, sqlParams);
        }

        public void ExecuteSql(string sql)
        {
            Context.Database.ExecuteSqlCommand(sql);
        }

        public List<T> SqlQuery<T>(string sql)
        {
            return Context.Database.SqlQuery<T>(sql).ToList();
        }

        public void SaveChanges()
        {
            try
            {
                Context.SaveChanges();
            }
            catch (InvalidOperationException ex)
            {
                if (!ex.Message.Contains("The changes to the database were committed successfully"))
                {
                    throw;
                }
            }
        }

        public void Dispose()
        {
            Context?.Dispose();
        }
    }

三、Repository設計的具體的使用

這里我們定義一個IStudentRepository接口, 包含了方法GetAllStudents(), 同時繼承于IRepository<Student>接口

public interface IStudentRepository : IRepository<Student>
{
    IEnumerable<dynamic> GetAllStudents();
}

接著定義StudentRepository類來實現(xiàn)這個接口

public class StudentRepository : BaseRepository<Student>, IStudentRepository
{
    private readonly SchoolContext _context;

    public StudentRepository(SchoolContext context)
        : base(context)
    {
        _context = context;
    }

    public IEnumerable<dynamic> GetAllStudents()
    {
        return _context.Students;
    }
}
  • Application_Start方法中使用Autofac注冊Repository的代碼如下:
    var builder = new ContainerBuilder();

    //register controllers
    builder.RegisterControllers(typeof(MvcApplication).Assembly);

    //register repository
    builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces();

    //add the Entity Framework context to make sure only one context per request
    builder.RegisterType<SchoolContext>().InstancePerRequest();
    builder.Register(c => c.Resolve<SchoolContext>()).As<DbContext>().InstancePerRequest();

    var container = builder.Build();
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  • 在控制器中注入使用Repository的代碼如下:
private readonly IUnitOfWork _repositoryCenter;

private readonly IStudentRepository _studentRepository;

public HomeController(IUnitOfWork repositoryCenter)
{
    _repositoryCenter = repositoryCenter;
    _studentRepository = _repositoryCenter.GetRepository<IStudentRepository>();
}

public ActionResult Index(Student sessionStudent)
{
    var students = _studentRepository.GetAllStudents();

    // 同時你也可以使用定義于IRepository<Student>中的方法, 比如:

    _studentRepository.Delete(students.First());
    _repositoryCenter.SaveChanges();

    ...

    return View(students);
}

四、思路總結

上面的設計,把Repository的通用代碼剝離到父類中,同時又允許每個Repository擴展自己的方法,達到了比較理想的狀態(tài)。

只是現(xiàn)在的設計和Autofac耦合了,但是如果想繼續(xù)剝離Autofac直接使用 _repositoryCenter.GetRepository<IStudentRepository>(); 的方式獲取IStudentRepository的實例就很困難了。

五、案例源碼

源代碼倉庫 AutoFacMvc

到此這篇關于ASP.Net Core基于EF6、Unitwork、Autofac實現(xiàn)Repository模式的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • ASP.NET筆記之CKEditor的使用方法

    ASP.NET筆記之CKEditor的使用方法

    本篇文章小編為大家介紹,ASP.NET筆記之CKEditor的使用方法。需要的朋友參考下
    2013-04-04
  • 剖析Asp.Net路由系統(tǒng)實現(xiàn)原理

    剖析Asp.Net路由系統(tǒng)實現(xiàn)原理

    本篇文章主要介紹了剖析Asp.Net路由系統(tǒng)實現(xiàn)原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • .Net Core中使用EFCore生成反向工程

    .Net Core中使用EFCore生成反向工程

    這篇文章介紹了.Net Core中使用EFCore生成反向工程的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-06-06
  • asp.net core razor自定義taghelper的方法

    asp.net core razor自定義taghelper的方法

    這篇文章主要介紹了asp.net core razor自定義taghelper的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • .NET 中的裝箱與拆箱實現(xiàn)過程

    .NET 中的裝箱與拆箱實現(xiàn)過程

    本文分別介紹裝箱(boxing)與拆箱(unboxing)的實現(xiàn)過程,感興趣的朋友可以了解下
    2013-01-01
  • asp.net Server.MapPath方法注意事項

    asp.net Server.MapPath方法注意事項

    當我發(fā)布之后,對存儲圖片的文件夾創(chuàng)建了虛擬目錄,并賦予該目錄寫入的權限,但是,當我上傳圖片的時候,總是失敗。以前沒遇到過這種情況,覺得很是怪異,所以想盡辦法去解決。
    2008-09-09
  • ASP.NET數(shù)據(jù)綁定控件詳解

    ASP.NET數(shù)據(jù)綁定控件詳解

    這篇文章主要為大家詳細介紹了ASP.NET數(shù)據(jù)綁定控件,為大家解析了ListBox、GridView、Repeater三個數(shù)據(jù)綁定控件的“高效分頁”,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • .NET垃圾回收器原理及使用

    .NET垃圾回收器原理及使用

    這篇文章介紹了.NET垃圾回收器原理及使用方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-01-01
  • .NET 6開發(fā)TodoList應用之實現(xiàn)全局異常處理

    .NET 6開發(fā)TodoList應用之實現(xiàn)全局異常處理

    因為在項目中,會有各種各樣的領域異?;蛳到y(tǒng)異常被拋出來,那么在Controller里就需要進行完整的try-catch捕獲,并根據(jù)是否有異常拋出重新包裝返回值。有沒有辦法讓框架自己去做這件事呢?本文將為大家介紹如何實現(xiàn)全局異常處理,需要的可以參考一下
    2021-12-12
  • .NET中JSON的序列化和反序列化的幾種方式

    .NET中JSON的序列化和反序列化的幾種方式

    JSON是一種輕量級的數(shù)據(jù)交換格式。這篇文章主要介紹了.NET中JSON的序列化和反序列化的幾種方式,非常具有實用價值,需要的朋友可以參考下
    2018-06-06

最新評論