Autofac的使用
Autofac的使用過程,以下為模板
配置:
1、Demo.Repository(1)NuGet包:①EntityFramework②Z.EntityFramework.Extensions③Z.Expressions.Eval④Z.EntityFramework.Plus.EF6注:②③為④的依賴項(xiàng),先安裝②③再安裝④
2、Demo.Service(1)NuGet包:①Autofac②Autofac.Mvc5(2)引用:Demo.Repository
3、Demo.Mvc(1)NuGet包:①EntityFramework②Autofac③Autofac.Mvc5(2)引用:Demo.Repository、Demo.Service
數(shù)據(jù)庫(kù)映射
在Demo.Repository添加數(shù)據(jù)庫(kù)映射后,將Demo.Repository下的App.Config中關(guān)于數(shù)據(jù)庫(kù)連接的相關(guān)配置復(fù)制粘貼于Demo.Mvc下的Web.config中
AutofacConfig.cs
public static class AutofacConfig?? ?{?? ??? ?private static IContainer _container;?? ??? ?public static void InitAutofac()?? ??? ?{?? ??? ??? ?var builder = new ContainerBuilder();?? ??? ??? ?//注冊(cè)數(shù)據(jù)庫(kù)基礎(chǔ)操作和工作單元?? ??? ??? ?builder.RegisterGeneric(typeof(BaseDAL<>)).As(typeof(IBaseDAL<>)).PropertiesAutowired();?? ??? ??? ?builder.RegisterType(typeof(UnitWork)).As(typeof(IUnitWork)).PropertiesAutowired();?? ??? ??? ?//注冊(cè)service層?? ??? ??? ?builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).PropertiesAutowired();?? ??? ??? ?// 注冊(cè)controller,使用屬性注入?? ??? ??? ?builder.RegisterControllers(Assembly.GetCallingAssembly()).PropertiesAutowired();?? ??? ??? ?注冊(cè)所有的ApiControllers?? ??? ??? ?//builder.RegisterApiControllers(Assembly.GetCallingAssembly()).PropertiesAutowired();?? ??? ??? ?builder.RegisterModelBinders(Assembly.GetCallingAssembly());?? ??? ??? ?builder.RegisterModelBinderProvider();?? ??? ??? ?// OPTIONAL: Register web abstractions like HttpContextBase.?? ??? ??? ?//builder.RegisterModule<AutofacWebTypesModule>();?? ??? ??? ?// OPTIONAL: Enable property injection in view pages.?? ??? ??? ?builder.RegisterSource(new ViewRegistrationSource());?? ??? ??? ?// 注冊(cè)所有的Attribute?? ??? ??? ?builder.RegisterFilterProvider();?? ??? ??? ?// Set the dependency resolver to be Autofac.?? ??? ??? ?_container = builder.Build();?? ??? ??? ?//Set the MVC DependencyResolver?? ??? ??? ?DependencyResolver.SetResolver(new AutofacDependencyResolver(_container));?? ??? ??? ?//Set the WebApi DependencyResolver?? ??? ??? ?//GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver((IContainer)_container);?? ??? ?}?? ?}
DLL層
實(shí)現(xiàn)類(接口類不寫)
BaseDAL.cs
public class BaseDAL<T> : IBaseDAL<T> where T : class, new()?? ?{?? ??? ?private CompressorTestContext dbContext = new CompressorTestContext();?? ??? ?/// <summary>?? ??? ?/// 查找單個(gè),且不被上下文所跟蹤?? ??? ?/// AsNoTracking:能從數(shù)據(jù)庫(kù)獲取數(shù)據(jù)?? ??? ?/// </summary>?? ??? ?public T FindSingle(Expression<Func<T, bool>> exp)?? ??? ?{?? ??? ??? ?dbContext.Configuration.ProxyCreationEnabled = false;?? ??? ??? ?return dbContext.Set<T>().AsNoTracking().FirstOrDefault(exp);?? ??? ?}?? ??? ?public IQueryable<T> Find(Expression<Func<T, bool>> exp = null)?? ??? ?{?? ??? ??? ?var dbSet = dbContext.Set<T>().AsNoTracking().AsQueryable();?? ??? ??? ?if (exp != null)?? ??? ??? ??? ?dbSet = dbSet.Where(exp);?? ??? ??? ?return dbSet;?? ??? ?}?? ??? ?public void Add2(T t)?? ??? ?{?? ??? ??? ?dbContext.Set<T>().Add(t);?? ??? ?}?? ??? ?public void Delete2(T t)?? ??? ?{?? ??? ??? ?dbContext.Entry<T>(t).State = EntityState.Deleted;?? ??? ??? ?//dbContext.Set<T>().Remove(t);?? ??? ?}?? ??? ?public void Update2(T t)?? ??? ?{?? ??? ??? ?dbContext.Set<T>().AddOrUpdate(t);?? ??? ?}?? ??? ?public bool Add(T t)?? ??? ?{?? ??? ??? ?dbContext.Set<T>().Add(t);?? ??? ??? ?return SaveChanges();?? ??? ?}?? ??? ?public bool Delete(T t)?? ??? ?{?? ??? ??? ?dbContext.Entry<T>(t).State = EntityState.Deleted;?? ??? ??? ?//dbContext.Set<T>().Remove(t);?? ??? ??? ?return SaveChanges();?? ??? ?}?? ??? ?public bool Update(T t)?? ??? ?{?? ??? ??? ?dbContext.Set<T>().AddOrUpdate(t);?? ??? ??? ?return SaveChanges();?? ??? ?}?? ??? ?public bool SaveChanges()?? ??? ?{?? ??? ??? ?return dbContext.SaveChanges() > 0;?? ??? ?}?? ?}
UnitWork .cs
public class UnitWork : IUnitWork?? ?{?? ??? ?private CompressorTestContext dbContext = new CompressorTestContext();?? ??? ?public IQueryable<T> Find<T>(Expression<Func<T, bool>> exp = null) where T : class?? ??? ?{?? ??? ??? ?var dbSet = dbContext.Set<T>().AsNoTracking().AsQueryable();?? ??? ??? ?if (exp != null)?? ??? ??? ??? ?dbSet = dbSet.Where(exp);?? ??? ??? ?return dbSet;?? ??? ?}?? ??? ?/// <summary>?? ??? ?/// 查找單個(gè)?? ??? ?/// </summary>?? ??? ?public T FindSingle<T>(Expression<Func<T, bool>> exp =null) where T : class?? ??? ?{?? ??? ??? ?return dbContext.Set<T>().AsNoTracking().FirstOrDefault(exp);?? ??? ?}?? ??? ?public void ExecuteSql(string sql)?? ??? ?{?? ??? ??? ?dbContext.Database.ExecuteSqlCommand(sql);?? ??? ?}?? ?}
BAL層
接口類
BaseService.cs
public class BaseService<T> where T : class?? ?{?? ??? ?public IBaseDAL<T> BaseDAL { get; set; }?? ??? ?public IUnitWork UnitWork { get; set; }?? ?}
實(shí)現(xiàn)類
public class HandleService:BaseService<sy_sql>
{
BaseDal.where(...)
}
控制器調(diào)用
public HandleService HandleService{ get; set; }
總結(jié)
以上是生活随笔為你收集整理的Autofac的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: caa catia 视图缩放_CATIA
- 下一篇: php 让浏览器格式化显示XML