开发者

nhibernate one-to-many collection- retrieve only the superclass

开发者 https://www.devze.com 2023-01-30 16:44 出处:网络
I have the following classes: class Person { public string Name { get; set; } } class Employee : Person { public int Salary { get; set; }

I have the following classes:

class Person
{
    public string Name { get; set; }
}

class Employee : Person
{
    public int Salary { get; set; }
}

class Company
{
    public开发者_开发问答 IList<Person> PeopleWhoAreNotEmployees { get; set; }
}

Person and Employee are mapped using table-per-class-heirarchy strategy.

When I retrieve the PeopleWhoAreNotEmployees collection, I want it only to contain elements that are Person, and NOT Employees.

How can I (fluently) configure the collection to only retrieve elements of the super class?

I think it's something to do with the Polymorphism property, but I couldn't really figure out how to do that.

thanks,

Jhonny

EDIT:

following the discussion with Jamie, I feel I need to clarify that the case here isn't really Person and Employee, but more like Employee and HistoricalEmployee.

Meaning- when an employee 'dies', they're not really deleted,

but they become HistoricalEmployee (with a few more attributes, such as termination date etc.).

Obviously, over time, the number of HistoricalEmployees will exceed the number of Employees by magnitudes,

so I can't fetch all HistoricalEmployees when I only need current Employees.

Sorry for the ambigiuity of the original question...

J

P.S.

I didn't change the original question since it would make the answer irrelevant.

a new version of this question is available here


I don't think you can, but that's not how I would approach it anyway. I would use a private field for the collection and expose methods that filter the list. This is much easier to map and work with and performance will be fine if the collection is reasonably sized (I don't know what's reasonable but I wouldn't worry about it if it's < 1000). It would make it easier if you had an abstract PersonBase class that both Person and Employee extend so that you don't have to deal with uncertainty that a Person might be an Employee.

public class Company
{
    private IList<Person> _allPeople;

    public IEnumerable<Employee> Employees()
    {
        return _allPeople.OfType<Employee>();
    }

    public IEnumerable<Person> PeopleWhoAreNotEmployees()
    {
        return _allPeople.Where(x => !(x is Employee));
    }
}

EDIT:

In response to your comment, the filtering would take place in the class. The private collection would cause all the People to be loaded, and the properties would dynamically filter that collection. The mapping wold look like:

public class CompanyMap : ClassMap<Company>
{
    public CompanyMap ()
    {
        // only collection is shown w/o cascade option
        HasManyToMany(x => x.Person).Access.CamelCaseField(Prefix.Underscore);
    }
}


what I ended up doing was using a 'where' clause on my property. the fluent configuration looks like so:

 mapping.HasMany(x => x.Employees)
            .Where("IsFired = 0")
0

精彩评论

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