2008-09-18 2 views

ответ

13

В свойстве Ядра контейнера есть метод AddComponentInstance.

Из модульных тестов:

[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); 
    } 
+6

В качестве обновления, этот метод в настоящее время не рекомендуется. Используйте `container.Register (Component.For () .Instance (myT)); вместо этого. – eouw0o83hf 2012-04-10 13:36:07

Смежные вопросы