Note that there are some explanatory texts on larger screens.

plurals
  1. POUnity's automatic abstract factory
    text
    copied!<p>I'm new to Unity and I'm having a hard time trying to figure out how to expand Unity's concept of Auto Factory. Unity provides out of box the ability to create factories using Func in the place of the parameter itself. Like that:</p> <pre><code>public class Foo { private readonly Func&lt;IBar&gt; _barFactory; public Foo(Func&lt;IBar&gt; barFactory) { _bar = barFactory; } } </code></pre> <p>The thing is, there are places where the class created by the factory needs some parameters and I'll only know those parameters in run time. Other than that, the number and type of those parameters varies from class to class.</p> <p>What I'm looking for is something similar to <a href="http://code.google.com/p/autofac/wiki/DelegateFactories" rel="nofollow">Autofac DelegateFacotry</a>, but I would like to keep the Unity's feeling. So, I want to make Unity to work like that:</p> <pre><code>public class Foo { private readonly Func&lt;int, string, IBar&gt; _barFactory; public Foo(Func&lt;int, string, IBar&gt; barFactory) { _bar = barFactory; } } </code></pre> <p>The above code does not work, because Unity does work with that constructor, but it is exactly what I'm looking for.</p> <p>I have tried use BuilderStrategy but it boils down to either Expression or IL generation. Before going down that path I would like to check for other options.</p> <p>Is any one experienced enough with Unity who can help me with that?</p> <p>Here are the constrains I have:</p> <ol> <li>Keep Unity's concept of using Func</li> <li>Do not need to register every single Func within the container</li> <li>The constructor should accept Func from Func to Func (Func is handled already)</li> <li>Changing the container is not an option</li> </ol> <p>I hope I was clear enough. If I was not, please let me know.</p> <p><strong>Edit 1</strong>: I understand I can use abstract factories directly. But, the first goal is to keep Unity's feeling.</p>
 

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