Пример #1
0
        public void Configure <T>(Action <TypeConfiguration <T> > configure)
        {
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            if (Built)
            {
                throw new InvalidOperationException("The configuration has already been built.");
            }

            var type = typeof(T);
            var typeConfiguration = TypeConfigurations.FirstOrDefault(c => c.Type == type) as TypeConfiguration <T>;

            if (typeConfiguration == null)
            {
                typeConfiguration = new TypeConfiguration <T>();
                TypeConfigurations.Add(typeConfiguration);
            }

            configure(typeConfiguration);
        }
Пример #2
0
        private void CollectAssemblyDefinedTypeConfigurations()
        {
            if (!Assemblies.Any())
            {
                return;
            }

            foreach (var assembly in Assemblies)
            {
                var typeConfigurationTypeInfo = typeof(TypeConfiguration).GetTypeInfo();
                var types = assembly.ExportedTypes
                            .Select(t => t.GetTypeInfo())
                            .Where(t => !t.IsAbstract && typeConfigurationTypeInfo.IsAssignableFrom(t))
                            .ToList();

                foreach (var type in types)
                {
                    var baseTypeConfigurationType = FindGenericBaseTypeConfigurationType(type);
                    if (baseTypeConfigurationType == null)
                    {
                        throw new InvalidOperationException("You should extend from the generic version of TypeConfiguration.");
                    }

                    var instance    = (TypeConfiguration)Activator.CreateInstance(type.AsType());
                    var elementType = GetElementTypeFromConcreteTypeConfiguration(baseTypeConfigurationType);
                    var existing    = TypeConfigurations.FirstOrDefault(tc => tc.Type == elementType);
                    if (existing != null)
                    {
                        existing.Combine(instance);
                    }
                    else
                    {
                        TypeConfigurations.Add(instance);
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Loads the specified loaders.
        /// </summary>
        /// <param name="loaders">The list of configuration loaders to load into the context.</param>
        public void Load(params IConfigurationLoader[] loaders)
        {
            if (loaders.Any())
            {
                var typeConfigurations = loaders
                                         .Select(loader => loader.Load()).Aggregate((x, y) => x.Union(y));



                //first we have to add each type config to the collection
                foreach (var typeConfig in typeConfigurations)
                {
                    typeConfig.PerformAutoMap();

                    TypeConfigurations.Add(typeConfig.Type, typeConfig);
                }
                //then process the properties.
                //this stops the problem of types not existing for certain data handlers
                foreach (var typeConfig in typeConfigurations)
                {
                    ProcessProperties(typeConfig.Properties);
                }
            }
        }