using Autofac; using ERP.Framework.WebApi; using System.Reflection; namespace ERP.Framework.Extensions { /// <summary> /// Autofac扩展 /// </summary> public static class AutofacExtension { /// <summary> /// 初始化Autofac,注入Repository 和 Service /// </summary> /// <param name="builder"></param> /// <param name="assemblyName">程序集</param> public static void InitAutofac(this ContainerBuilder builder) { var assemblyList = AppDomain.CurrentDomain.GetAssemblies() .Where(t => t.FullName != null && t.FullName.Contains("ERP")); //根据名称约定(业务处理层以Service结尾,仓储层以Repository结尾),实现服务接口和服务实现的依赖 builder.RegisterAssemblyTypes(assemblyList.ToArray()) .Where(t => t.IsClass) .Where(t => t.Name.EndsWith("Service") || t.Name.EndsWith("Repository")) .AsSelf().AsImplementedInterfaces() .InstancePerLifetimeScope(); } } }