Domanda

So come dire a Castle Windsor di risolvere un riferimento dal metodo di una factory utilizzando XML, ma posso farlo a livello di codice tramite l'interfaccia Container.AddComponent()?In caso contrario, esiste un altro modo per farlo dal codice?

MODIFICARE:Sembra che ci sia un po' di confusione quindi lasciatemi chiarire, sto cercando un modo per eseguire quanto segue nel codice:

<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>

(da questo articolo di codebetter sul supporto di fabbrica in Windsor e spring.net)

È stato utile?

Soluzione

Direttamente dallo Unit Test FactorySupportTestCase (che sono i tuoi amici):

[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);
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top