NHibernate.QueryBuilder
1.0.0
See the version list below for details.
dotnet add package NHibernate.QueryBuilder --version 1.0.0
NuGet\Install-Package NHibernate.QueryBuilder -Version 1.0.0
<PackageReference Include="NHibernate.QueryBuilder" Version="1.0.0" />
paket add NHibernate.QueryBuilder --version 1.0.0
#r "nuget: NHibernate.QueryBuilder, 1.0.0"
// Install NHibernate.QueryBuilder as a Cake Addin
#addin nuget:?package=NHibernate.QueryBuilder&version=1.0.0
// Install NHibernate.QueryBuilder as a Cake Tool
#tool nuget:?package=NHibernate.QueryBuilder&version=1.0.0
How to use it?
Simple example illustrating preferred conventions:
- Implementing a query class
internal enum UserAlias {
Role,
Right,
Group
}
internal class UsersQuery : Query<UserPersistent, UserAlias>
{
private readonly RolePersistent _role = null;
private readonly RightPersistent _right = null;
private readonly GroupPersistent _group = null;
protected override IDictionary<UserAlias, string> CreateAliasesDictionary()
{
return new Dictionary<UserAlias, string>
{
[UserAlias.Role] = nameof(_role),
[UserAlias.Right] = nameof(_right),
[UserAlias.Group] = nameof(_group)
};
}
internal UsersQuery WithRolesAndRights()
{
Query = Query
.Left.JoinAlias(usr => usr.Roles, () => _role)
.Left.JoinAlias(() => _role.Rights, () => _right);
return this;
}
internal UsersQuery WithGroups()
{
Query = Query.Left.JoinAlias(usr => usr.Groups, () => _group);
return this;
}
}
- Creating QueryBuilder for above:
var builder = new QueryBuilder<UserPersistent, UserAlias>(session);
- Performing a query (let's say it's a very uncommon case, in which we want to find users who have role similar to 'Admin', are in a specific group type and are from within several countries - just for a demonstration purpose)
public IEnumerable<User> FindUsersByWeirdCriteria(GroupType type, string[] countries)
{
IEnumerable<UserPersistent> users = builder.NewQuery()
.WithRolesAndRights()
.WithGroups()
.Restrict.Like<RolePersistent>(UserAlias.Role, role => role.Name, 'Admin')
.Restrict.In(usr => usr.Country, countries)
.Restrict.Eq<GroupPersistent>(UserAlias.Group, grp => grp.Type, groupType)
.Build()
.List();
//some mapping logic
return users.Select(usr => mapper.MapToEntity(usr));
}
One thing to notice - restrictions made on aliased classes require usage of <T> typed methods in contrast to methods working directly on type of query (TPersistent) where only property selector and value is required (see Restrict.In method call above). Alias resolution by key, and not only by type, is required to distinguish multiple join operations to the same table and add restrictions to the desired operation.
Additional information
Query class can use two strategies for case-insensitive string comparison
- SqlNative - default mechanism provided by NHibernate (and specific to used database engine)
- CollationIndependent - projection property with sql function
UPPER
and in c# with method.ToUpperInvariant()
. This way collation set in database is disregarded andlike
comparison returns expected results. This strategy should only be used if you see problems with native mechanism. To change default behavior pass parameter to base constructor
public MyQuery() : base(StringEqualityComparison.CollationIndependent)
{
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- NHibernate (>= 5.1.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.