(流畅)NHibernate:将完整的表映射到一个对象

发布于 2024-12-01 04:11:02 字数 395 浏览 1 评论 0原文

我有以下数据库表:

Settings

Key            | Value
---------------|-------------------
SomeSetting    | Foo
AnotherSetting | Bar
...            | ...

并希望将其映射到以下对象:

public class Settings
{
      public virtual string SomeSetting { get; set; }

      public virtual string AnotherSetting { get; set; }
}

如何使用(流畅的)NHibernate 完成此操作?

I have the following database table:

Settings

Key            | Value
---------------|-------------------
SomeSetting    | Foo
AnotherSetting | Bar
...            | ...

And want to map this to the following object:

public class Settings
{
      public virtual string SomeSetting { get; set; }

      public virtual string AnotherSetting { get; set; }
}

How could I accomplish this using (Fluent) NHibernate?

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

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

发布评论

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

评论(2

难忘№最初的完美 2024-12-08 04:11:02

我会将键/值对映射到私有 IDictionary 并通过访问字典来公开属性。请参阅 Ayende 在地图上的博客条目了解如何创建字典。像这样的东西:

public class Settings
{
    private IDictionary<string, string> _dict;

    //initialize dictionary in constructor
    public Settings()
    {
         _dict = new Dictionary<string, string> { {"SomeSetting", string.Empty} };
    }

    public virtual string SomeSetting
    {
        get { return _dict["SomeSetting"]; }
        set { _dict["SomeSetting"] = value; }
    }

    // etc.

}

I would map the key/value pairs to a private IDictionary and expose the properties by accessing the dictionary. See Ayende's blog entry on map for creating the dictionary. Something like:

public class Settings
{
    private IDictionary<string, string> _dict;

    //initialize dictionary in constructor
    public Settings()
    {
         _dict = new Dictionary<string, string> { {"SomeSetting", string.Empty} };
    }

    public virtual string SomeSetting
    {
        get { return _dict["SomeSetting"]; }
        set { _dict["SomeSetting"] = value; }
    }

    // etc.

}
萌梦深 2024-12-08 04:11:02

替代方案是有两列:Key,Value

将它们映射到数据库中的列,现在编写一个带有属性的存储库类,负责在键上查询数据库并获取值。您的存储库模型可能类似于您当前的对象模型。

public class Settings
{
      private readonly ISession _session;
      public Settings(ISEssion session)
      {
      _session=session;
      }
      public Setting SomeSetting { get {return session.QueryOver<Setting>().SingleOrDefault(x.Key=="SomeSetting "} }

}

你怎么认为?

Well an alternate would be to have two columns: Key,Value

map them into a column in the database and now write a repository class with properties will be responsible for querying the database on the key and getting the value. your repository model might resemble your current object model.

public class Settings
{
      private readonly ISession _session;
      public Settings(ISEssion session)
      {
      _session=session;
      }
      public Setting SomeSetting { get {return session.QueryOver<Setting>().SingleOrDefault(x.Key=="SomeSetting "} }

}

what do you think?

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