Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing iBATIS.NET with generic custom collection interfaces and Unity
    text
    copied!<p>I'm trying to use a generic custom collection interface (to support injection with Microsoft Patterns and Practices Unity) in a class O/R mapped with iBATIS.NET. Does anyone know if this is possible and if so how to do it?</p> <p>I have an IDataItemCollection&lt;T&gt; interface that I map to SqlDataItemCollection&lt;T&gt; which extends CollectionBase. I want to use the IDataItemCollection&lt;T&gt; in my classes so I can swap SqlDataItemCollection&lt;T&gt; with other classes that extend the interface through Unity. The iBATIS.NET mapping file can reference the concrete class directly as there won't be one without the other.</p> <p>Below I have included a very simplified example of the code, database and mappings. I'm completely new to iBATIS.NET and really just want to prove its use at the moment, so please re-jig the mapping XML as necessary.</p> <p>Many thanks,</p> <p>Paul</p> <hr> <p>C# Code</p> <pre><code>public interface IDataItem { object Id { get; set; } } public class DataItem : IDataItem { public object Id { get; set; } } public interface IDataItemCollection&lt;T&gt; : ICollection where T : IDataItem { // Various Getters and Setters ... } public class SqlDataItemCollection&lt;T&gt; : CollectionBase, IDataItemCollection&lt;T&gt; where T : DataItem { public SqlDataItemCollection() { } public SqlDataItemCollection(T injType) { } // Getters and Setters to implement interfaces ... } public class Foo : DataItem { public Foo(IDataItemCollection&lt;Bar&gt; bars) { Bars = bars; } public IDataItemCollection&lt;Bar&gt; Bars { get; set; } } public class Bar : DataItem { } </code></pre> <p>SQL Server 2005 Database</p> <pre><code>CREATE TABLE Foo ( Id bigint IDENTITY(1,1) ) CREATE TABLE Bar ( Id bigint IDENTITY(1,1) ) CREATE TABLE FooBar ( FooId bigint, BarId bigint ) </code></pre> <p>iBATIS.NET mapping.xml</p> <pre><code>&lt;resultMaps&gt; &lt;resultMap id="FooResult" class="Foo"&gt; &lt;result property="Id" column="Id"/&gt; &lt;result property="Bars" column="Id" select="SelectBarsInFoo" lazyLoad="false"/&gt; &lt;/resultMap&gt; &lt;resultMap id="BarResult" class="Bar"&gt; &lt;result property="Id" column="Id"/&gt; &lt;/resultMap&gt; &lt;/resultMaps&gt; &lt;statements&gt; &lt;select id="SelectFoo" resultMap="FooResult"&gt; SELECT Id FROM Foo &lt;/select&gt; &lt;select id="SelectBarsInFoo" parameterClass="long" resultMap="BarResult" listClass="SqlDataItemCollection`1[Bar]" &gt; SELECT Bar.Id FROM Bar JOIN FooBar ON Bar.Id = FooBar.BarId WHERE FooBar.FooId = #value# &lt;/select&gt; &lt;/statements&gt; </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