Пример #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);
                    }
                }
            }
        }