FluentNHibernate - 映射同级对象的集合,其中一个同级对象持有另一个同级对象的集合

发布于 2024-11-25 01:50:54 字数 1948 浏览 2 评论 0原文

我有一个像这样的对象集合

public class ApplicationConfiguration
{
    public virtual long ApplicationConfigurationId { get; set; }
    public virtual Site Site { get; set; }

    public virtual ApplicationParameters ApplicationParameters { get; set; }
    public virtual IList<ApplicationParameters> ApplicationParametersHistory { get; set; }
}

public class ApplicationParameters
{
    public virtual int ApplicationParametersId { get; set; }
    public virtual Site Site { get; set; }
    public virtual int Status { get; set; }
}

public class Site
{
    public virtual int SiteId { get; set; }
}

我希望我的模型工作的方式是它们全部由我的站点对象绑定在一起。我的业务逻辑确保一个 ApplicationParameters 处于“活动”状态,其余部分保存在 History 集合中。

当我用 Fluent 将它们映射到一起时,我的问题就出现了。我有这样的东西

public class ApplicationConfigurationMap : ClassMap<ApplicationConfiguration>
{
    public ApplicationConfigurationMap()
    {
        Table("MerchantApplicationConfigurations");

        Id(x => x.ApplicationConfigurationId, "MerchantApplicationConfigurationId").GeneratedBy.Identity();

        References<Site>(x => x.Site, "SiteId");

        References<ApplicationParameters>(x => x.ApplicationParameters, "ApplicationParametersId");
    }
}

但是,如何映射我的集合以获取状态为 4 且兄弟姐妹具有相同 SiteId 的所有 ApplicationParameters ?我知道我可以使用 .Where("Status = 4") 但其余的映射我不确定。

编辑

我需要澄清一下我的设计。

站点是遗留系统中使用的外部对象,因此我无法更改它。创建这些应用程序(纸质应用程序,而不是软件应用程序)时,我必须保留所有更改的完整历史记录。我将应用程序分成了一堆不同的部分,这个配置对象是我们配置的应用程序方面的基础(定价、费用等)。

该系统的工作方式是每个部分都可以编辑,当编辑时,我创建一个具有活动状态的新记录,然后将具有非历史状态的前一个项目设置为历史记录。这样,每个站点在给定时间只有一条记录处于活动状态。

我按站点拉回数据,因为我始终可以选择该站点的活动组件。如果我将我的应用程序片段绑定到 ApplicationConfiguration ,则意味着基础对象无法再遵循我的历史保存模型。我还想避免单独拉回所有部分并构建 DTO。

我不想将所有内容附加到 Site 对象,因为它已在共享此代码库的其他应用程序中使用。该数据模型仅在管理应用程序中使用,因此我希望将其与在管理和客户端应用程序中使用的 Site 对象分开。

I have a collection of objects like this

public class ApplicationConfiguration
{
    public virtual long ApplicationConfigurationId { get; set; }
    public virtual Site Site { get; set; }

    public virtual ApplicationParameters ApplicationParameters { get; set; }
    public virtual IList<ApplicationParameters> ApplicationParametersHistory { get; set; }
}

public class ApplicationParameters
{
    public virtual int ApplicationParametersId { get; set; }
    public virtual Site Site { get; set; }
    public virtual int Status { get; set; }
}

public class Site
{
    public virtual int SiteId { get; set; }
}

The way I want my model to work is that they are all bound together by my site object. My business logic ensures that one ApplicationParameters is "active" and the rest are held within the History collection.

My problem comes when mapping these together with Fluent. I have something like this

public class ApplicationConfigurationMap : ClassMap<ApplicationConfiguration>
{
    public ApplicationConfigurationMap()
    {
        Table("MerchantApplicationConfigurations");

        Id(x => x.ApplicationConfigurationId, "MerchantApplicationConfigurationId").GeneratedBy.Identity();

        References<Site>(x => x.Site, "SiteId");

        References<ApplicationParameters>(x => x.ApplicationParameters, "ApplicationParametersId");
    }
}

But, how do I map my collection to pick up all the ApplicationParameters with Status 4 where the siblings have the same SiteId? I know I can use .Where("Status = 4") but the rest of the mapping I'm not sure about.

EDIT

I need to clarify my design a bit.

A site is an external object used in legacy systems, so I cant alter it. When creating these applications (a paper application, not a software application), I have to retain a complete history of all changes. I broke the application out into a bunch of different pieces, and this configuration object is the base for the side of the application that we configure (pricing, fees, etc).

The way this system is going to work is that every part can be edited, and when it is, I create a new record with an active status and then the previous item(s) with a non-historical status are set to historical. This way, only one record for each site is active at a given time.

I am pulling my data back by site, because I can always select the active components for that site. If I bind my application pieces to the ApplicationConfiguration it means the base object can no longer follow my historical preservation model. I also want to avoid pulling back all the pieces separately and building a DTO.

I didn't want to attach everything to the Site object because it's used in other applications that share this codebase. This data model is used only within an administration application, so I wanted to keep it separate from the Site object, which is used in the admin and client applications.

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

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

发布评论

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

评论(2

白日梦 2024-12-02 01:50:54

在 NHibernate 中,当映射实体集合时,您必须在集合表中提供一个键列来引用回父表。 .Where("") 只是在此之上提供额外的过滤。

在您的情况下,如果 ApplicationParameters 列表属于 ApplicationConfiguration 那么它应该有 ApplicationConfigurationId 列。

如果您需要通过 SiteId 绑定对象,那么您应该将 ApplicationParametersHistory 移动到 Site 实体。

现在,您的模型在您的类和数据库中以不同的方式表示。因此,如果您能够稍微重新组织您的模型,那么您可以使用标准的映射方式。

In NHibernate, when mapping a collection of entities, you have to provide a key column in the collection table that references back to the parent table. .Where("") just provides additional filtering on top of that.

In your case, if a list of ApplicationParameters belongs to ApplicationConfiguration then it should have ApplicationConfigurationId column.

If you need to tie objects through SiteId then you should move ApplicationParametersHistory to the Site entity.

Now your model is represented differently in your classes and in your DB. So, if you are able to reorganize your model a bit then you can use the standard way of mapping.

风筝有风,海豚有海 2024-12-02 01:50:54

您可以使用 HasMany 方法来映射您的集合。

HasMany(x => x.ApplicationParametersHistory).Where("Status = 4");

这是您想要的吗?

You could use the HasMany method to map your collection..

HasMany(x => x.ApplicationParametersHistory).Where("Status = 4");

Is this what you're after?

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