asp.net core 实现一个简单的仓储的方法
一直有自己写个框架的想法,但是一直没有行动起来,最近比较闲,正好可以开工了.
现在已经完成了两部分.1.一个简单仓储,实现使用的是ef2.IOC部分,这里是把内置的ioc替换成了aotofac,这部分感觉还是有一点缺陷的.下面说
仓储部分
这里主要是接口是实现,目前使用ef实现了仓储的接口.看一下代码
publicinterfaceIRepositorywhereTEntity:class { #regionSelect/Get/Query IQueryable GetAll(); IQueryable GetAllIncluding(paramsExpression >[]propertySelectors); List GetAllList(); Task >GetAllListAsync(); List
GetAllList(Expression >predicate); Task >GetAllListAsync(Expression
>predicate); TQuery (Func ,T>queryMethod); TEntityGet(TPrimaryKeyid); Task GetAsync(TPrimaryKeyid); TEntitySingle(Expression >predicate); Task SingleAsync(Expression >predicate); TEntityFirstOrDefault(TPrimaryKeyid); Task FirstOrDefaultAsync(TPrimaryKeyid); TEntityFirstOrDefault(Expression >predicate); Task FirstOrDefaultAsync(Expression >predicate); TEntityLoad(TPrimaryKeyid); #endregion #regionInsert TEntityInsert(TEntityentity); Task InsertAsync(TEntityentity); #endregion #regionUpdate TEntityUpdate(TEntityentity); Task UpdateAsync(TEntityentity); TEntityUpdate(TPrimaryKeyid,Action updateAction); Task UpdateAsync(TPrimaryKeyid,Func updateAction); #endregion #regionDelete voidDelete(TEntityentity); TaskDeleteAsync(TEntityentity); voidDelete(TPrimaryKeyid); TaskDeleteAsync(TPrimaryKeyid); voidDelete(Expression >predicate); TaskDeleteAsync(Expression >predicate); #endregion #regionAggregates intCount(); Task CountAsync(); intCount(Expression >predicate); Task CountAsync(Expression >predicate); longLongCount(); Task LongCountAsync(); longLongCount(Expression >predicate); Task LongCountAsync(Expression >predicate); #endregion }
下面是实现的部分代码,代码比较占版面,就不贴全了.
publicabstractclassRepositoryBase:IRepository whereTEntity:class { publicabstractIQueryable GetAll(); publicabstractIQueryable GetAllIncluding(paramsExpression >[]propertySelectors); publicvirtualList GetAllList() { returnGetAll().ToList(); } publicvirtualasyncTask >GetAllListAsync() { returnawaitTask.FromResult(GetAllList()); } }
publicclassEfRepositoryBase:RepositoryBase whereTEntity:class whereTDbContext:DbContext { publicvirtualTDbContextContext{privateset;get;} publicvirtualDbSet Table=>Context.Set (); publicEfRepositoryBase(TDbContextcontext) { Context=context; } publicoverrideIQueryable GetAll() { returnTable; } publicoverrideIQueryable GetAllIncluding(paramsExpression >[]propertySelectors) { if(propertySelectors==null) { returnGetAll(); } varlinq=GetAll(); foreach(variteminpropertySelectors) { linq=linq.Include(item); } returnlinq; } }
注意看EfRepositoryBase继承了RepositoryBase,而RepositoryBase实现了IRepository.这里的RepositoryBase是所有实现的基类.GetAllList虚方法直接调用了抽象方法GetAll,这样在EfRepositoryBase中就可以减少很多代码了.
这里有个坑EfRepositoryBase是不能直接注册到IOC中的,因为EfRepositoryBase和IRepository的泛型参数个数不一致,ioc不能找到多出的一个泛型的值.使用仓储的时候继承EfRepositoryBase把dbcontext传进去就好了
publicclassTestRepository:EfRepositoryBase whereTEntity:class { publicTestRepository(TestContextcontext) :base(context) { } }
IOC部分
asp.netcore微软提供了一个简单的IOC,但是接口比较少,替换成我们熟悉的ioc框架就方便多了.asp.netcore也有很方便的替换ioc的方法.简单说就是修改ConfigureServices方法的返回值为IServiceProvider.我使用了autofac,下面看代码.
publicIServiceProviderConfigureServices(IServiceCollectionservices) { services.AddMvc(); returnservices.AddLuna(); } publicstaticIServiceProviderAddLuna ([NotNull]thisIServiceCollectionservices) whereTModule:IModule,new() { varbuilder=newContainerBuilder(); builder.Populate(services); builder.RegisterModule (); returnnewAutofacServiceProvider(builder.Build()); } publicclassAutofacModule:Module { protectedoverridevoidLoad(ContainerBuilderbuilder) { builder.RegisterType (); builder.RegisterGeneric(typeof(TestRepository<,>)).As(typeof(IRepository<,>)) .InstancePerLifetimeScope(); } }
这里的Module和IModule是autofac的,功能已经实现了,但是作为框架来说直接暴露了autofac的东西显然是不合适的,下一步要实现一个框架自身的模块加载方式.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。