Schloss Windsor: Wie fügen Sie einen Anruf an eine Fabrikanlage nicht in xml?
-
09-06-2019 - |
Frage
Ich weiß, wie Schloss Windsor zu sagen, einen Verweis von einer Fabrik-Methode zu lösen mit Hilfe von XML, aber kann ich es programmatisch über die Container.AddComponent () Schnittstelle? Wenn nicht, ist es eine andere Möglichkeit, es aus dem Code zu tun?
EDIT: Es scheint eine gewisse Verwirrung, so lassen Sie mich klar zu sein, ich bin auf der Suche nach einer Möglichkeit, die folgenden in Code zu tun:
<facilities>
<facility
id="factory.support"
type="Castle.Facilities.FactorySupport.FactorySupportFacility, Castle.MicroKernel"
/>
</facilities>
<components>
<component
id="CustomerRepositoryFactory"
type="ConsoleApplication2.CustomerRepositoryFactory, ConsoleApplication2"
/>
<component
id="CustomerRepository"
service="ConsoleApplication2.ICustomerRepository, ConsoleApplication2"
type="ConsoleApplication2.CustomerRepository, ConsoleApplication2"
factoryId="CustomerRepositoryFactory"
factoryCreate="Create"
/>
</components>
( von diesem codebetter Artikel auf Werksunterstützung in windsor und spring.net )
Lösung
Direkt von der Einheit Test FactorySupportTestCase (die Ihre Freunde sind):
[Test]
public void FactorySupport_UsingProxiedFactory_WorksFine()
{
container.AddFacility("factories", new FactorySupportFacility());
container.AddComponent("standard.interceptor", typeof(StandardInterceptor));
container.AddComponent("factory", typeof(CalulcatorFactory));
AddComponent("calculator", typeof(ICalcService), typeof(CalculatorService), "Create");
ICalcService service = (ICalcService) container["calculator"];
Assert.IsNotNull(service);
}
private void AddComponent(string key, Type service, Type type, string factoryMethod)
{
MutableConfiguration config = new MutableConfiguration(key);
config.Attributes["factoryId"] = "factory";
config.Attributes["factoryCreate"] = factoryMethod;
container.Kernel.ConfigurationStore.AddComponentConfiguration(key, config);
container.Kernel.AddComponent(key, service, type);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow