asp.net core 實(shí)現(xiàn)一個簡單的倉儲的方法
一直有自己寫個框架的想法,但是一直沒有行動起來,最近比較閑,正好可以開工了.
現(xiàn)在已經(jīng)完成了兩部分.1.一個簡單倉儲,實(shí)現(xiàn)使用的是ef 2.IOC部分,這里是把內(nèi)置的ioc替換成了aotofac,這部分感覺還是有一點(diǎn)缺陷的.下面說
倉儲部分
這里主要是接口是實(shí)現(xiàn),目前使用ef實(shí)現(xiàn)了倉儲的接口.看一下代碼
public interface IRepository<TEntity, TPrimaryKey>
where TEntity : class
{
#region Select/Get/Query
IQueryable<TEntity> GetAll();
IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] propertySelectors);
List<TEntity> GetAllList();
Task<List<TEntity>> GetAllListAsync();
List<TEntity> GetAllList(Expression<Func<TEntity, bool>> predicate);
Task<List<TEntity>> GetAllListAsync(Expression<Func<TEntity, bool>> predicate);
T Query<T>(Func<IQueryable<TEntity>, T> queryMethod);
TEntity Get(TPrimaryKey id);
Task<TEntity> GetAsync(TPrimaryKey id);
TEntity Single(Expression<Func<TEntity, bool>> predicate);
Task<TEntity> SingleAsync(Expression<Func<TEntity, bool>> predicate);
TEntity FirstOrDefault(TPrimaryKey id);
Task<TEntity> FirstOrDefaultAsync(TPrimaryKey id);
TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate);
Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate);
TEntity Load(TPrimaryKey id);
#endregion
#region Insert
TEntity Insert(TEntity entity);
Task<TEntity> InsertAsync(TEntity entity);
#endregion
#region Update
TEntity Update(TEntity entity);
Task<TEntity> UpdateAsync(TEntity entity);
TEntity Update(TPrimaryKey id, Action<TEntity> updateAction);
Task<TEntity> UpdateAsync(TPrimaryKey id, Func<TEntity, Task> updateAction);
#endregion
#region Delete
void Delete(TEntity entity);
Task DeleteAsync(TEntity entity);
void Delete(TPrimaryKey id);
Task DeleteAsync(TPrimaryKey id);
void Delete(Expression<Func<TEntity, bool>> predicate);
Task DeleteAsync(Expression<Func<TEntity, bool>> predicate);
#endregion
#region Aggregates
int Count();
Task<int> CountAsync();
int Count(Expression<Func<TEntity, bool>> predicate);
Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate);
long LongCount();
Task<long> LongCountAsync();
long LongCount(Expression<Func<TEntity, bool>> predicate);
Task<long> LongCountAsync(Expression<Func<TEntity, bool>> predicate);
#endregion
}
下面是實(shí)現(xiàn)的部分代碼,代碼比較占版面,就不貼全了.
public abstract class RepositoryBase<TEntity, TPrimaryKey> : IRepository<TEntity, TPrimaryKey>
where TEntity : class
{
public abstract IQueryable<TEntity> GetAll();
public abstract IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] propertySelectors);
public virtual List<TEntity> GetAllList()
{
return GetAll().ToList();
}
public virtual async Task<List<TEntity>> GetAllListAsync()
{
return await Task.FromResult(GetAllList());
}
}
public class EfRepositoryBase<TDbContext, TEntity, TPrimaryKey> : RepositoryBase<TEntity, TPrimaryKey>
where TEntity : class
where TDbContext : DbContext
{
public virtual TDbContext Context { private set; get; }
public virtual DbSet<TEntity> Table => Context.Set<TEntity>();
public EfRepositoryBase(TDbContext context)
{
Context = context;
}
public override IQueryable<TEntity> GetAll()
{
return Table;
}
public override IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] propertySelectors)
{
if (propertySelectors == null)
{
return GetAll();
}
var linq = GetAll();
foreach (var item in propertySelectors)
{
linq = linq.Include(item);
}
return linq;
}
}
注意看EfRepositoryBase繼承了RepositoryBase,而RepositoryBase實(shí)現(xiàn)了IRepository.這里的RepositoryBase是所有實(shí)現(xiàn)的基類.GetAllList虛方法直接調(diào)用了抽象方法GetAll,這樣在EfRepositoryBase中就可以減少很多代碼了.
這里有個坑 EfRepositoryBase 是不能直接注冊到IOC中的,因?yàn)镋fRepositoryBase和IRepository的泛型參數(shù)個數(shù)不一致,ioc不能找到多出的一個泛型的值.使用倉儲的時候繼承EfRepositoryBase把dbcontext傳進(jìn)去就好了
public class TestRepository<TEntity, TPrimaryKey> : EfRepositoryBase<TestContext, TEntity, TPrimaryKey> where TEntity : class
{
public TestRepository(TestContext context)
: base(context)
{
}
}
IOC部分
asp.net core 微軟提供了一個簡單的IOC,但是接口比較少,替換成我們熟悉的ioc框架就方便多了. asp.net core 也有很方便的替換ioc的方法.簡單說就是修改ConfigureServices方法的返回值為IServiceProvider.我使用了autofac,下面看代碼.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
return services.AddLuna<AutofacModule>();
}
public static IServiceProvider AddLuna<TModule>([NotNull]this IServiceCollection services)
where TModule : IModule, new()
{
var builder = new ContainerBuilder();
builder.Populate(services);
builder.RegisterModule<TModule>();
return new AutofacServiceProvider(builder.Build());
}
public class AutofacModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<TestContext>();
builder.RegisterGeneric(typeof(TestRepository<,>)).As(typeof(IRepository<,>))
.InstancePerLifetimeScope();
}
}
這里的Module和IModule是autofac的,功能已經(jīng)實(shí)現(xiàn)了,但是作為框架來說直接暴露了autofac的東西顯然是不合適的,下一步要實(shí)現(xiàn)一個框架自身的模塊加載方式.
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET Core 奇淫技巧之偽屬性注入的實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET Core 奇淫技巧之偽屬性注入的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
.NET?7?AOT?的使用及?.NET?與?Go?互相調(diào)用的過程
本文主要介紹如何在.NET和Go語言中如何生成系統(tǒng)(Windows)動態(tài)鏈接庫,又如何從代碼中引用這些庫中的函數(shù),在文章中會演示.NET和Go相互調(diào)用各自生成的動態(tài)鏈接庫,以及對比兩者之間的差異,感興趣的朋友一起看看吧2024-12-12
MVC+EasyUI+三層新聞網(wǎng)站建立 后臺登錄界面的搭建(二)
這篇文章主要為大家詳細(xì)介紹了MVC+EasyUI+三層新聞網(wǎng)站建立的第二篇,教大家如何搭建后臺登錄界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
.NET AppSettings與ConnectionStrings使用案例詳解
這篇文章主要介紹了.NET AppSettings與ConnectionStrings使用案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
CKEditor與dotnetcore實(shí)現(xiàn)圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了CKEditor與dotnetcore實(shí)現(xiàn)圖片上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09
ASP.NET生成兩個日期范圍內(nèi)隨機(jī)時間的實(shí)現(xiàn)方法
這篇文章主要介紹了ASP.NET生成兩個日期范圍內(nèi)隨機(jī)時間的實(shí)現(xiàn)方法,通過自定義函數(shù)記錄開始時間與結(jié)束時間確定時間范圍進(jìn)而生成該時間段的隨機(jī)時間,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12
顯示非站點(diǎn)目錄及映射網(wǎng)絡(luò)磁盤路徑的圖片
本文就將教你怎樣顯示非站點(diǎn)目錄下的圖片,你可以顯示站點(diǎn)所在服務(wù)器所有驅(qū)動器目錄的圖片,以及映射網(wǎng)絡(luò)磁盤路徑的圖片,感興趣的朋友可以了解下就當(dāng)鞏固知識了或許對你學(xué)習(xí).net有所幫助2013-02-02
關(guān)于WPF使用MultiConverter控制Button狀態(tài)的詳細(xì)介紹
本篇文章小編將為大家介紹,關(guān)于WPF使用MultiConverter控制Button狀態(tài)的詳細(xì)介紹。需要的朋友參考下2013-04-04
.NET使用?OpenTelemetry?Traces?追蹤應(yīng)用程序的方法
OpenTelemetry Traces是OpenTelemetry提供的一種遙測數(shù)據(jù)類型,用于記錄和描述在分布式系統(tǒng)中的單個操作或工作單元的生命周期,這篇文章主要介紹了.NET中使用OpenTelemetry Traces追蹤應(yīng)用程序,需要的朋友可以參考下2024-06-06

