<commit_before>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.DataProtection;
using NinjaHive.BusinessLayer;
using NinjaHive.BusinessLayer.CrossCuttingConcerns;
using NinjaHive.BusinessLayer.QueryHandlers;
using NinjaHive.BusinessLayer.Services;
using NinjaHive.Core;
using NinjaHive.Core.Decorators;
using NinjaHive.Core.Services;
using NinjaHive.Domain;
using NinjaHive.WebApp.Models.IdentityModels;
using Owin;
using SimpleInjector;
using SimpleInjector.Advanced;
using SimpleInjector.Extensions.LifetimeScoping;
using SimpleInjector.Integration.Web.Mvc;

namespace NinjaHive.WebApp
{
    public static class Bootstrapper
    {
        public static Container container;

        public static Container Initialize(IAppBuilder app)
        {
            AutoMapperConfiguration.Configure();
            
            container = GetInitializedContainer(app);

            container.Verify();

            DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));

            return container;
        }

        public static Container GetInitializedContainer(IAppBuilder app)
        {
            container = new Container();
            container.Options.DefaultScopedLifestyle = new LifetimeScopeLifestyle();

            RegisterOwinAndIdentity(app);
            RegisterNinjaHiveDatabase();

            RegisterCommandHandlers();
            RegisterQueryHandlers();

            RegisterServices();

            container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
            container.RegisterMvcIntegratedFilterProvider();

            return container;
        }

        private static void RegisterServices()
        {
            container.RegisterSingleton(typeof (IEntityMapper<>), typeof (EntitiesAutoMapper<>));
            container.RegisterSingleton(typeof (IEntityMapper<,>), typeof (EntitiesAutoMapper<,>));
        }

        private static void RegisterNinjaHiveDatabase()
        {
            var connectionString = ConfigurationManager.ConnectionStrings["NinjaHiveContext"].ConnectionString;

            var dbContextRegistration = Lifestyle.Scoped.CreateRegistration(() => new NinjaHiveContext(connectionString), container);

            container.AddRegistration(typeof(NinjaHiveContext), dbContextRegistration);
            container.AddRegistration(typeof(DbContext), dbContextRegistration);
        }

        private static void RegisterCommandHandlers()
        {
            container.Register(typeof (ICommandHandler<>), Bootstrapper.GetAssemblies());

            container.RegisterDecorator(typeof (ICommandHandler<>), typeof (ValidationCommandHandlerDecorator<>));
            container.RegisterDecorator(typeof (ICommandHandler<>), typeof (SaveChangesCommandHandlerDecorator<>));
            container.RegisterDecorator(typeof(ICommandHandler<>), typeof(LifetimeScopeCommandHandlerProxy<>), Lifestyle.Singleton);
        }<commit_msg>Register IRepository in a scoped lifestyle<commit_after>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.DataProtection;
using NinjaHive.BusinessLayer;
using NinjaHive.BusinessLayer.CrossCuttingConcerns;
using NinjaHive.BusinessLayer.QueryHandlers;
using NinjaHive.BusinessLayer.Services;
using NinjaHive.Core;
using NinjaHive.Core.Decorators;
using NinjaHive.Core.Services;
using NinjaHive.Domain;
using NinjaHive.WebApp.Models.IdentityModels;
using Owin;
using SimpleInjector;
using SimpleInjector.Advanced;
using SimpleInjector.Extensions.LifetimeScoping;
using SimpleInjector.Integration.Web.Mvc;

namespace NinjaHive.WebApp
{
    public static class Bootstrapper
    {
        public static Container container;

        public static Container Initialize(IAppBuilder app)
        {
            AutoMapperConfiguration.Configure();
            
            container = GetInitializedContainer(app);

            container.Verify();

            DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));

            return container;
        }

        public static Container GetInitializedContainer(IAppBuilder app)
        {
            container = new Container();
            container.Options.DefaultScopedLifestyle = new LifetimeScopeLifestyle();

            RegisterOwinAndIdentity(app);
            RegisterNinjaHiveDatabase();

            RegisterCommandHandlers();
            RegisterQueryHandlers();

            RegisterServices();

            container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
            container.RegisterMvcIntegratedFilterProvider();

            return container;
        }

        private static void RegisterServices()
        {
            container.RegisterSingleton(typeof (IEntityMapper<>), typeof (EntitiesAutoMapper<>));
            container.RegisterSingleton(typeof (IEntityMapper<,>), typeof (EntitiesAutoMapper<,>));
        }

        private static void RegisterNinjaHiveDatabase()
        {
            var connectionString = ConfigurationManager.ConnectionStrings["NinjaHiveContext"].ConnectionString;

            var dbContextRegistration = Lifestyle.Scoped.CreateRegistration(() => new NinjaHiveContext(connectionString), container);

            container.AddRegistration(typeof(NinjaHiveContext), dbContextRegistration);
            container.AddRegistration(typeof(DbContext), dbContextRegistration);

            container.Register(typeof(IRepository<>), typeof(NinjaHiveRepository<>), Lifestyle.Scoped);
        }

        private static void RegisterCommandHandlers()
        {
            container.Register(typeof (ICommandHandler<>), Bootstrapper.GetAssemblies());

            container.RegisterDecorator(typeof (ICommandHandler<>), typeof (ValidationCommandHandlerDecorator<>));
            container.RegisterDecorator(typeof (ICommandHandler<>), typeof (SaveChangesCommandHandlerDecorator<>));
            container.RegisterDecorator(typeof(ICommandHandler<>), typeof(LifetimeScopeCommandHandlerProxy<>), Lifestyle.Singleton);
        }
{'fraction_non_alphanumeric': 0.08451768974901723, 'fraction_numerical': 0.0, 'mean_word_length': 3.6782178217821784, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4646623', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1616, 'n_words': 296}