Skip to content

Instantly share code, notes, and snippets.

@sunilkumarmedium
Created November 22, 2020 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunilkumarmedium/570ea844459ba191e95cc641b4278edd to your computer and use it in GitHub Desktop.
Save sunilkumarmedium/570ea844459ba191e95cc641b4278edd to your computer and use it in GitHub Desktop.
NHibernateServiceRegistration.cs
namespace CleanArchitectureApp.Infrastructure.Persistence
{
public static class ServiceRegistration
{
public static void AddPersistenceInfrastructure(this IServiceCollection services, IConfiguration configuration)
{
var mapper = new ModelMapper();
mapper.AddMappings(typeof(UserMap).Assembly.ExportedTypes);
HbmMapping domainMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
var NHConfiguration = new Configuration();
NHConfiguration.DataBaseIntegration(c =>
{
c.Dialect<MsSql2008Dialect>();
c.ConnectionString = configuration.GetConnectionString("DefaultConnection");
c.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
c.LogFormattedSql = true;
c.LogSqlInConsole = true;
});
NHConfiguration.AddMapping(domainMapping);
var sessionFactory = NHConfiguration.BuildSessionFactory();
services.AddSingleton(sessionFactory);
services.AddScoped(factory => sessionFactory.OpenSession());
#region Repositories
services.AddTransient(typeof(IGenericRepositoryAsync<>), typeof(GenericRepositoryAsync<>));
services.AddTransient<IUserRepositoryAsync, UserRepositoryAsync>();
services.AddTransient<IUserStatusRepositoryAsync, UserStatusRepositoryAsync>();
services.AddTransient<ILoginLogRepositoryAsync, LoginLogRepositoryAsync>();
services.AddTransient<ILoginLogTypeRepositoryAsync, LoginLogTypeRepositoryAsync>();
services.AddTransient<IUserTokenRepositoryAsync, UserTokenRepositoryAsync>();
services.AddTransient<IEmailTemplateRepositoryAsync, EmailTemplateRepositoryAsync>();
services.AddTransient<IEmailRecipientRepositoryAsync, EmailRecipientRepositoryAsync>();
#endregion Repositories
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment