将 iBATIS.NET 与通用自定义集合接口和 Unity 结合使用

发布于 2024-07-14 05:46:20 字数 2307 浏览 4 评论 0原文

我正在尝试在使用 iBATIS.NET 映射的类 O/R 中使用通用自定义集合接口(以支持使用 Microsoft 模式和实践 Unity 进行注入)。 有谁知道这是否可能,如果可以的话该怎么做?

我有一个 IDataItemCollection我映射到 SqlDataItemCollection的接口 它扩展了 CollectionBase。 我想使用 IDataItemCollection在我的课程中,这样我就可以交换 SqlDataItemCollection与通过 Unity 扩展接口的其他类一起使用。 iBATIS.NET 映射文件可以直接引用具体类,因为缺一不可。

下面我提供了一个非常简化的代码、数据库和映射示例。 我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小兔几 2024-07-21 05:46:20

在将应用程序的一部分重构回接口后,我遇到了同样的问题。 我通过显式实现集合的接口定义然后将实现复制为其具体类来解决这个问题。 这可能无法解决您的问题。

public interface IGroup { }
public class Group : IGroup { }
public class IGroupCollection : IList<IGroup> { }
public class GroupCollection : IGroupCollection { }

public interface IConcrete
{
    IGroupCollection Items { get; set; }
}

public class Concrete : IConcrete
{
    public GroupCollection Items { get; set; }
    IGroupCollection IConcrete.Items
    {
        get { return Items; }
        set { Items = value as GroupCollection; }
    }
}

这允许 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.

public interface IGroup { }
public class Group : IGroup { }
public class IGroupCollection : IList<IGroup> { }
public class GroupCollection : IGroupCollection { }

public interface IConcrete
{
    IGroupCollection Items { get; set; }
}

public class Concrete : IConcrete
{
    public GroupCollection Items { get; set; }
    IGroupCollection IConcrete.Items
    {
        get { return Items; }
        set { Items = value as GroupCollection; }
    }
}

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 IConcretes throughout my app without referring to the actual Concrete or actual GroupCollection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文