Note that there are some explanatory texts on larger screens.

plurals
  1. PORepository pattern with generics and DI
    text
    copied!<p>I have a base repository contract which other contracts extend, like follows</p> <pre><code>public interface IBaseRepository&lt;T&gt; where T : class { IList&lt;T&gt; GetContents(); } </code></pre> <p>and then there are other contracts which extend it like follows</p> <pre><code>public interface IRepository1 : IBaseRepository&lt;MyClass1&gt; { } public interface IRepository2 : IBaseRepository&lt;MyClass2&gt; { } </code></pre> <p>I implement IRepository1 as follows</p> <pre><code>public class Repository1 : IRepository1 { public IList&lt;MyClass1&gt; GetContents() { //some code goes here } } </code></pre> <p>similarly for IRepository2 </p> <pre><code>public class Repository2 : IRepository2 { public IList&lt;MyClass2&gt; GetContents() { //some code goes here } } </code></pre> <p>Now i have a service Service1 which implments IService like follows </p> <pre><code>public class Service1 : IService { } </code></pre> <p>I want to use my base repository (IBaseRepository) here in my service constructor, get an instance of this base repository and use it like so </p> <pre><code> public class Service1 : IService { private IBaseRepository&lt;T&gt; _baseRepository; public Service1(IBaseRepository&lt;T&gt; baseRepository) { _baseRepository = baseRepository; } public MyMethod1() { var contentsOfType1 = _baseRepository&lt;MyClass1&gt;.GetContents(); } public MyMethod1() { var contentsOfType2 = _baseRepository&lt;MyClass2&gt;.GetContents(); } } </code></pre> <p>and this is what i am unable to do. </p> <p>So i have a generic base repository contract with type T and have other contracts (interfaces) extending the base contract and also specifying what type T will be. </p> <p>All these contracts (which extend generic base contract) have thier individual implementations. </p> <p>What i want to do is in my service class, instantiate this generic base contract, and use it to infer the extending types (and hence implementations) and use the method from the base repository.</p> <p>So if the base contract is <code>IBaseRepository&lt;T&gt;</code></p> <p>and extending contract is <code>IRepository1 : IBaseRepository&lt;MyClass1&gt;</code></p> <p>which is implemented by <code>Repository1 : IRepository1</code></p> <p>i want to use this in my service class like</p> <pre><code>public class service() { *private IBaseRepository&lt;T&gt; _repo; public service(IBaseRepository&lt;T&gt; repo) { *_repo = repo; } public void MyMethod() { *var x = _repo&lt;MyClass1&gt;.MethodFromIBaseRepository() } } </code></pre> <p>So its the *marked lines i want to achieve, which i am unable to. </p> <p>I am using castle windsor for DI. </p> <p>Thanks for your help guys</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