将 iBATIS.NET 与通用自定义集合接口和 Unity 结合使用
我正在尝试在使用 iBATIS.NET 映射的类 O/R 中使用通用自定义集合接口(以支持使用 Microsoft 模式和实践 Unity 进行注入)。 有谁知道这是否可能,如果可以的话该怎么做?
我有一个 IDataItemCollection
下面我提供了一个非常简化的代码、数据库和映射示例。 我对 iBATIS.NET 完全陌生,目前只想证明它的用途,因此请根据需要重新调整映射 XML。
非常感谢,
Paul
C# 代码
public interface IDataItem
{
object Id { get; set; }
}
public class DataItem : IDataItem
{
public object Id { get; set; }
}
public interface IDataItemCollection<T> : ICollection where T : IDataItem
{
// Various Getters and Setters
...
}
public class SqlDataItemCollection<T> : CollectionBase, IDataItemCollection<T> where T : DataItem
{
public SqlDataItemCollection() { }
public SqlDataItemCollection(T injType) { }
// Getters and Setters to implement interfaces
...
}
public class Foo : DataItem
{
public Foo(IDataItemCollection<Bar> bars)
{
Bars = bars;
}
public IDataItemCollection<Bar> Bars { get; set; }
}
public class Bar : DataItem { }
SQL Server 2005 数据库
CREATE TABLE Foo
(
Id bigint IDENTITY(1,1)
)
CREATE TABLE Bar
(
Id bigint IDENTITY(1,1)
)
CREATE TABLE FooBar
(
FooId bigint,
BarId bigint
)
iBATIS.NET 映射.xml
<resultMaps>
<resultMap id="FooResult" class="Foo">
<result property="Id" column="Id"/>
<result property="Bars" column="Id" select="SelectBarsInFoo" lazyLoad="false"/>
</resultMap>
<resultMap id="BarResult" class="Bar">
<result property="Id" column="Id"/>
</resultMap>
</resultMaps>
<statements>
<select id="SelectFoo" resultMap="FooResult">
SELECT Id
FROM Foo
</select>
<select id="SelectBarsInFoo" parameterClass="long" resultMap="BarResult" listClass="SqlDataItemCollection`1[Bar]" >
SELECT Bar.Id
FROM Bar
JOIN FooBar ON Bar.Id = FooBar.BarId
WHERE FooBar.FooId = #value#
</select>
</statements>
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?
I have an IDataItemCollection<T> interface that I map to SqlDataItemCollection<T> which extends CollectionBase. I want to use the IDataItemCollection<T> in my classes so I can swap SqlDataItemCollection<T> 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.
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.
Many thanks,
Paul
C# Code
public interface IDataItem
{
object Id { get; set; }
}
public class DataItem : IDataItem
{
public object Id { get; set; }
}
public interface IDataItemCollection<T> : ICollection where T : IDataItem
{
// Various Getters and Setters
...
}
public class SqlDataItemCollection<T> : CollectionBase, IDataItemCollection<T> where T : DataItem
{
public SqlDataItemCollection() { }
public SqlDataItemCollection(T injType) { }
// Getters and Setters to implement interfaces
...
}
public class Foo : DataItem
{
public Foo(IDataItemCollection<Bar> bars)
{
Bars = bars;
}
public IDataItemCollection<Bar> Bars { get; set; }
}
public class Bar : DataItem { }
SQL Server 2005 Database
CREATE TABLE Foo
(
Id bigint IDENTITY(1,1)
)
CREATE TABLE Bar
(
Id bigint IDENTITY(1,1)
)
CREATE TABLE FooBar
(
FooId bigint,
BarId bigint
)
iBATIS.NET mapping.xml
<resultMaps>
<resultMap id="FooResult" class="Foo">
<result property="Id" column="Id"/>
<result property="Bars" column="Id" select="SelectBarsInFoo" lazyLoad="false"/>
</resultMap>
<resultMap id="BarResult" class="Bar">
<result property="Id" column="Id"/>
</resultMap>
</resultMaps>
<statements>
<select id="SelectFoo" resultMap="FooResult">
SELECT Id
FROM Foo
</select>
<select id="SelectBarsInFoo" parameterClass="long" resultMap="BarResult" listClass="SqlDataItemCollection`1[Bar]" >
SELECT Bar.Id
FROM Bar
JOIN FooBar ON Bar.Id = FooBar.BarId
WHERE FooBar.FooId = #value#
</select>
</statements>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在将应用程序的一部分重构回接口后,我遇到了同样的问题。 我通过显式实现集合的接口定义然后将实现复制为其具体类来解决这个问题。 这可能无法解决您的问题。
这允许 iBATIS.NET 将项目添加到集合中,而不会遇到类型转换错误,而显式接口实现允许我在整个应用程序中使用
IConcrete
,而无需引用实际的Concrete< /code> 或实际的
GroupCollection
。I just had this same problem after refactoring a portion of my application back to interfaces. I got around it by explicitly implementing the interface's definition of my collection and then duplicating the implementation as its concrete class. This may not solve your problem.
This allows iBATIS.NET to add items to the collection without running into a type conversion error while the explicit interface implementation allows me to use the
IConcrete
s throughout my app without referring to the actualConcrete
or actualGroupCollection
.