Skip to content

ASP.NET Core

Martin Havlišta edited this page Sep 4, 2018 · 33 revisions

WORK IN PROGRESS

Follow these steps to use CoreDdd in ASP.NET Core MVC application (tested with Visual Studio 2017 and .NET Core 2.1):

  1. Create a new ASP.NET Core MVC project.
  2. Install CoreDdd.AspNetCore nuget package.
  3. Install CoreDdd.Nhibernate nuget package.
  4. Add a new empty NHibernate configurator class, example:
using System.Reflection;
using CoreDdd.Nhibernate.Configurations;

namespace CoreDddSampleAspNetCoreWebApp
{
    public class CoreDddSampleNhibernateConfigurator : NhibernateConfigurator
    {
        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>

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

Set hibernate.cfg.xml Build Action to Content, and Copy to Output Directory to Copy if newer. For different databases, please look at Entity database persistence.

  1. Add services.AddScoped<IUnitOfWork, NhibernateUnitOfWork>(); into Startup.ConfigureServices().
  2. Add services.AddSingleton<TransactionScopeUnitOfWorkMiddleware>(); into Startup.ConfigureServices().
  3. Add app.UseMiddleware<TransactionScopeUnitOfWorkMiddleware>(); into Startup.Configure().

Clone this wiki locally