0

我已经使用NHibernate很长一段时间了,但仍然很难做一些“简单”的事情。我正在尝试处理many-to-many我的实体ServiceProviderFeatures.

基本上每个人都SERVICE_PROVIDERS可以有不同的特征,这些特征必须存在于我的桌子上FEATURES

这些是我的映射文件:

ServiceProviders.hbm.xml,

<class name="App.Domain.ServiceProvider, App.Domain" table="ServiceProviders">
    <id name="Code" type="System.Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
        <column name="ServiceProviderCode" />
        <generator class="guid.comb" />
    </id>
    <property name="Description" type="AnsiString">
        <column name="Description" length="150" not-null="true" />
    </property> 
    <set name="Features" table="ServiceProvidersFeatures" access="field.pascalcase-underscore" cascade="save-update" optimistic-lock="false">
        <key column="ServiceProviderCode"></key>
        <many-to-many class="App.Domain.Feature" column="FeatureCode" not-found="exception" />
    </set>
</class>

Features,

<class name="App.Domain.Feature, App.Domain" table="Features">
    <id name="Code" type="System.Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
        <column name="FeatureCode" />
        <generator class="guid.comb" />
    </id>
    <property name="Description" type="AnsiString">
        <column name="Description" length="150" not-null="true" />
    </property>
    <set name="ServiceProviders" table="ServiceProvidersFeatures" cascade="none" inverse="true" lazy="true" access="field.pascalcase-underscore" optimistic-lock="false" mutable="false">
        <key column="FeatureCode"></key>
        <many-to-many class="App.Domain.ServiceProvider" column="ServiceProviderCode" not-found="ignore" />
    </set>
</class>

这些是两个主要类:

ServiceProvider.cs

public class ServiceProvider
{
    public ServiceProvider()
    {
        this._Features = new HashSet<Feature>();
    }

    public virtual Guid Code { get; protected set; }

    public virtual string Description { get; set; }

    private ICollection<Feature> _Features = null;

    public virtual ReadOnlyCollection<Feature> Features
    {
        get { return (new List<Feature>(_Features).AsReadOnly()); }
    }   
}

Feature.cs

public class Feature
{
    public Feature()
    {
        this._ServiceProviders = new HashSet<ServiceProvider>();
    }

    public virtual Guid Code { get; protected set; }

    public virtual string Description { get; set; }

    private ICollection<ServiceProvider> _ServiceProviders = null;

    public virtual ReadOnlyCollection<ServiceProvider> ServiceProviders
    {
        get { return (new List<ServiceProvider>(_ServiceProviders).AsReadOnly()); }
    }
}

我要做的是获取描述以某个字符串开头的所有服务提供者(具有所有功能),并且它们至少指定了一个功能(参数)。

我认为我需要一个子查询,但我不知道如何构建 QueryOver。它应该是这样的:

var serviceProviders = 
    session.QueryOver<App.Domain.ServiceProvider>()
           .Inner.JoinAlias(x => x.Features, () => features)
           .WhereRestrictionOn(f => f.Description).IsLike("%" + "test" + "%")
           .AndRestrictionOn(() => features.Code).IsIn(<SubQuery>)
           .List();
4

2 回答 2

1

我想出了一个扩展方法,允许我构建表达式:

public static class ServiceProviderSearch
{
    public static IQueryOver<App.Domain.ServiceProvider, App.Domain.ServiceProvider> AttachWhereForSearchText(this IQueryOver<App.Domain.ServiceProvider, App.Domain.ServiceProvider> mainQuery, string searchText)
    {
        if (!string.IsNullOrEmpty(searchText))
        {
        ICriterion filterSearchText = Expression.Disjunction()
            .Add(Restrictions.On<App.Domain.ServiceProvider>(f => f.Description).IsLike(searchText, MatchMode.Anywhere))
            .Add(Restrictions.On<App.Domain.ServiceProvider>(f => f.ExtendedDescription).IsLike(searchText, MatchMode.Anywhere));
        mainQuery.Where(filterSearchText);
        }
        return (mainQuery);
    }

    public static IQueryOver<App.Domain.ServiceProvider, App.Domain.ServiceProvider> AttachWhereForFeatures(this IQueryOver<App.Domain.ServiceProvider, App.Domain.ServiceProvider> mainQuery, App.Domain.ServiceProvider serviceProvider, App.Domain.Feature features, Guid[] listOfFeatures)
    {
        if ((listOfFeatures != null) && (listOfFeatures.Count() > 0))
        {
        mainQuery
            .Inner.JoinAlias(() => serviceProvider.Features, () => features);

        mainQuery.WithSubquery.WhereProperty(() => features.Code)
                .In(
                QueryOver.Of<App.Domain.Feature>()
                    .WhereRestrictionOn(f => f.Code)
                    .IsIn(listOfFeatures)
                    .Select(f => f.Code)
                    );
        }
        return (mainQuery);
    }
}

所以现在我可以写这样的东西:

App.Domain.ServiceProvider serviceProvider = null;
App.Domain.Feature features = null;
App.Domain.Language languages = null;
App.Domain.Service services = null;

Guid[] selectedFeatures = {};

var serviceProviders = Session.QueryOver(() => serviceProvider);    
serviceProviders
    .AttachWhereForSearchText(<searchText>)
    .AttachWhereForFeatures(serviceProvider, features, selectedFeatures);

Results = serviceProviders
    .TransformUsing(Transformers.DistinctRootEntity)
    .Take(<pageSize>)
    .Skip((<page> - 1) * <pageSize>)
    .ToList<App.Domain.ServiceProvider>();

灵感来自这个答案

于 2013-02-19T14:40:09.583 回答
0

我会在您的代码段中更改几件事。首先,您不需要%在您的IsLike方法中指定:NHibernate 会自动执行此操作。其次,您可以通过以下方式构造子查询:

var subquery = 
    session.QueryOver<App.Domain.Feature>()
           .WhereRestrictionOn(f => f.Description).IsLike("test", MatchMode.Anywhere)
           .Select(f => f.Code);

您可以将其插入主查询中:

var serviceProviders = 
    session.QueryOver<App.Domain.ServiceProvider>()
           .WithSubquery.WhereProperty(s => s.Code).In(subquery)
           .List();

或者你甚至可以尝试:

var serviceProviders = 
    session.QueryOver<App.Domain.ServiceProvider>()
           .JoinQueryOver<App.Domain.Feature>(s => s.Features)
             .WhereRestrictionOn(f => f.Description).IsLike("test", MatchMode.Anywhere)
           .List<App.Domain.ServiceProvider>();

根据您的lazy设置,您可以使用以下方法初始化Features集合,

serviceProviders.ToList()
                .ForEach(service => NHibernateUtil.Initialize(service.Features));
于 2013-02-19T04:41:27.480 回答