1

I have a class which needs a string as a parameter in its constructor but this parameter will be decided by the calling code. At the same point of time, the life time of this class has to be tied to per HTTP request. So, I created a custom PerWebRequestTimelineManager and used that for my target type in the config file. But since the string in the constructor has to be dynamically determined, I cannot use the ConstructorInjection via the config file. I can use an abstract factory to solve the problem of dynamic dependency, but I am not sure about the implementation: Can you check the code below and validate the approach. Specifically the RegisterType and Resolve calls seem a bit out of place though the successive Resolve calls across the application will be able to retrieve the same instance.:

public class PerformanceTracerFactory : IPerformanceTracerFactory
{
    private readonly IPerformanceTracer tracer;

    public IPerformanceTracer CreateInstance(string operationTitle)
    {
        _container.RegisterType<IPerformanceTracer, PerformanceTracer>(new InjectionConstructor(operationTitle));

        return _container.Resolve<IPerformanceTracer>();
    }
}

Relevant portion of config file:

<register type="IPerformanceTracer" mapTo="PerformanceTracer">
  <lifetime type="PerWebRequest"/>
</register>
<register type="IPerformanceTracerFactory" mapTo="PerformanceTracerFactory"/>

I have another question. In case if the above way of configuring and injecting the dependency using code is correct, then I think I do not need the config entries. I can always use the suitable overload to push the custom lifetime manager. In case, I would want to achieve the same thing using only config file, then how do I code the solution?

1 Answer 1

1

If you use a container-based factory you don't have to register/resolve your IPerformanceTracer in each call.

Register the mapping IPerformanceTracer --> PerformanceTracer once in your config file and use a ParameterOverride when you resolve your interface.

public IPerformanceTracer CreateInstance(string operationTitle)
{
    return _container.Resolve<IPerformanceTracer>(new ParameterOverride("nameOfTheParameterInTheConstructorOfPerformanceTracer", operationTitle);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for your response Sebastian. It perfectly solved the problem. I have another favor to ask in order to avoid similar issues due to ignorance about the full API. What is the best place for referring to Unity API Documentation?
@user608100 I'm sorry to disappoint you but the Unity documentation in general is dreadful. Some information can be found on MSDN. But you have to figure out most of that stuff on your own. I am currently working on two articles that will be put on the Unity site on CodePlex that shed some light on Unity's internals. But they will take some more time to translate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.