Search code examples
c#.netdependency-injectioncastle-windsor

passing parameters to Castle Windsor Typed Factory


I am following this document to use typed factory and pass parameters into constructor. The typed factory is giving me this error when I try to pass 2 parameters (1,"fo") as shown in the code.

enter image description here

public class SomeClass {
    public ITypedFactory2 F2 { get; set; } 
    public void SomeFunction() {
        var req = F2.Create<IGetFooRequest>(1, "fo"); // ERROR HERE
    }
}
public class GetFooRequest : IGetFooRequest {
    public int Bar { get; private set; }
    public string Ton { get; private set; }
    public GetFooRequest(int bar, string ton ) {
        Bar = bar;
        Ton = ton;
    }
}
public interface IGetFooRequest{
    int Bar { get; }
    string Ton { get; }
}    
public interface ITypedFactory2 {
    T Create<T>(int param1, string param2);
    void Release(object t);
}

and this is the windsor installer part...

container.AddFacility<TypedFactoryFacility>();
container.Register(Component.For<ITypedFactory2>().AsFactory());
container.Register(AllTypes
            .FromAssemblyContaining<IGetFooRequest>()
            .Where(type => type.Name.EndsWith("Request"))
            .WithService.AllInterfaces().LifestyleTransient());

Why it says could not resolve non-optional dependency...? I have passed in (1,"fo"); I really don't understand why this is happening... Please help.


Solution

  • I had the same question and just figured out the answer. The parameters' names of your factory method and those of your class constructor must match, case insensitive.

    so change your factory interface to

    public interface ITypedFactory2 {
        T Create<T>(int **bar**, string **ton**);
        void Release(object t);
    }
    

    or your class to

    public class GetFooRequest : IGetFooRequest {
        public int Bar { get; private set; }
        public string Ton { get; private set; }
        public GetFooRequest(int **param1**, string **param2**) {
            Bar = bar;
            Ton = ton;
        }
    }