流畅的 NHibernate 自动映射

发布于 2024-12-03 16:09:18 字数 359 浏览 3 评论 0原文

我只是想知道是否可以使用 Fluent NHibernate 自动映射 .Net TcpClient 对象?

我有一个类,它有一个我想映射的 TcpClient 属性。

我尝试创建一个继承自 TcpClient 的自定义类(称为 tTcpClient),并添加带有 getter/setter 的 Id 属性;然而,它仍在寻找基类的 Id 字段。

如果可能的话,任何人都有任何想法,或者我是否需要为 TcpClient 创建自己的 xml 映射?

我有点希望能够保存该对象,以便在重新加载应用程序时轻松地重新创建它,并将 TcpClient 对象的属性绑定到 PropertiesGrid,并允许通过相当简单的配置。

谢谢。

I was just wandering if it would be possible to use the Fluent NHibernate to auto map a .Net TcpClient object?

I have a class that has a TcpClient property which I would like to map.

I tried creating a custom class inheriting from TcpClient called tTcpClient and adding an Id Property with a getter/setter; however, it was still looking for the Id field for the base class.

Anyone have any ideas if it is possible, or will I need to create my own xml mapping for the TcpClient?

I was sort of hoping to be able to save the object to easily recreate it on reloading the application and to bind the properties of the TcpClient object to the PropertiesGrid and allowing configuration through that rather easy.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

情场扛把子 2024-12-10 16:09:18

NHibernate 不知道如何处理像 TcpClient 这样开箱即用的复杂类型。但它允许您提供自己的加载和存储代码。您可以使用 IUserType:

public class TcpClientMapper : IUserType {

    public SqlType[] SqlTypes {
        get {
            return new[] { 
                           new SqlType(DbType.String), 
                           new SqlType(DbType.Int32) 
                         };
        }
    }

    public Object NullSafeGet(IDataReader rs, String[] names, ...) {

        String address = NHibernateUtil.String.NullSafeGet(rs, names[0]);
        Int32 port = NHibernateUtil.Int32.NullSafeGet(rs, names[1]);

        return new TcpClient(address, port);
    }

    public void NullSafeSet(IDbCommand cmd, Object value, Int32 index) {
        TcpClient tcpClient = value as TcpClient;
        if(tcpClient == null) {
            NHibernateUtil.String.NullSafeSet(cmd, null, index);
            NHibernateUtil.Int32.NullSafeSet(cmd, null, index + 1);
        } else {
            EndPoint red = tcpClient.Client.RemoteEndPoint;
            IPEndPoint endpoint = ((IPEndPoint)red);
            NHibernateUtil.String.Set(cmd, endpoint.Address.ToString(), index);
            NHibernateUtil.Int32.Set(cmd, endpoint.Port, index + 1);
        }
    }

    public Type ReturnedType {
        get { return typeof(TcpClient); }
    }

    // TODO: implement other methods
}

并在 hbm 中像这样映射它:

<property name="_tcpClient" type="MyNamespace.TcpClientMapper, MyAssembly">
    <column name="Address" />  <!-- NullSafeGet/Set index == 0 -->
    <column name="Port" />     <!-- NullSafeGet/Set index == 1 -->
</property>

或者使用流畅的 UserTypeConvention

public class TcpClientUserTypeConvention : UserTypeConvention<TcpClientMapper> {
}

NHibernate does not know how to deal with complex types like TcpClient out of the box. But it lets you provide your own loading and storing code. You can use IUserType:

public class TcpClientMapper : IUserType {

    public SqlType[] SqlTypes {
        get {
            return new[] { 
                           new SqlType(DbType.String), 
                           new SqlType(DbType.Int32) 
                         };
        }
    }

    public Object NullSafeGet(IDataReader rs, String[] names, ...) {

        String address = NHibernateUtil.String.NullSafeGet(rs, names[0]);
        Int32 port = NHibernateUtil.Int32.NullSafeGet(rs, names[1]);

        return new TcpClient(address, port);
    }

    public void NullSafeSet(IDbCommand cmd, Object value, Int32 index) {
        TcpClient tcpClient = value as TcpClient;
        if(tcpClient == null) {
            NHibernateUtil.String.NullSafeSet(cmd, null, index);
            NHibernateUtil.Int32.NullSafeSet(cmd, null, index + 1);
        } else {
            EndPoint red = tcpClient.Client.RemoteEndPoint;
            IPEndPoint endpoint = ((IPEndPoint)red);
            NHibernateUtil.String.Set(cmd, endpoint.Address.ToString(), index);
            NHibernateUtil.Int32.Set(cmd, endpoint.Port, index + 1);
        }
    }

    public Type ReturnedType {
        get { return typeof(TcpClient); }
    }

    // TODO: implement other methods
}

And map it like this in hbm:

<property name="_tcpClient" type="MyNamespace.TcpClientMapper, MyAssembly">
    <column name="Address" />  <!-- NullSafeGet/Set index == 0 -->
    <column name="Port" />     <!-- NullSafeGet/Set index == 1 -->
</property>

Or use fluent UserTypeConvention:

public class TcpClientUserTypeConvention : UserTypeConvention<TcpClientMapper> {
}
情深已缘浅 2024-12-10 16:09:18

内森,

你看过这个项目吗?

http://automapper.org/

干杯

Nathan,

Have you had a look at this project?

http://automapper.org/

Cheers

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