Note that there are some explanatory texts on larger screens.

plurals
  1. POCastle Windsor fluent registration : AllTypes and type forwarding
    text
    copied!<p>Given the following classes/interfaces :</p> <pre><code>class Repository&lt;T&gt; : IRepository&lt;T&gt; {} class UserRepository : Repository&lt;User&gt;, IUserRepository {} </code></pre> <p>Is there a simple way to transform this :</p> <pre><code>container.Register( Component.For&lt;IUserRepository&gt;() .Forward&lt;IRepository&lt;User&gt;&gt;() .ImplementedBy&lt;UserRepository&gt;() ); </code></pre> <p>Into an automatic registration through AllTypes ? So far I'm missing type forwarding.</p> <pre><code>container.Register( AllTypes.FromAssemblyContaining(typeof(IUserRepository)) .BasedOn(typeof(Repository&lt;&gt;)) .WithService.Select((t, b) =&gt; new List&lt;Type&gt; { t.GetInterfaces().Where( x =&gt; !x.IsGenericType ).FirstOrDefault() }) ); </code></pre> <p>But with the type forwarding ? So this will work :</p> <pre><code>container.Resolve&lt;IUserRepository&gt;().GetType() == container.Resolve&lt;IRepository&lt;User&gt;&gt;().GetType() </code></pre> <p>The following code doesn't quite work, as FirstInterface() returns the IRepository&lt;User> :</p> <pre><code>container.Register( AllTypes.FromAssemblyContaining(typeof(IUserRepository)) .BasedOn(typeof(Repository&lt;&gt;)) .WithService.FirstInterface() ); </code></pre> <p>Edit : I was pretty close, so in my case :</p> <pre><code>container.Register( AllTypes.FromAssemblyContaining(typeof(IUserRepository)) .BasedOn(typeof(Repository&lt;&gt;)) .WithService.Select((t, b) =&gt; new List&lt;Type&gt; { t.GetInterfaces() }) ); </code></pre>
 

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