1

私は Fluent NHibernate の完全な初心者であり、推奨事項に基づいてクエリ オブジェクト パターンを使用しています。私も初めてです。コード サンプルを簡潔で役立つものにするように努めます。

ユーザークラス:

public class User {
    public Guid ID { get; set; }
    public string Name { get; set; }
}

視認性:

public enum VisibilityType {
    Anybody,
    OwnersOnly,
    Nobody
}

車のクラス:

public class Car {
    public Guid ID { get; set; }
    public VisibilityType Visibility { get; set; }
    public ICollection<User> Owners { get; set; }
}

そのため、クエリ オブジェクトの条件付き制限メソッドを記述する必要があります。を持つすべての車を返しますVisibilityType.Publicが、車Visibilityのプロパティ値が である場合はVisibilityType.OwnersOnly、そのグループに属するユーザーに返すように制限します。

これは私が働いている現在の制限方法ですが、条件はありません:

public class CarQueryObject
{

    private User user { get; set; }
    private const string OwnersProperty = "Owners";
    private const string OwnersIDProperty = "Owners.ID";

    public CarQueryObject RestrictToOwners()
    {
        // How do I add a conditional criteria here?  Only restrict by owner
        // if the QueryObject has VisibilityType.OwnersOnly?  Note that it should
        // *NOT* restrict VisibilityType.Anybody
        CreateOwnersAlias();
        Criteria.Add(Restrictions.Eq(OwnersIDProperty, user.Id));
        return this;
    }

    public CarQueryObject JoinFetchOwned()
    {
        Criteria.SetFetchMode(OwnersProperty, FetchMode.Join);
        return this;
    }

    public void CreateOwnersAlias()
    {
        Criteria.CreateAlias(OwnersProperty, OwnersProperty, JoinType.LeftOuterJoin);
        JoinFetchOwned();
    }
}

?_?

4

1 に答える 1