Resolve factory method as constructor parameter

23 views Asked by At

I have an instance constructor that takes a String as a runtime parameter, followed by several service parameters. I'd like to be able to specify the string at runtime and let DryIoc take care of the rest of the parameters

This answer shows how to do it for simple resolution, however, if the object with the factory method parameter is itself a constructor parameter, the resolution fails as DryIoc attempts to resolve the string.

public class BigBrother
{
    public BigBrother(Camera camera) {
        // Is watching you
    }
}

public class Camera
{
    public Camera(Func<String, Incident> incidentFactory)
    {
        var incident = incidentFactory("Something bad was seen");
    }
}

public class Incident
{
    private String _title;
    public Incident(String title, ICctvService cctvService, IUserService userService, ILoggingService logger)
    {
        _title = title;
        // Things happened
    }
}

Resolving BigBrother in the above code fails because the parameters of camera cannot be resolved.

Is there any way to achieve this?

0

There are 0 answers