Domanda

In Windsor IOC container è possibile registrare un tipo che ho già ottenuto un esempio invece di avere il contenitore di crearla?

È stato utile?

Soluzione

C'è un AddComponentInstance metodo sul Contenitore per il Kernel di proprietà.

Da l'Unità di Test:

[Test]
public void AddComponentInstance()
{
    CustomerImpl customer = new CustomerImpl();

    kernel.AddComponentInstance("key", typeof(ICustomer), customer);
    Assert.IsTrue(kernel.HasComponent("key"));

    CustomerImpl customer2 = kernel["key"] as CustomerImpl;
    Assert.AreSame(customer, customer2);

    customer2 = kernel[typeof(ICustomer)] as CustomerImpl;
    Assert.AreSame(customer, customer2);
}

[Test]
public void AddComponentInstance_ByService()
{
    CustomerImpl customer = new CustomerImpl();

    kernel.AddComponentInstance <ICustomer>(customer);
    Assert.AreSame(kernel[typeof(ICustomer)],customer);
}

[Test]
public void AddComponentInstance2()
{
    CustomerImpl customer = new CustomerImpl();

    kernel.AddComponentInstance("key", customer);
    Assert.IsTrue(kernel.HasComponent("key"));

    CustomerImpl customer2 = kernel["key"] as CustomerImpl;
    Assert.AreSame(customer, customer2);

    customer2 = kernel[typeof(CustomerImpl)] as CustomerImpl;
    Assert.AreSame(customer, customer2);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top