Exemplo n.º 1
0
        public IAssemblyTypeConfigurator Type()
        {
            var configurator = new TypeConfigurator();

            configurator.Setup(c => c(new TypeContext()));
            configurator.ConfigureBuild(s => this.typeRegistration.Execute(s));
            registrationActions.Add(() => configurator.Build());
            return(configurator);
        }
Exemplo n.º 2
0
        private TypeConfigurator GetOrCreateTypeConfigurator <TInterface>()
        {
            var key = typeof(TInterface);

            if (!_hash.TryGetValue(key, out var configurator))
            {
                _hash[key] = configurator = new TypeConfigurator(key);
            }

            return(configurator);
        }
Exemplo n.º 3
0
        public TypeConfigurator <T> Config <T>(Action <TypeConfigurator <T> > configuration)
        {
            var type = typeof(T);

            if (!_configurators.TryGetValue(type, out TypeConfigurator result))
            {
                result = new TypeConfigurator <T>();
                _configurators[type] = result;
            }
            var typed = (TypeConfigurator <T>)result;

            configuration(typed);
            return(typed);
        }
Exemplo n.º 4
0
        private TypeConfiguration BuildModel(Type type)
        {
            if (_configurations.TryGetValue(type, out var typeConfig))
            {
                return(typeConfig);
            }

            if (!_configurators.TryGetValue(type, out var configurator))
            {
                configurator = new TypeConfigurator();
            }

            typeConfig            = configurator.Configuration;
            typeConfig.Type       = type;
            _configurations[type] = typeConfig;

            if (type.IsValueType)
            {
                typeConfig.DefaultValue = Activator.CreateInstance(type);
            }
            else
            {
                typeConfig.DefaultValue = null;
            }

            if (type.IsGenericType)
            {
                var generic = type.GetGenericTypeDefinition();
                var args    = type.GetGenericArguments();
                if (generic == typeof(IDictionary <,>))
                {
                    typeConfig.KeyType   = BuildModel(args[0]);
                    typeConfig.ValueType = BuildModel(args[1]);
                }
                else
                {
                    typeConfig.IsExternalType = true;
                }
            }
            else if (type.IsInterface)
            {
                var props = new[] { type }.Concat(type.GetInterfaces()).SelectMany(s => s.GetProperties()).ToArray();
                foreach (var prop in props)
                {
                    if (!typeConfig.Members.TryGetValue(prop.Name, out var propConfig))
                    {
                        if (configurator.Members.TryGetValue(prop.Name, out var propConfigurator))
                        {
                            propConfig = propConfigurator.Configuration;
                        }
                        else
                        {
                            propConfig = new TypeMemberConfiguration(prop.PropertyType);
                        }
                        typeConfig.Members[prop.Name] = propConfig;
                    }
                    propConfig.TypeConfiguration = BuildModel(prop.PropertyType);
                }
            }
            else
            {
                typeConfig.IsExternalType = true;
            }

            return(typeConfig);
        }