Search code examples
inversion-of-controlcastle-windsortyped-factory-facility

Different parameters type for typed factory in Castle Windsor


Given an interface like this:

public interface IFooFactory
{
  IFoo Create();
  void Release(IBar bar);
}

and assuming that the IFoo must implement IBar, will Castle Windsor know what to do with the object or are we creating any issues where the object types isn't exactly the same? The documentation did not say one way or other whether this is supported.


Solution

  • Yes it will. The declared type on the releasing method doesn't matter to Windsor. So all following methods will have the same behaviour:

    void Release(IBar obj)
    void ReleaseMe(IFoo obj)
    void Exterminate(Object obj)
    

    Having different types in your create and release methods may be a bit confusing to whoever will end up using your IFooFactory though.