NHibernateUtils 2.0.2
dotnet add package NHibernateUtils --version 2.0.2
NuGet\Install-Package NHibernateUtils -Version 2.0.2
<PackageReference Include="NHibernateUtils" Version="2.0.2" />
<PackageVersion Include="NHibernateUtils" Version="2.0.2" />
<PackageReference Include="NHibernateUtils" />
paket add NHibernateUtils --version 2.0.2
#r "nuget: NHibernateUtils, 2.0.2"
#:package NHibernateUtils@2.0.2
#addin nuget:?package=NHibernateUtils&version=2.0.2
#tool nuget:?package=NHibernateUtils&version=2.0.2
NHibernateUtils
AuditingInterceptor
When an entity is being saved, AuditingInterceptor sets its properties decorated with CreationTimeAttribute, CreationUserAttribute, ModificationTimeAttribute or ModificationUserAttribute,
When an entity is being updated, AuditingInterceptor sets its properties decorated with ModificationTimeAttribute or ModificationUserAttribute:
public class Foo
{
// ...
[CreationTime]
public virtual DateTime CreationTime { get; set; }
[CreationUser]
public virtual string? CreationUser { get; set; }
[ModificationTime]
public virtual DateTime ModificationTime { get; set; }
[ModificationUser]
public virtual string? ModificationUser { get; set; }
public virtual int Quantity { get; set; }
public virtual bool IsMarked { get; set; }
}
// ...
ClaimsPrincipal principal = ...;
AuditingInterceptor interceptor = new AuditingInterceptor(principal);
using var session = _sessionFactory.WithOptions().Interceptor(interceptor).OpenSession();
using var tx = session.BeginTransaction();
// ...
// ...
await session.SaveAsync(foo1).ConfigureAwait(false);
await session.UpdateAsync(foo2).ConfigureAwait(false);
// ...
await tx.CommitAsync().ConfigureAwait(false);
CheckTransactionListener
CheckTransactionListener ensures each sql statement is executed in a transaction, it throws an NoTransactionException if there is no transaction.
Configuration configuration = new Configuration();
configuration.Configure();
CheckTransactionListener checkTransactionListener = new CheckTransactionListener();
configuration.AppendListeners(ListenerType.PreInsert, new IPreInsertEventListener[] { checkTransactionListener });
configuration.AppendListeners(ListenerType.PreUpdate, new IPreUpdateEventListener[] { checkTransactionListener });
configuration.AppendListeners(ListenerType.PreDelete, new IPreDeleteEventListener[] { checkTransactionListener });
configuration.AppendListeners(ListenerType.PreLoad, new IPreLoadEventListener[] { checkTransactionListener });
// ...
ChunkExtensions
ChunkExtensions.LoadInChunksAsync allows loading data from database in chunks by using foreach syntax.
It comes from an inventory allocation senario. Assume there are a lot of pallet records in database:
| PalletCode | Fifo | Quantity |
|---|---|---|
| P01 | 01 | 1 |
| P02 | 02 | 2 |
| P03 | 03 | 3 |
| ... | ... | ... |
| P99 | 99 | 99 |
The required number is 100 and the top 14 records would suffice, 1 + 2 + 3 + ... + 14 = 105, it is wasteful to load all these records to do the allocation work.
Loading records in chunks should improve the performance:
int required = 100;
int sum = 0;
var q = _session.Query<Pallet>().OrderBy(x => x.Fifo);
// the chunk size is 10 and only 2 chunks will be loaded
await foreach (var item in q.LoadInChunksAsync(10).ConfigureAwait(false))
{
sum += item.Quantity;
item.IsAllocated = true;
if (sum >= 100)
{
break;
}
}
IModelMapperConfigurator
IModelMapperConfigurator is a custom interface to configure NHibernate.Mapping.ByCode.ModelMapper:
public interface IModelMapperConfigurator
{
void Configure(ModelMapper mapper);
}
DataAnnotationsModelMapperConfigurator applies conventions on before mapping columns:
- if a property is a value type and not a Nullable<T>, then the column is
not null - if a property is decorated with a
RequiredAttribute, then the column isnot null - if a property is decorated with a
MaxLengthAttribute, then use it's value as the column's length
SimpleSearchExtensions
SimpleSearchExtensions translates a searchArgs object into a predicate:
class Student
{
public string StudentName { get; set; } = default!;
public Clazz Clazz { get; set; } = default!;
public int No { get; set; }
}
class Clazz
{
public string ClazzName { get; set; } = default!;
}
class StudentSearchArgs
{
// default SearchMode is Auto, Like for string and Equals for other datatype
[SearchArg]
public string? StudentName { get; set; }
// source property path is Clazz.ClazzName
[SearchArg(new string[] { nameof(Student.Clazz), nameof(Clazz.ClazzName) }, SearchMode.Equal)]
public string? ClassName { get; set; }
[SearchArg]
publick int? No { get; set; }
}
StudentSearchArgs args = new StudentSearchArgs
{
StudentName = "A",
ClassName = "B",
No = 3,
};
// args means:
// q.Where(x => x.StudentName.Like("%A%") && x.Clazz.ClazzName == "B" && x.No == 3);
var IQueryable<Student> q = ...;
var pagedList = await q.SearchAsync(args, "No", 0, 10);
TestingQueryable
This is a unit testing tool allowing mock IQueryable<T> returned from ISession.Query<T> method:
List<Foo> list = new List<Foo>
{
new Foo { Bar = "Bar1", Baz = "Baz1" },
new Foo { Bar = "Bar2", Baz = "Baz2" },
new Foo { Bar = "Bar3", Baz = "Baz3" },
};
// q is a query running against list
TestingQueryable<Foo> q = new TestingQueryable<Foo>(list);
var foos = await q.Where(x => x.Bar == "Bar2").ToListAsync();
The source code is from:
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net7.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net7.0
- NHibernate (>= 5.4.0)
- System.Linq.Dynamic.Core (>= 1.2.23)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
增加 TestingQueryable 静态类,为创建 TestingQueryable<T> 提供便利方法。