Note that there are some explanatory texts on larger screens.

plurals
  1. POSpecify run time parameter dependency in Unity
    primarykey
    data
    text
    <p>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.:</p> <pre><code>public class PerformanceTracerFactory : IPerformanceTracerFactory { private readonly IPerformanceTracer tracer; public IPerformanceTracer CreateInstance(string operationTitle) { _container.RegisterType&lt;IPerformanceTracer, PerformanceTracer&gt;(new InjectionConstructor(operationTitle)); return _container.Resolve&lt;IPerformanceTracer&gt;(); } } </code></pre> <p>Relevant portion of config file:</p> <pre><code>&lt;register type="IPerformanceTracer" mapTo="PerformanceTracer"&gt; &lt;lifetime type="PerWebRequest"/&gt; &lt;/register&gt; &lt;register type="IPerformanceTracerFactory" mapTo="PerformanceTracerFactory"/&gt; </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload