开发者

FluentNHibernate HasManyToMany Conditional Mappings

Is there a way that I filter out rows in a HasManyToMany mapping?

I have three tables (legacy, not able to change them) Service, Resource and ResourceService. The ResourceService allows multiple resources to link to multiple services but it also has an "Active" column.

On my Resource domain object I've mapped the services linked to the resource with a "ProvidedBy" property which returns an array of the services. The problem is that I only want rows from services that are marked as开发者_如何学运维 active.

Am I missing something basic here?


Fluent NHibernate release 1.0 did not support NHibernate filters or filter-defs. I submitted a patch to James Gregory and team a while back which they have now incorporated in the trunk, so if you get the trunk rather than the release version you'll find the ability to do this sort of thing included.

Essentially, you can set up a class that inherits from FilterDefinition like so:

public class TestFilter : FilterDefinition
{
    public TestFilter()
    {
        WithName("test")
            .WithCondition("Age > :age")
            .AddParameter("age", NHibernateUtil.Int32);
    }
}

and then apply this filter in your fluent mapping:

HasManyToMany(x => x.Oldies)
    .Table("People")
    .ApplyFilter<TestFilter>();

You can set the parameter value and enable the filter using the session object as normal:

session.EnableFilter("test").SetParameter("age", 65);


Wouldn't this be a candidate for a Where?

HasManyToMany(x => x.Whatevers)
  .Where(x => x.Active);


You can actually achieve outer join conditions using Filters.

See FluentNHibernate HasManyToMany Conditional Mappings

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜