本文整理汇总了C#中Autofac.ContainerBuilder.RegisterTypeRegistration方法的典型用法代码示例。如果您正苦于以下问题:C# ContainerBuilder.RegisterTypeRegistration方法的具体用法?C# ContainerBuilder.RegisterTypeRegistration怎么用?C# ContainerBuilder.RegisterTypeRegistration使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Autofac.ContainerBuilder
的用法示例。
在下文中一共展示了ContainerBuilder.RegisterTypeRegistration方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RegisterTypeRegistration_Default_WithEnumerationParameter
public void RegisterTypeRegistration_Default_WithEnumerationParameter()
{
var itemNames = new string[]
{
"first",
"second",
"third"
};
var registration = new TypeRegistration<RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.ResolvedEnumerable<ISampleService>(itemNames)));
registration.IsDefault = true;
registration.Lifetime = TypeRegistrationLifetime.Transient;
var builder = new ContainerBuilder();
builder.RegisterTypeRegistration(registration);
var first = new SampleServiceImpl();
builder.RegisterInstance(first).Named<ISampleService>("first");
var second = new SampleServiceImpl();
builder.RegisterInstance(second).Named<ISampleService>("second");
var third = new SampleServiceImpl();
builder.RegisterInstance(third).Named<ISampleService>("third");
var container = builder.Build();
var resolved = container.Resolve<RegisteredServiceConsumer>();
Assert.IsInstanceOf<IEnumerable<ISampleService>>(resolved.CtorParameter, "The constructor parameter was not the right type.");
var services = ((IEnumerable<ISampleService>)resolved.CtorParameter).ToArray();
Assert.AreSame(first, services[0], "The first enumerable service was not resolved properly.");
Assert.AreSame(second, services[1], "The second enumerable service was not resolved properly.");
Assert.AreSame(third, services[2], "The third enumerable service was not resolved properly.");
}
示例2: RegisterTypeRegistration_Default_NoParameters
public void RegisterTypeRegistration_Default_NoParameters()
{
var registration = new TypeRegistration<RegisteredServiceConsumer>(() => new RegisteredServiceConsumer());
registration.IsDefault = true;
registration.Lifetime = TypeRegistrationLifetime.Singleton;
var builder = new ContainerBuilder();
builder.RegisterTypeRegistration(registration);
var container = builder.Build();
var instance = container.Resolve<RegisteredServiceConsumer>();
Assert.AreEqual("DEFAULTCTOR", instance.CtorParameter, "The default constructor should have been invoked.");
var instance2 = container.Resolve<RegisteredServiceConsumer>();
Assert.AreSame(instance, instance2, "The lifetime was not set on the registration.");
}
示例3: RegisterTypeRegistration_Default_WithSimpleParameter
public void RegisterTypeRegistration_Default_WithSimpleParameter()
{
var registration = new TypeRegistration<RegisteredServiceConsumer>(() => new RegisteredServiceConsumer("abc"));
registration.IsDefault = true;
registration.Lifetime = TypeRegistrationLifetime.Transient;
var builder = new ContainerBuilder();
builder.RegisterTypeRegistration(registration);
var container = builder.Build();
var instance = container.Resolve<RegisteredServiceConsumer>();
Assert.AreEqual("abc", instance.CtorParameter, "The string parameter constructor should have been invoked with the appropriate argument.");
var instance2 = container.Resolve<RegisteredServiceConsumer>();
Assert.AreNotSame(instance, instance2, "The lifetime was not set on the registration.");
}
示例4: RegisterTypeRegistration_NullTypeRegistration
public void RegisterTypeRegistration_NullTypeRegistration()
{
TypeRegistration registration = null;
var builder = new ContainerBuilder();
Assert.Throws<ArgumentNullException>(() => builder.RegisterTypeRegistration(registration));
}
示例5: RegisterTypeRegistration_Named_WithParameters
public void RegisterTypeRegistration_Named_WithParameters()
{
var registration = new TypeRegistration<RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.Resolved<ISampleService>()));
registration.Name = "named-service";
registration.Lifetime = TypeRegistrationLifetime.Transient;
var dependency = new SampleServiceImpl();
var builder = new ContainerBuilder();
builder.RegisterTypeRegistration(registration);
builder.RegisterInstance(dependency).As<ISampleService>();
var container = builder.Build();
var instance = container.ResolveNamed<RegisteredServiceConsumer>("named-service"); ;
Assert.AreSame(dependency, instance.CtorParameter, "The service implementation parameter constructor should have been invoked with the appropriate argument.");
var instance2 = container.ResolveNamed<RegisteredServiceConsumer>("named-service"); ;
Assert.AreNotSame(instance, instance2, "The lifetime was not set on the registration.");
}