如何在 N 层应用程序中使用 Fluent NHibernate?

发布于 2024-09-10 14:20:06 字数 2263 浏览 3 评论 0原文

我正在尝试在我的项目中采用 Fluent NHibernate,目前我可以从数据库获取数据,当我在应用程序服务器上时,数据包括其 PK,但是当我将此数据(作为列表)返回给客户端时,其所有 PK 都是松动的。

我该如何解决这个问题?

更新

我的 POCO 类如下: PK 是 CountryCd 和 CityCd

public class coCity
{
    public virtual string CountryCd { get; private set; }
    public virtual string CityCd { get; private set; }
    public virtual string CityNameTH { get; set; }
    public virtual string CityNameEN { get; set; }
    public virtual int DeliveryLeadTime { get; set; }
    public virtual string CreateBy { get; set; }
    public virtual DateTime CreateDate { get; set; }
    public virtual string UpdateBy { get; set; }
    public virtual DateTime UpdateDate { get; set; }

    public override bool Equals(object obj)
    {
        return this.GetHashCode().Equals(obj.GetHashCode());
    }

    public override int GetHashCode()
    {
        return (this.CountryCd + this.CityCd).GetHashCode();
    }
}

映射类:

public class coCityMap : ClassMap<coCity>
{
    public coCityMap()
    {
        Table("coCity"); // this is optional

        CompositeId()
            .KeyProperty(x => x.CountryCd)
            .KeyProperty(x => x.CityCd);
        Map(x => x.CityNameTH);
        Map(x => x.CityNameEN);
        Map(x => x.DeliveryLeadTime);
        Map(x => x.CreateBy);
        Map(x => x.CreateDate);
        Map(x => x.UpdateBy);
        Map(x => x.UpdateDate);
    }
}

在应用程序服务器获取数据的源代码

public List<coCity> GetTest()
{
    List<coCity> result = new List<coCity>();

    var sessionFactory = CreateSessionFactory();

    using (var session = sessionFactory.OpenSession())
    {
        result = (List<coCity>)session.CreateCriteria(typeof(coCity)).List<coCity>();
    }

    return result;
}

当其仍在应用程序服务器时,数据可以正确检索,如下图所示 替代文本 http://img138.imageshack.us/img138/1071/serverside.png< /a>

但是,当此数据传输回客户端时,其所有 PK 都松散,如下所示。 替代文本 http://img203.imageshack.us/img203/1664/clientside.png< /a>

I'm trying to adopt Fluent NHibernate with my project, currently I can get data from database, when I'm at application server, data is include its PK but when I return this data (as List) to client all of its PK is loose.

How can I fixed this problem?

Update

My POCO class is below: PKs are CountryCd and CityCd

public class coCity
{
    public virtual string CountryCd { get; private set; }
    public virtual string CityCd { get; private set; }
    public virtual string CityNameTH { get; set; }
    public virtual string CityNameEN { get; set; }
    public virtual int DeliveryLeadTime { get; set; }
    public virtual string CreateBy { get; set; }
    public virtual DateTime CreateDate { get; set; }
    public virtual string UpdateBy { get; set; }
    public virtual DateTime UpdateDate { get; set; }

    public override bool Equals(object obj)
    {
        return this.GetHashCode().Equals(obj.GetHashCode());
    }

    public override int GetHashCode()
    {
        return (this.CountryCd + this.CityCd).GetHashCode();
    }
}

Mapping class:

public class coCityMap : ClassMap<coCity>
{
    public coCityMap()
    {
        Table("coCity"); // this is optional

        CompositeId()
            .KeyProperty(x => x.CountryCd)
            .KeyProperty(x => x.CityCd);
        Map(x => x.CityNameTH);
        Map(x => x.CityNameEN);
        Map(x => x.DeliveryLeadTime);
        Map(x => x.CreateBy);
        Map(x => x.CreateDate);
        Map(x => x.UpdateBy);
        Map(x => x.UpdateDate);
    }
}

Source code to get data at application server

public List<coCity> GetTest()
{
    List<coCity> result = new List<coCity>();

    var sessionFactory = CreateSessionFactory();

    using (var session = sessionFactory.OpenSession())
    {
        result = (List<coCity>)session.CreateCriteria(typeof(coCity)).List<coCity>();
    }

    return result;
}

When its still at application server data is retrieve correctly as image below
alt text http://img138.imageshack.us/img138/1071/serverside.png

However when this data transit back to client side all of its PKs is loose like below.
alt text http://img203.imageshack.us/img203/1664/clientside.png

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

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

发布评论

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

评论(2

肩上的翅膀 2024-09-17 14:20:06

首先,这对于 Fluent NHibernate 来说不是问题,因此:

  1. 当您序列化 POCO 时,必须在 POCO 上使用 Serialized。
  2. (来自您的评论)NHibernate 将从数据库检索到的对象的引用保留到缓存(一级缓存)。当您序列化此“托管”对象时,序列化的输出是非托管对象。 Nhibernate 不会仅仅因为您在新构造的对象中设置值而检测到数据库中存在对象。您必须从数据库获取对象并更新其属性并调用 Update(),或者使用纯 sql 处理从客户端返回的对象(哎呀!)。

请注意,这与这个问题无关:您的 Equals() 实现非常糟糕,因为它没有考虑类型并且仅依赖于 GetHashCode 值。如果你的所有类都有这个实现,你可能会遇到麻烦。

First of all, this isn't a problem with Fluent NHibernate so:

  1. Serializable must be used on your POCO's when you serialize them.
  2. (from your comment) NHibernate keeps a reference of the object retrieved from the database to a cache (1-st level cache). While you serialize this 'managed' object the output of the serialization is an unmanaged object. Nhibernate does not detect that a an object exists in the db just because you set an value in a newly constructed object. You must get the object from the database and update its properties and call Update() or you work with pure sql with the object that returned from the client (yikes!).

Note that is irrelevant with this question: your Equals() implementation is really bad as it doesn't take into account types and depends only on GetHashCode value. If all your classes have this implementation you could run into trouble.

阳光的暖冬 2024-09-17 14:20:06

我认为问题在于 PK 属性上的私有设置器。尝试将其更改为公开。

无论哪种方式,都用 Serialized 标记您的实体

一些注释:

  • 使用 nhibernate 时的一般建议是避免复合 Id。在模型上创建一个作为身份列的代理 ID,并在其他地方强制 CityCd 和 CountryCd 的唯一性
  • 在客户端/服务器层传递数据时,请考虑使用 DTO 以避免一些常见的 LazyInitializationExceptions 问题。

I think the problem is with that private setter on the PK's properties. Try changing that to public.

Either way, mark your entity with Serializable

A few comments:

  • As a general recomendation when using nhibernate is to avoid composite Ids. Create on your model a surrogate Id that is an identity column and enforce uniqueness of CityCd and CountryCd somewhere else
  • When passing data around client/server tiers, consider using DTOs to avoid some commong LazyInitializationExceptions problems.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文