CompileTimeAOP.TransactionAwareness 3.9.1

.NET Standard 2.0 .NET Framework 4.6.1
NuGet\Install-Package CompileTimeAOP.TransactionAwareness -Version 3.9.1
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.
dotnet add package CompileTimeAOP.TransactionAwareness --version 3.9.1
<PackageReference Include="CompileTimeAOP.TransactionAwareness" Version="3.9.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CompileTimeAOP.TransactionAwareness --version 3.9.1
#r "nuget: CompileTimeAOP.TransactionAwareness, 3.9.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install CompileTimeAOP.TransactionAwareness as a Cake Addin
#addin nuget:?package=CompileTimeAOP.TransactionAwareness&version=3.9.1

// Install CompileTimeAOP.TransactionAwareness as a Cake Tool
#tool nuget:?package=CompileTimeAOP.TransactionAwareness&version=3.9.1

NHibernate Transaction Awareness Framework

NHibernate Transaction Awareness Framework is implemented by CompileTimeWeaver.Fody ( to gain the best performance. It removes the burden of NHibernate developers of managing sessions, transactions, and domain model validations.

1. Transaction-aware Object

    public class MyClass: IDisposable
        public async Task TravelAllPersonsAsync()
            var session = TransactionContext.GetCurrentSession("CustomerDb");

            var persons = await session.Query<Person>().ToListAsync().ConfigureAwait(false);
            foreach (var person in persons)
                Trace.WriteLine(person.Id + " - " + person.FirstName);

        public void Dispose()
            //do nothing

It is a C# class decorated by TransactionAwareAttribute. In the TravelAllPersonsAsync() method above, developers can write code to read or write database with NHibernate session without think about when the transaction is commited or when the session is opened/closed, the framework manages the session lift-cycle and transaction lift-cycle automatically.

Use Transaction-aware class:

    using (var testObj = new MyClass())
        await testObj.TravelAllPersonsAsync();

2. Transaction-aware methods

A method is Transaction-aware method if: (1) it is a non-static method, and (2) it is decorated by TransactionAwareAttribute, or the class is decorated by TransactionAwareAttribute.

It can either be sync method or async method since version 3.9, and it is not thread-safe. It can call static TransactionContext.GetCurrentSession() method to get current context session.

    ISession session = TransactionContext..GetCurrentSession("CustomerDb");
    var user1 = session.Get<User>(1);
    user1.Name = "Simon Lu";

3. Requirements of a Transaction-aware Object

A transaction-aware object is an instance of an ordinary .net class meeting a few simple requirements:

  • Decorated with TransactionAwareAttribute.
  • Implement IDisposable interface, the void Dispose() method must exist event though it is empty.
  • Call TransactionContext's methods in transaction-aware methods only.
  • Don't explicitly close session or commit/rollback transaction

4. Transaction

Because framework manages the life-time of transactions, developers don't need to start a transaction and commit/roolback it in the code. The framework makes the transaction control more easy with these two programming interfaces:

Using AutoComplete

The AutoCompleteAttribute indicates the Debit() method below to abort the transaction if any exception is thrown, and commits the transaction otherwise.

public class Account
    public virtual void Debit(int amount)
        // Do some database work. Any exception thrown here aborts the transaction;
        // otherwise, transaction commits.
Voting in an Automatic Transaction

The following code fragment shows the SetAbort and SetComplete methods in use.

//Try to do something crucial to the transaction in progress.
if( !DoSomeWork() )
  //Something goes wrong.
  //All goes well.

SetComplete indicates that your method votes to commit its work; SetAbort indicates that your object encountered a problem and votes to abort the ongoing transaction, a single abort vote from any method participating in the transaction causes the entire transaction to fail.

5. Join existing session or open new session

  [TransactionAware(DbName="your_database_name", TransactionAwareOption = TransactionAwareOption.Required)]
  public class MyClass: IDisposable

When applying the TransactionAwareAttribute, you can set TransactionAwareOption property to control if the object joins existing session or open a new session.

  • TransactionAwareOption.Required (default) Indicates that the object requires a session. It runs in the scope of an existing session, if one exists. If no session exists, the object starts one.
  • TransactionAwareOption.RequiresNew Indicates that the object requires a new session.

6. Object Injection

A Transaction-aware object can inject objects as its properties or fields by declare the properties/fields with InjectedOnCreatedAttribute, and transparently dispose them when it is disposed. This open the door for developers to inject Transaction-aware objects into the other Transaction-aware object so that they can share the same database session. Below is an example class, and instances of EntityRepository is created and injected as private field _entityRepository, and it is disposed automatically even though the Dispose method body is empty.

    public class TransactionAwareUnitOfWork : IDisposable
        private EntityRepository _entityRepository;
        public void Dispose()
            //Transaction Awareness framework will automatically dispose all injected objects

7. Multiple Databases in Distributed-transaction

    public class OnlineShoppingStore: IDisposable
        public void PlaceOrder(OrderDto order)
            var customerDbSession = TransactionContext.GetCurrentSession("CustomerDb");
            var customer = customerDbSession.Query<CustomerEntity>(order.CustomerId);
            var transDbSession = TransactionContext.GetCurrentSession("TransactionDb");
            transDbSession.Save(new OrderEntity(){...});

In the PlaceOrder method above, framework puts two database update in one distributed transaction so that the integrity across multi-database is guaranteed.

8. Fluent Validation

The out-of-the-box FluentValidation feature guarantees the integrity of domain model whenever there is database update. It allows to put all model validation rules in different assembly to avoid domain model polution.


  • Create a C# project to contain all the FluentValidation validators.
  • Add all validators to validate entity classes, for example, the PersonValidator class below to validate Person entity class:
    using FluentValidation;

    public class PersonValidator : AbstractValidator<Person>
        public PersonValidator()
            RuleFor(x => x.Id).NotEmpty();
            RuleFor(x => x.FirstName).NotNull().Length(1, 10);


See wiki for detail usage:

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.9.1 659 2/6/2019
3.8.1 812 10/5/2017
3.8.0 732 7/2/2017
3.7.0 673 6/29/2017
3.6.1 714 6/2/2017
3.5.3 840 5/2/2017
3.5.1 795 4/30/2017
3.5.0 843 4/27/2017
3.4.1 873 4/10/2017
3.3.4 863 4/1/2017
3.1.4 830 3/11/2017
3.1.3 819 3/10/2017
3.1.2 828 3/9/2017
3.1.1 838 3/8/2017
3.1.0 805 3/8/2017
3.0.1 898 3/3/2017
3.0.0 815 3/2/2017

- support .netstandard 2.0 and .net framework 4.6.1
- allow async methods