本文整理汇总了C#中Container.For方法的典型用法代码示例。如果您正苦于以下问题:C# Container.For方法的具体用法?C# Container.For怎么用?C# Container.For使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Container
的用法示例。
在下文中一共展示了Container.For方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Should_return_an_instance_with_method_chaining_syntax
public void Should_return_an_instance_with_method_chaining_syntax()
{
var container = new Container();
container.For<ICounter>().Use<Counter>();
container.For<IRepository>().Use<Repository>();
container.For<Processor>().Use<Processor>();
var processor = container.Resolve<Processor>();
Assert.IsInstanceOf<Processor>(processor);
Assert.IsInstanceOf<Counter>(processor.Counter);
Assert.IsInstanceOf<Repository>(processor.Repository);
}
示例2: Get_WhenInstanceHasDependency_ReturnsCorrectInstance
public void Get_WhenInstanceHasDependency_ReturnsCorrectInstance()
{
// Arrange
var c = new Container();
c.For<IVehicle>().Use<Car>();
c.For<IEngine>().Use<DieselEngine>();
// Act
var vehicle = c.Get<IVehicle>();
// Assert
Assert.IsInstanceOf<Car>(vehicle);
Assert.IsInstanceOf<DieselEngine>(((Car)vehicle).Engine);
}
示例3: Get_WhenInstanceHasMultipleCtors_Throws
public void Get_WhenInstanceHasMultipleCtors_Throws()
{
// Arrange
var c = new Container();
// Act
// Assert
Assert.Throws<InvalidOperationException>(() => c.For<IVehicle>().Use<Train>());
}
示例4: Get_WhenInstanceDependencyNotConfigures_Throws
public void Get_WhenInstanceDependencyNotConfigures_Throws()
{
// Arrange
var c = new Container();
c.For<IVehicle>().Use<Car>();
// Act
// Assert
Assert.Throws<InvalidOperationException>(() => c.Get<IVehicle>());
}
示例5: Get_WhenInstanceCtorHasNonConfiguresValueType_ReturnsCorrectInstance
public void Get_WhenInstanceCtorHasNonConfiguresValueType_ReturnsCorrectInstance()
{
// Arrange
var c = new Container();
c.For<IVehicle>().Use<Bicycle>();
// Act
var vehicle = c.Get<IVehicle>();
// Assert
Assert.IsInstanceOf<Bicycle>(vehicle);
Assert.AreEqual(0, ((Bicycle)vehicle).NumberOfGears);
}
示例6: Get_WhenInstanceIsClassWithDependency_ReturnsClass
public void Get_WhenInstanceIsClassWithDependency_ReturnsClass()
{
// Arrange
var c = new Container();
c.For<IEngine>().Use<DieselEngine>();
// Act
var vehicle = c.Get<Car>();
// Assert
Assert.IsNotNull(vehicle);
Assert.IsNotNull(vehicle.Engine);
}
示例7: Get_WhenInstanceIsExplicitSetAndResolvedSeveralTimes_ReturnsSameInstance
public void Get_WhenInstanceIsExplicitSetAndResolvedSeveralTimes_ReturnsSameInstance()
{
// Arrange
var mc = new Motorcycle();
var c = new Container();
c.For<IVehicle>().Use(mc);
// Act
var vehicle1 = c.Get<IVehicle>();
var vehicle2 = c.Get<IVehicle>();
// Assert
Assert.IsNotNull(vehicle1);
Assert.IsNotNull(vehicle2);
Assert.AreSame(vehicle1, vehicle2);
}
示例8: Get_WhenInstanceIsConfigured_ReturnsCorrectInstance
public void Get_WhenInstanceIsConfigured_ReturnsCorrectInstance()
{
// Arrange
var c = new Container();
c.For<IVehicle>().Use<Motorcycle>();
// Act
var vehicle = c.Get<IVehicle>();
// Assert
Assert.IsInstanceOf<Motorcycle>(vehicle);
}
示例9: Get_WhenInstanceIsConfiguredWithLamdas_ReturnsCorrectInstance
public void Get_WhenInstanceIsConfiguredWithLamdas_ReturnsCorrectInstance()
{
// Arrange
var c = new Container();
c.For<IVehicle>().Use<Bicycle>(() => new Bicycle(10));
// Act
var vehicle = c.Get<IVehicle>();
// Assert
Assert.IsInstanceOf<Bicycle>(vehicle);
Assert.AreEqual(10, ((Bicycle)vehicle).NumberOfGears);
}
示例10: Should_return_the_same_object_if_a_singleton_with_method_chaining_syntax
public void Should_return_the_same_object_if_a_singleton_with_method_chaining_syntax()
{
var container = new Container();
container.For<ICounter>().Use<Counter>().AsSingleton();
var counterA = container.Resolve<Counter>();
var counterB = container.Resolve<Counter>();
Assert.AreSame(counterA, counterB);
}