For me, one big benefit of IoC is deferring the selection of actual type that gets instantiated. Currently, the resolver has a default factory that creates an object that is an instance of the return type. This means that I cannot use virtual methods in the return type which pretty much limits my selection of type that actually gets instanced.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Doesn't have to. The default Factory is just the simplest approach. I'll try to put together an example that shows how you can use a derived type. You have to register your own factory to do it:
I've checked code into the simple example on the TRUNK that shows creating an instance of type Base that is backed by the subclass Derived.
class Base {
public:
virtual ~Base(){};
virtual void do_action();
};
class Derived : public Base {
public:
virtual ~Derived(){};
virtual void do_action();
};
Create a factory method….
Base* makeBase(Zone& zone){
return new Derived();
}
and register it:
supply<Base>::configure(makeBase);
Then use it:
Base * base = supply<Base>::fetch(request1);
base->do_action()
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
For me, one big benefit of IoC is deferring the selection of actual type that gets instantiated. Currently, the resolver has a default factory that creates an object that is an instance of the return type. This means that I cannot use virtual methods in the return type which pretty much limits my selection of type that actually gets instanced.
Doesn't have to. The default Factory is just the simplest approach. I'll try to put together an example that shows how you can use a derived type. You have to register your own factory to do it:
I've checked code into the simple example on the TRUNK that shows creating an instance of type Base that is backed by the subclass Derived.
class Base {
public:
virtual ~Base(){};
virtual void do_action();
};
class Derived : public Base {
public:
virtual ~Derived(){};
virtual void do_action();
};
Create a factory method….
Base* makeBase(Zone& zone){
return new Derived();
}
and register it:
supply<Base>::configure(makeBase);
Then use it:
Base * base = supply<Base>::fetch(request1);
base->do_action()