NHibernateUtils 2.0.2

dotnet add package NHibernateUtils --version 2.0.2
NuGet\Install-Package NHibernateUtils -Version 2.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NHibernateUtils" Version="2.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NHibernateUtils --version 2.0.2
#r "nuget: NHibernateUtils, 2.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NHibernateUtils as a Cake Addin
#addin nuget:?package=NHibernateUtils&version=2.0.2

// Install NHibernateUtils as a Cake Tool
#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 is not 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on NHibernateUtils:

Package Downloads
Arc.AppSettings

基于 NHibernate 实现的应用程序参数。

Arc.Ops

基于 NHibernate 实现的操作记录功能。

Arc.AppSeqs

基于 NHibernate 实现的应用程序级别的序列。

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.2 318 1/23/2023
2.0.1 272 12/18/2022
1.1.1 550 7/27/2022
1.1.0 415 7/27/2022
1.0.1 434 7/25/2022
1.0.0 620 6/24/2022

增加 TestingQueryable 静态类,为创建 TestingQueryable<T> 提供便利方法。