Exemple #1
0
        public NHibernate.Cfg.Configuration Configure()
        {
            SetAssemblyBinding();

            var configuration = new NHibernate.Cfg.Configuration();

            // NOTE: the NHibernate documentation states that this file would be automatically loaded, however in testings this was not the case.
            // The expectation is that this file will be in the binaries location.
            configuration.Configure(
                Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "hibernate.cfg.xml"));

            // 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);

            void SetAssemblyBinding()
            {
                // NHibernate does not behave nicely with assemblies that are loaded from another folder.
                // By default NHibernate tries to load the assembly from the execution folder.
                // In our case we have pre loaded the assemblies into the domain, so we just need to tell NHibernate to pull the loaded
                // assembly. Setting the AssemblyResolve event fixes this. c.f. https://nhibernate.jira.com/browse/NH-2063 for further details.
                var assemblies = AppDomain.CurrentDomain.GetAssemblies();

                AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
                {
                    var assemblyName = e.Name.Split(",")[0];

                    return(assemblies.FirstOrDefault(a => a.GetName().Name.EqualsIgnoreCase(assemblyName)));
                };
            }
        }
        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);
        }