AutofacExtension.cs 1.0 KB

12345678910111213141516171819202122232425262728
  1. using Autofac;
  2. namespace ERP.Framework.Extensions
  3. {
  4. /// <summary>
  5. /// Autofac扩展
  6. /// </summary>
  7. public static class AutofacExtension
  8. {
  9. /// <summary>
  10. /// 初始化Autofac,注入Repository 和 Service
  11. /// </summary>
  12. /// <param name="builder"></param>
  13. /// <param name="assemblyName">程序集</param>
  14. public static void InitAutofac(this ContainerBuilder builder)
  15. {
  16. var assemblyList = AppDomain.CurrentDomain.GetAssemblies()
  17. .Where(t => t.FullName != null && t.FullName.Contains("ERP"));
  18. //根据名称约定(业务处理层以Service结尾,仓储层以Repository结尾),实现服务接口和服务实现的依赖
  19. builder.RegisterAssemblyTypes(assemblyList.ToArray())
  20. .Where(t => t.IsClass)
  21. .Where(t => t.Name.EndsWith("Service") || t.Name.EndsWith("Repository"))
  22. .AsSelf().AsImplementedInterfaces()
  23. .InstancePerLifetimeScope();
  24. }
  25. }
  26. }