I will give very simple example.
class Implementation: IMyInterface
{
string mArg;
public Implementation(string arg)
{
mArg = arg;
}
public DoStuff(object param)
{
// snip
}
}
class Decorator: IMyInterface
{
IMyInterface mWrapped;
public Decorator(IMyInterface wrapped)
{
mWrapped = wrapped;
}
public DoStuff(object param)
{
var result = mWrapped.DoStuff(param);
// snip
return result;
}
}
Now the argument I need for Implementation constructor I get from user in run-time.
IMyInterface GetMyObject()
{
string arg = AskUserForString();
return mContext.Resolve // HOW?
}
So what is the proper way to set this up and resolve to Decorated instance?
The example is simple. But imagine there are more layers (decorators/adapters) and the innermost implementation needs parameter I get in run-time.
I am assuming you are using autofac, as it is in the tags of the question.
You can try something along these lines:
And the registration/resolving part:
If you have multiple decorators, you can chain them in the order you want within the registration:
Here is a good article about decorator support in autofac as well.