views:

88

answers:

1

Hi

I having some problems implementing IOC using windsor. I have several different implementations of a data access class and i want to use windsor to be able to specify which data access class to use when using a business object. See the code below

public interface IPersistable
{
    bool Save();
    bool Delete();
}

public class Address
{
 public Address()
 {
 }

    public Address(IPersistable Factory)
    {
        this.DataAccess = Factory;
    }

    private IPersistable _DataAccess;

    public IPersistable DataAccess
    {
        get { return _DataAccess; }
        set { _DataAccess = value; }
    }
}

public class AddressFactoryUsingSQL : IPersistable
{

    public bool Save()
    {
        throw new NotImplementedException();
    }

    public bool Delete()
    {
        throw new NotImplementedException();
    }
}

public class AddressFactoryUsingWebService : IPersistable
{
    public bool Save()
    {
        throw new NotImplementedException();
    }

    public bool Delete()
    {
        throw new NotImplementedException();
    }
}

public class AddressFactoryUsingRepository : IPersistable
{
    public bool Save()
    {
        throw new NotImplementedException();
    }

    public bool Delete()
    {
        throw new NotImplementedException();
    }
}

Add when the class is consumed im using the following: Address addr = new Address(new AddressFactoryUsingSQL);

I want to be able to use Windsor so i can write something similar to:

    IWindsorContainer _Container= new WindsorContainer();
    _Container.Register(Castle.MicroKernel.Registration.Component.For<IPersistable>()); //this would retrieve the IPersistable class i want to use for the Address business object
    IPersistable addrF = _Container.Resolve<IPersistable>();
    Address addr = new Address(addrF);

how do i achieve this? I get the error message," Type IPersistable is abstract. As such, it is not possible to instansiate it as implementation of IPersistable service"

I want to be able to change the data access method without having to change all of my code. Also, how can i use web.config to this?

Al

+1  A: 

The problem is that you forgot to mention your concrete type implementing IPersistable, folowing code should work:

IWindsorContainer container = new WindsorContainer();
container.Register(
   Component.For<IPersistable>()
     .ImplementedBy<AddressFactoryUsingSQL>());

And you can use it this way:

var persistable = container.Resolve<IPersistable>();
Restuta
I tried: IWindsorContainer container = new WindsorContainer(); container.Register(Castle.MicroKernel.Registration.Component.For<IPersistable>().ImplementedBy<AddressFactoryUsingSQL>()); IPersistable addrF = container.Resolve<IPersistable>; Address addr = new Address(addrF); i then get the error message," Cannot convert method group 'Resolve' to non-delegate type 'IPersistable'. Did you intend to invoke the method?"
Al P
`Resolve` is a method, not a property. Read the compiler error again.
Krzysztof Koźmic
Ok, thanks, How do i get this working from the web.config file?
Al P
Modified my answer to reflect usage scenario, what do you mean under "working from web.config"?
Restuta