开发者

FluentNHibernate - Map a collection of sibling objects where one sibling holds a collection of the other

开发者 https://www.devze.com 2023-03-21 09:08 出处:网络
I have a collection of objects like this public class ApplicationConfiguration { public virtual long ApplicationConfigurationId { get; set; }

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");

开发者_Python百科        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.


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.


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

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

Is this what you're after?

0

精彩评论

暂无评论...
验证码 换一张
取 消