AutoFacBootStrapper.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Autofac;
  2. using Autofac.Core;
  3. using Autofac.Integration.Mvc;
  4. using Autofac.Integration.WebApi;
  5. using CB.Cache;
  6. using CB.Cache.WebCache;
  7. using Data.Dapper;
  8. using Data.Interfaces;
  9. using Owin;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Reflection;
  15. using System.Web;
  16. using System.Web.Http;
  17. using System.Web.Mvc;
  18. namespace UC.Api
  19. {
  20. public class AutoFacBootStrapper
  21. {
  22. public static void AutoFacInit()
  23. {
  24. var builder = new ContainerBuilder();
  25. HttpConfiguration config = GlobalConfiguration.Configuration;
  26. SetupResolveRules(builder);
  27. //注册Commonc层
  28. builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).PropertiesAutowired();
  29. ////注册所有的Controllers
  30. builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
  31. //注册所有的ApiControllers
  32. builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
  33. // 可选:在视图页面中启用属性注入。
  34. builder.RegisterSource(new ViewRegistrationSource());
  35. // 注册所有的Attribute
  36. builder.RegisterFilterProvider();
  37. var container = builder.Build();
  38. //注册api容器需要使用HttpConfiguration对象
  39. config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
  40. //注册Mvc容器
  41. DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  42. }
  43. private static void SetupResolveRules(ContainerBuilder builder)
  44. {
  45. //var controllerAssmbly = Assembly.Load("UC.Api");
  46. //builder.RegisterApiControllers(controllerAssmbly);
  47. //WebAPI只用引用services和repository的接口,不用引用实现的dll。
  48. //如需加载实现的程序集,将dll拷贝到bin目录下即可,不用引用dll
  49. var dal = Assembly.Load("UC.DAL");
  50. var bll = Assembly.Load("UC.BLL");
  51. var data = Assembly.Load("Data.Dapper");
  52. var idata = Assembly.Load("Data.Interfaces");
  53. var cache = Assembly.Load("Data.Cache");
  54. //根据名称约定(服务层的接口和实现均以Services结尾),实现服务接口和服务实现的依赖
  55. builder.RegisterAssemblyTypes(bll, bll)
  56. .Where(t => t.Name.EndsWith("BLL"))
  57. .AsImplementedInterfaces().PropertiesAutowired();
  58. //根据名称约定(服务层的接口和实现均以Services结尾),实现服务接口和服务实现的依赖
  59. builder.RegisterTypes(dal.GetTypes())
  60. .Where(a => a.Name.Contains("DAL"))
  61. .AsImplementedInterfaces().PropertiesAutowired(); ;
  62. //数据库注册
  63. builder.RegisterAssemblyTypes(idata, data)
  64. .Where(t => t.Name.EndsWith("Database"))
  65. .AsImplementedInterfaces().PropertiesAutowired();
  66. //缓存注册
  67. builder.RegisterType<WebCache>().As<ICache>()
  68. .AsImplementedInterfaces().PropertiesAutowired();
  69. ////根据名称约定(数据访问层的接口和实现均以Repository结尾),实现数据访问接口和数据访问实现的依赖
  70. //builder.RegisterAssemblyTypes(iRepository, repository)
  71. // .Where(t => t.Name.EndsWith("Repository"))
  72. // .AsImplementedInterfaces();
  73. }
  74. }
  75. }