private static void InitializeNHibernate(IWindsorContainer container)
            {
                Environment.ObjectsFactory = new WindsorObjectsFactory(container);

                container.Register(Component.For <IAssembliesProvider>().ImplementedBy <AssembliesProvider>());

                container.Register(
                    Component.For <IOrmMappingFileDataProvider>()
                    .ImplementedBy <OrmMappingFileDataProvider>()
                    .DependsOn(Dependency.OnValue <DatabaseEngine>(DatabaseEngine.SqlServer))
                    .DependsOn(Dependency.OnValue <string>(OrmMappingFileConventions.OrmMappingAssembly))
                    );

                var ormMappingFileData      = container.Resolve <IOrmMappingFileDataProvider>().OrmMappingFileData();
                var nHibernateConfiguration = new Configuration().AddResources(ormMappingFileData.MappingFileFullNames, ormMappingFileData.Assembly);

                nHibernateConfiguration.AddCreateDateHooks();

                container
                .Register(
                    Component
                    .For <ISessionFactory>()
                    .UsingFactoryMethod(nHibernateConfiguration.BuildSessionFactory)
                    .LifeStyle.Singleton);

                container
                .Register(
                    Component
                    .For <Func <IStatelessSession> >()
                    .UsingFactoryMethod <Func <IStatelessSession> >(kernel => () => kernel.Resolve <ISessionFactory>().OpenStatelessSession())
                    .LifestyleSingleton());         // The function is a singleton, not the session

                container.Register(
                    Component
                    .For <EdFiOdsConnectionProvider>()
                    .DependsOn(
                        Dependency
                        .OnComponent(
                            typeof(IDatabaseConnectionStringProvider),
                            typeof(IDatabaseConnectionStringProvider).GetServiceNameWithSuffix(Databases.Ods.ToString()))));
            }
        public Configuration Configure(IWindsorContainer container)
        {
            //Resolve all extensions to include in core mapping
            var extensionConfigurationProviders = container.ResolveAll <IExtensionNHibernateConfigurationProvider>()
                                                  .ToList();

            _entityExtensionHbmBagsByEntityName = extensionConfigurationProviders
                                                  .SelectMany(x => x.EntityExtensionHbmBagByEntityName)
                                                  .GroupBy(x => x.Key)
                                                  .ToDictionary(
                x => x.Key,
                x => x.Select(y => y.Value)
                .ToArray());

            _aggregateExtensionHbmBagsByEntityName = extensionConfigurationProviders
                                                     .SelectMany(x => x.AggregateExtensionHbmBagsByEntityName)
                                                     .GroupBy(x => x.Key)
                                                     .ToDictionary(
                x => x.Key,
                x => x.SelectMany(y => y.Value)
                .ToArray());

            _extensionDescriptorByEntityName = extensionConfigurationProviders
                                               .SelectMany(x => x.NonDiscriminatorBasedHbmJoinedSubclassesByEntityName)
                                               .GroupBy(x => x.Key)
                                               .ToDictionary(
                x => x.Key,
                x => x.SelectMany(y => y.Value)
                .ToArray());

            _extensionDerivedEntityByEntityName = extensionConfigurationProviders
                                                  .SelectMany(x => x.DiscriminatorBasedHbmSubclassesByEntityName)
                                                  .GroupBy(x => x.Key)
                                                  .ToDictionary(k => k.Key, v => v.SelectMany(y => y.Value).ToArray());

            _beforeBindMappingActivities = container.ResolveAll <INHibernateBeforeBindMappingActivity>();

            // Start the NHibernate configuration
            var configuration = new Configuration();

            // Add the configuration to the container
            container.Register(
                Component.For <Configuration>()
                .Instance(configuration));

            configuration.BeforeBindMapping += Configuration_BeforeBindMapping;

            // Get all the authorization strategy configurators
            var authorizationStrategyConfigurators = container.ResolveAll <INHibernateFilterConfigurator>();

            // Get all the filter definitions from all the configurators
            var allFilterDetails =
                (from c in authorizationStrategyConfigurators
                 from f in c.GetFilters()
                 select f)
                .Distinct()
                .ToList();

            // Group the filters by name first (there can only be 1 "default" filter, but flexibility
            // to apply same filter name with same parameters to different entities should be supported
            // (and is in fact supported below when filters are applied to individual entity mappings)
            var allFilterDetailsGroupedByName =
                from f in allFilterDetails
                group f by f.FilterDefinition.FilterName
                into g
                select g;

            // Add all the filter definitions to the NHibernate configuration
            foreach (var filterDetails in allFilterDetailsGroupedByName)
            {
                configuration.AddFilterDefinition(
                    filterDetails.First()
                    .FilterDefinition);
            }

            // Configure the mappings
            var ormMappingFileData = container.Resolve <IOrmMappingFileDataProvider>().OrmMappingFileData();

            configuration.AddResources(ormMappingFileData.MappingFileFullNames, ormMappingFileData.Assembly);

            //Resolve all extension assemblies and add to NHibernate configuration
            extensionConfigurationProviders
            .ForEach(
                e => configuration.AddResources(e.OrmMappingFileData.MappingFileFullNames, e.OrmMappingFileData.Assembly));

            var filterCriteriaApplicatorProvider = container.Resolve <IFilterCriteriaApplicatorProvider>();

            // Invoke configuration activities
            var configurationActivities = container.ResolveAll <INHibernateConfigurationActivity>();

            foreach (var configurationActivity in configurationActivities)
            {
                configurationActivity.Execute(configuration);
            }

            // Apply the previously defined filters to the mappings
            foreach (var mapping in configuration.ClassMappings)
            {
                Type entityType = mapping.MappedClass;
                var  properties = entityType.GetProperties();

                var applicableFilters = allFilterDetails
                                        .Where(filterDetails => filterDetails.ShouldApply(entityType, properties))
                                        .ToList();

                foreach (var filter in applicableFilters)
                {
                    var filterDefinition = filter.FilterDefinition;

                    // Save the filter criteria applicators
                    filterCriteriaApplicatorProvider.AddCriteriaApplicator(
                        filterDefinition.FilterName,
                        entityType,
                        filter.CriteriaApplicator);

                    mapping.AddFilter(
                        filterDefinition.FilterName,
                        filterDefinition.DefaultFilterCondition);

                    var metaAttribute = new MetaAttribute(filterDefinition.FilterName);
                    metaAttribute.AddValue(filter.HqlConditionFormatString);

                    mapping.MetaAttributes.Add(
                        "HqlFilter_" + filterDefinition.FilterName,
                        metaAttribute);
                }
            }

            // NHibernate Dependency Injection
            Environment.ObjectsFactory = new WindsorObjectsFactory(container);

            configuration.AddCreateDateHooks();

            // Build and register the session factory with the container
            container.Register(
                Component
                .For <ISessionFactory>()
                .UsingFactoryMethod(configuration.BuildSessionFactory)
                .LifeStyle.Singleton);

            container.Register(
                Component
                .For <Func <IStatelessSession> >()
                .UsingFactoryMethod <Func <IStatelessSession> >(
                    kernel => () => kernel.Resolve <ISessionFactory>().OpenStatelessSession())
                .LifestyleSingleton());     // The function is a singleton, not the session

            container.Register(
                Component
                .For <EdFiOdsConnectionProvider>()
                .DependsOn(
                    Dependency
                    .OnComponent(
                        typeof(IDatabaseConnectionStringProvider),
                        typeof(IDatabaseConnectionStringProvider).GetServiceNameWithSuffix(
                            Databases.Ods.ToString()))));

            // Register the SQL Server version of the parameter list setter
            // This is registered with the API by the SqlServerSupportConditionalFeature
            // in the non-legacy code.
            container.Register(
                Component
                .For <IParameterListSetter>()
                .ImplementedBy <SqlServerTableValuedParameterListSetter>());

            return(configuration);
        }
        public Configuration Configure()
        {
            var configuration = new Configuration();

            // Add the configuration to the container
            configuration.BeforeBindMapping += Configuration_BeforeBindMapping;

            // Get all the filter definitions from all the configurators
            var allFilterDetails = _authorizationStrategyConfigurators
                                   .SelectMany(c => c.GetFilters())
                                   .Distinct()
                                   .ToList();

            // Group the filters by name first (there can only be 1 "default" filter, but flexibility
            // to apply same filter name with same parameters to different entities should be supported
            // (and is in fact supported below when filters are applied to individual entity mappings)
            var allFilterDetailsGroupedByName = allFilterDetails
                                                .GroupBy(f => f.FilterDefinition.FilterName)
                                                .Select(g => g);

            // Add all the filter definitions to the NHibernate configuration
            foreach (var filterDetails in allFilterDetailsGroupedByName)
            {
                configuration.AddFilterDefinition(
                    filterDetails.First()
                    .FilterDefinition);
            }

            // Configure the mappings
            var ormMappingFileData = _ormMappingFileDataProvider.OrmMappingFileData();

            configuration.AddResources(ormMappingFileData.MappingFileFullNames, ormMappingFileData.Assembly);

            //Resolve all extension assemblies and add to NHibernate configuration
            _extensionConfigurationProviders.ForEach(
                e => configuration.AddResources(e.OrmMappingFileData.MappingFileFullNames, e.OrmMappingFileData.Assembly));

            // Invoke configuration activities
            foreach (var configurationActivity in _configurationActivities)
            {
                configurationActivity.Execute(configuration);
            }

            // Apply the previously defined filters to the mappings
            foreach (var mapping in configuration.ClassMappings)
            {
                Type entityType = mapping.MappedClass;
                var  properties = entityType.GetProperties();

                var applicableFilters = allFilterDetails
                                        .Where(filterDetails => filterDetails.ShouldApply(entityType, properties))
                                        .ToList();

                foreach (var filter in applicableFilters)
                {
                    var filterDefinition = filter.FilterDefinition;

                    // Save the filter criteria applicators
                    _filterCriteriaApplicatorProvider.AddCriteriaApplicator(
                        filterDefinition.FilterName,
                        entityType,
                        filter.CriteriaApplicator);

                    mapping.AddFilter(
                        filterDefinition.FilterName,
                        filterDefinition.DefaultFilterCondition);

                    var metaAttribute = new MetaAttribute(filterDefinition.FilterName);
                    metaAttribute.AddValue(filter.HqlConditionFormatString);

                    mapping.MetaAttributes.Add(
                        "HqlFilter_" + filterDefinition.FilterName,
                        metaAttribute);
                }
            }

            configuration.AddCreateDateHooks();

            return(configuration);
        }