Autofac的使用过程,以下为模板
配置:
1、Demo.Repository(1)NuGet包:①EntityFramework②Z.EntityFramework.Extensions③Z.Expressions.Eval④Z.EntityFramework.Plus.EF6注:②③为④的依赖项,先安装②③再安装④
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
数据库映射
在Demo.Repository添加数据库映射后,将Demo.Repository下的App.Config中关于数据库连接的相关配置复制粘贴于Demo.Mvc下的Web.config中
AutofacConfig.cs
public static class AutofacConfig { private static IContainer _container; public static void InitAutofac() { var builder = new ContainerBuilder(); //注册数据库基础操作和工作单元 builder.RegisterGeneric(typeof(BaseDAL<>)).As(typeof(IBaseDAL<>)).PropertiesAutowired(); builder.RegisterType(typeof(UnitWork)).As(typeof(IUnitWork)).PropertiesAutowired(); //注册service层 builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).PropertiesAutowired(); // 注册controller,使用属性注入 builder.RegisterControllers(Assembly.GetCallingAssembly()).PropertiesAutowired(); 注册所有的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()); // 注册所有的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层
实现类(接口类不写)
BaseDAL.cs
public class BaseDAL<T> : IBaseDAL<T> where T : class, new() { private CompressorTestContext dbContext = new CompressorTestContext(); /// <summary> /// 查找单个,且不被上下文所跟踪 /// AsNoTracking:能从数据库获取数据 /// </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> /// 查找单个 /// </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; } }
实现类
public class HandleService:BaseService<sy_sql>
{
BaseDal.where(...)
}
控制器调用
public HandleService HandleService{ get; set; }