I'd like extend Windsor's Typed Factory Facility to create a factory which retains some state, e.g. properties on the factory itself that will be used to resolve dependencies on the things the factory creates. Kind of like a Typed Builder Facility.
So, I would define my factory interface like so...
public interface IFishBuilder
{
string FishType { get; set; }
double SwimSpeed { get; set; }
IFish CreateFish(string name);
void ReleaseFish(IFish fish);
}
This would create components like this...
class Fish : IFish
{
public Fish(string name, string fishType, double swimSpeed)
{
}
}
And be used like so...
IFishBuilder b;
b.SwimSpeed = 50;
b.FishType = "Shark";
b.CreateFish("Frank");
b.CreateFish("Bob");
b.SwimSpeed = 95;
b.CreateFish("Usain");
Not a great example, but let's not worry too much about the use case, beyond understanding that the initialised factory will be passed to someone else that will do the creation, without understanding how it had been initialised.
I've made some steps in the right direction - well actually, it works OK already... However, I'm pretty sure there should be a simpler way to achieve this. What I have done so far is attach a second interceptor to the factory proxy, which intercepts the setter and getter properties on the interface. My original plan was that my custom ITypedFactoryComponentSelector for the factory would then read the properties from the interceptor attached to the proxy object.
Where this all falls down, of course, is that the ITypedFactoryComponentSelector interface doesn't give any way to access the proxy object that implements the factory upon which the factory method was called. This means I have no path back to calling the property getters to read the factories properties back in for the GetArguments to access. Let me try and explain that again. The component selector's function is defined as...
Func<IKernelInternal, IReleasePolicy, object> SelectComponent(MethodInfo method, Type type, object[] arguments);
You get access to the method on the factory class, the type and the arguments passed to the method, but not the factory object itself. Before you get clever with circular dependencies, my factories are transient.
My workaround for this currently is to access the Typed Factory's interceptor in my interceptor (via the proxy), and then grab hold of the component selector (which therefore needs to be made transient). I then poke the property values into a dictionary held by the component selector (effectively, using the component selector as the storage backing the factory's properties). However, this feels like a lot of hacky cross-coupling, which I would rather avoid.
So, now I am looking for suggestions as to how to improve this.
Thanks in advance!