标签:构造方法 没有 lambda表达式 intern config 灵活运用 方式 容器 很多
public void Register(ContainerBuilder builder,ITypeFinder typeFinder)
{
builder.RegisterType<ConsoleLogger>().As<ILogger>();
//通过类型注册
builder.RegisterType(typeof(ConfigReader));
//实例组件
builder.RegisterInstance(NopEngine).As<IEngine>().SingleInstance();
//Lambda表达式组件
builder.Register(c => new DefaultDapperContext("数据库连接字符串", DataProvider.Mysql)).InstancePerLifetimeScope();
//开放泛型
builder.RegisterGeneric(typeof(NHibernateRepository<>))
.As(typeof(IRepository<>))
.InstancePerLifetimeScope();
//程序集扫描
var dataAccess = Assembly.GetExecutingAssembly();
builder.RegisterAssemblyTypes(dataAccess)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces();
}
/// <summary>
/// 依赖注册接口定义 实现此接口可以使用autofac依赖注入
/// </summary>
public interface IDependencyRegistrar
{
/// <summary>
/// 注册服务或接口
/// </summary>
/// <param name="builder">ContainerBuilder对象</param>
/// <param name="typeFinder">类型查找器 </param>
/// <param name="config">配置参数</param>
void Register(ContainerBuilder builder, ITypeFinder typeFinder, NetProOption config);
/// <summary>
///依赖注册执行顺序 从小到大执行
/// </summary>
int Order { get; }
}
其次,实现接口
/// <summary>
/// Dependency registrar
/// </summary>
public class DependencyRegistrar : IDependencyRegistrar
{
/// <summary>
/// Register services and interfaces
/// </summary>
/// <param name="builder">Container builder</param>
/// <param name="typeFinder">Type finder</param>
/// <param name="config">Config</param>
public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
{
//普通注册
builder.RegisterType<ShipStationService>().As<IShipStationService>().InstancePerLifetimeScope();
//这里通过程序集扫描
builder.RegisterAssemblyTypes(typeFinder.GetAssemblies()
.Where(r => RegexHelper.IsMatch(r.GetName().Name,"模式字符串").ToArray())
.Where(t => t.Name.EndsWith("Service"))
}
/// <summary>
/// Order of this dependency registrar implementation
/// </summary>
public int Order => 2;
}
注册依赖项
/// <summary>
/// Register dependencies
/// </summary>
/// <param name="containerBuilder">Container builder</param>
/// <param name="nopConfig">Nop configuration parameters</param>
public virtual void RegisterDependencies(ContainerBuilder containerBuilder, NopConfig nopConfig)
{
//register engine
containerBuilder.RegisterInstance(this).As<IEngine>().SingleInstance();
//register type finder
containerBuilder.RegisterInstance(_typeFinder).As<ITypeFinder>().SingleInstance();
//find dependency registrars provided by other assemblies
var dependencyRegistrars = _typeFinder.FindClassesOfType<IDependencyRegistrar>();
//create and sort instances of dependency registrars
var instances = dependencyRegistrars
.Select(dependencyRegistrar => (IDependencyRegistrar)Activator.CreateInstance(dependencyRegistrar))
.OrderBy(dependencyRegistrar => dependencyRegistrar.Order);
//register all provided dependencies
foreach (var dependencyRegistrar in instances)
dependencyRegistrar.Register(containerBuilder, _typeFinder, nopConfig);
}
标签:构造方法 没有 lambda表达式 intern config 灵活运用 方式 容器 很多
原文地址:https://www.cnblogs.com/ccaa/p/13030372.html