本文整理汇总了C#中LinFu.IoC.ServiceContainer.Initialize方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceContainer.Initialize方法的具体用法?C# ServiceContainer.Initialize怎么用?C# ServiceContainer.Initialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LinFu.IoC.ServiceContainer
的用法示例。
在下文中一共展示了ServiceContainer.Initialize方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestPropertyInjection
private static void TestPropertyInjection(string serviceName)
{
var mockTarget = new Mock<IInjectionTarget>();
mockTarget.Expect(t => t.SetValue(123));
var target = mockTarget.Object;
var container = new ServiceContainer();
container.AddService(serviceName, target);
// Use the named fluent interface for
// named instances
if (!String.IsNullOrEmpty(serviceName))
{
container.Initialize<IInjectionTarget>(serviceName)
.With(service => service.SetValue(123));
}
else
{
// Otherwise, use the other one
container.Initialize<IInjectionTarget>()
.With(service => service.SetValue(123));
}
var result = container.GetService<IInjectionTarget>(serviceName);
Assert.IsNotNull(result);
// The container should initialize the
// service on every request
mockTarget.VerifyAll();
}
示例2: Main
static void Main(string[] args)
{
string directory = AppDomain.CurrentDomain.BaseDirectory;
IServiceContainer container = new ServiceContainer();
// Load CarLibrary3.dll; If you need load
// all the libaries in a directory, use "*.dll" instead
container.LoadFrom(directory, "CarLibrary3.dll");
// Inject the OldPerson type
container.Inject<IPerson>("OldPerson").Using<Person>();
container.Initialize<IPerson>("OldPerson")
.With(p =>
{
p.Age = 99;
p.Name = "OldPerson";
});
// Inject the YoungPerson type
container.Inject<IPerson>("YoungPerson").Using<Person>();
container.Initialize<IPerson>("YoungPerson")
.With(p =>
{
p.Age = 16;
p.Name = "YoungPerson";
});
// Inject the DeadEngine type
container.Inject<IEngine>("DeadEngine").Using<DeadEngine>()
.OncePerRequest();
// Inject the OldEngine type
container.Inject<IEngine>("OldEngine").Using<OldEngine>()
.OncePerRequest();
// Inject the BrokenVehicle type into the container
container.Inject<IVehicle>("BrokenVehicle")
.Using<Car>().OncePerRequest();
#region Broken Vehicle Configuration
AddVehicle(container, "BrokenVehicle", "DeadEngine", "YoungPerson");
#endregion
#region Old Vehicle Configuration
AddVehicle(container, "OldVehicle", "OldEngine", "OldPerson");
#endregion
// Inject the OldVehicle type into the container
container.Inject<IVehicle>("OldVehicle")
.Using<Car>().OncePerRequest();
Person person = new Person();
person.Name = "Someone";
person.Age = 18;
container.AddService<IPerson>(person);
var brokenVehicle = container.GetService<IVehicle>("BrokenVehicle");
var oldVehicle = container.GetService<IVehicle>("OldVehicle");
Console.Write("Broken Vehicle: ");
brokenVehicle.Move();
Console.Write("Old Vehicle: ");
oldVehicle.Move();
Console.WriteLine("Press ENTER to continue...");
Console.ReadLine();
}
示例3: ShouldBeAbleToCreateClosedGenericTypeUsingACustomFactoryInstance
public void ShouldBeAbleToCreateClosedGenericTypeUsingACustomFactoryInstance()
{
var container = new ServiceContainer();
container.Initialize();
container.LoadFrom(typeof (MyClass<>).Assembly);
// Get ServiceNotFoundException here instead of a service instance.
string serviceName = "frobozz";
var service = container.GetService<MyClass<string>>(serviceName);
Console.WriteLine("foo");
Assert.AreEqual(serviceName, service.Value);
}