having the class:
public class SomeClass{
public SomeClass(IBlabla bla, string name){
//....
}
}
I can write factory:
public class SomeClassFactory{
public SomeClassFactory(IBlabla bla){
//....
}
public SomeClass Create(string name){return new SomeClass(_bla, name)}
}
And live with it. But I wonder, is there any solution to do this without such obvious factory implementation, in the same way as .AsFactory() works, but with additional parameter?
Definitely possible, and you can even do it without an implementation through the typed factories in Castle. Here is how :
Given your base classes:
you can create a generic factory for elements with a
string named
in their constructor:No need for any implementation, you are going to use a tool that will create an automatic implementation of your factory backed up by Castle:
Why does it work? When going through a typed factory, Castle will try and hand the object being resolved all the parameters that are passed to the factory call. So the
named
parameter is given to the constructor of yourSomeClass
. TheIBlabla
is resolved through standard behavior for Castle.There you are, factory without any implementation are neat :D