Skip to content

ASP.NET Core

xhafan edited this page Nov 30, 2018 · 33 revisions

Follow these steps to use CoreDdd in ASP.NET Core MVC application:

  1. Create a new ASP.NET Core MVC project.
  2. Install following nuget packages into the project:
  1. Add a new empty NHibernate configurator class, example:
public class CoreDddSampleNhibernateConfigurator : BaseNhibernateConfigurator
{
    protected override Assembly[] GetAssembliesToMap()
    {
        return new Assembly[0];
    }
}
  1. Add a new NHibernate config file hibernate.cfg.xml . Here is SQLite example:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>

    <property name="connection.connection_string">Data Source=CoreDddSampleAspNetCoreWebApp.db</property>
    <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
    <property name="connection.release_mode">on_close</property>

    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="show_sql">false</property>
    <property name="proxyfactory.factory_class">NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate</property>
    <property name="transaction.use_connection_on_system_prepare">false</property> <!-- set transaction.use_connection_on_system_prepare to 'false' when using .NET 4.6.1+ or .NET Standard 2.0+, NHibernate 5.1.0+, more info here https://github.com/npgsql/npgsql/issues/1985#issuecomment-397266128 ; remove this line for previous NHibernate versions -->

  </session-factory>
</hibernate-configuration>

Set hibernate.cfg.xml Build Action to Content, and Copy to Output Directory to Copy if newer.

  1. Install System.Data.SQLite.Core nuget package for SQLite database. For different databases, please look at Entity database persistence.
  2. Add the following code into Startup.ConfigureServices() to register CoreDdd services into Dependency Injection:
services.AddCoreDdd();
services.AddCoreDddNhibernate<CoreDddSampleNhibernateConfigurator>();
  1. To handle each request within ADO.NET transaction, add the following code into Startup.Configure() (needs to be called before app.UseMvc call):
app.UseMiddleware<UnitOfWorkDependencyInjectionMiddleware>(System.Data.IsolationLevel.ReadCommitted);
  1. To handle each request within TransactionScope with ADO.NET connection enlisted in the transaction scope, use the following code (needs to be called before app.UseMvc call):
Action<TransactionScope> transactionScopeEnlistmentAction = transactionScope =>
{
    // enlist custom resource manager into the transaction scope
};

app.UseMiddleware<TransactionScopeUnitOfWorkDependencyInjectionMiddleware>(
    System.Transactions.IsolationLevel.ReadCommitted, 
    transactionScopeEnlistmentAction
    );

The code above is available as a sample ASP.NET Core MVC application here. The sample application also contains ShipController with methods to create a new Ship aggregate root domain entity, query ships by name and update ship data. Once command handlers, query handlers and domain event handlers are added to the application, they needs to be registered into the Dependency Injection (add Scrutor nuget package to be able to Scan for services):

// register command handlers, query handlers and domain event handlers
services.Scan(scan => scan
    .FromAssemblyOf<Ship>()
    .AddClasses(classes => classes.AssignableTo(typeof(ICommandHandler<>)))
        .AsImplementedInterfaces()
        .WithTransientLifetime()
    .AddClasses(classes => classes.AssignableTo(typeof(IQueryHandler<>)))
        .AsImplementedInterfaces()
        .WithTransientLifetime()
    .AddClasses(classes => classes.AssignableTo(typeof(IDomainEventHandler<>)))
        .AsImplementedInterfaces()
        .WithTransientLifetime()
    );

You can check out other wiki pages how to persist an entity, query data, modify an application state or create more complex DDD implementation.

Clone this wiki locally