Giter Site home page Giter Site logo

nhibernateutils's Introduction

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, 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 is Clazz.ClazzName
        [SearchArg("", 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", 1, 10);

TestingQueryable

This is a unit testing tool allowing mock IQueryable 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:

nhibernateutils's People

Contributors

dongbeifeng avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.