2008-09-18 1 views

Antwort

13

Es gibt eine AddComponentInstance-Methode für die Kernel-Eigenschaft des Containers.

Aus den Unit-Tests:

[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

als Update wird diese Technik jetzt veraltet. Verwenden Sie 'container.Register (Component.For () .Instance (myT));' stattdessen. – eouw0o83hf

+0

Sind diese Komponententests bei Ihnen oder stammen sie aus einer Burg-Dokumentation? Wenn es mehr davon geben könnte, könnten Sie einen Link bereitstellen? – joniba

Verwandte Themen