public override void ValidateAfterChildrenAdded()
        {
            base.ValidateAfterChildrenAdded();

            var defaultSerializer = OROptimizer.Serializer.TypeBasedSimpleSerializerAggregator.GetDefaultSerializerAggregator();

            var serializerAggregatorObject = _createInstanceFromTypeAndConstructorParameters.CreateInstance(this,
                                                                                                            typeof(ITypeBasedSimpleSerializerAggregator), _serializerAggregatorType, _parameters?.AllParameters ?? Array.Empty <IParameterElement>());

            TypeBasedSimpleSerializerAggregator = (ITypeBasedSimpleSerializerAggregator)serializerAggregatorObject;

            if (_parameterSerializersCollection != null)
            {
                foreach (var parameterSerializer in _parameterSerializersCollection.AllSerializers)
                {
                    if (parameterSerializer.Serializer == null)
                    {
                        LogHelper.Context.Log.WarnFormat("The value of '{0}.{1}' should not be null.", typeof(IParameterSerializer).FullName, nameof(IParameterSerializer.Serializer));
                        continue;
                    }

                    var defaultSerializerForType = defaultSerializer.GetSerializerForType(parameterSerializer.Serializer.SerializedType);
                    if (defaultSerializerForType != null)
                    {
                        LogHelper.Context.Log.InfoFormat("Replacing default serializer for type '{0}' with a serializer '{1}'. The default serializer was '{2}'.",
                                                         parameterSerializer.Serializer.SerializedType.GetTypeNameInCSharpClass(),
                                                         parameterSerializer.Serializer.GetType().GetTypeNameInCSharpClass(),
                                                         defaultSerializerForType.GetType().GetTypeNameInCSharpClass());

                        TypeBasedSimpleSerializerAggregator.UnRegister(parameterSerializer.Serializer.SerializedType);
                    }

                    TypeBasedSimpleSerializerAggregator.Register(parameterSerializer.Serializer);
                }
            }

            foreach (var serializer in defaultSerializer.GetRegisteredSerializers())
            {
                if (!TypeBasedSimpleSerializerAggregator.HasSerializerForType(serializer.SerializedType))
                {
                    TypeBasedSimpleSerializerAggregator.Register(serializer);
                }
            }

            LogSerializersData();
        }
 public override object GenerateValue()
 {
     return(_createInstanceFromTypeAndConstructorParameters.CreateInstance(this, ValueTypeInfo.Type, Parameters?.AllParameters, InjectedProperties?.AllProperties));
 }
Пример #3
0
        public override void ValidateAfterChildrenAdded()
        {
            base.ValidateAfterChildrenAdded();

            Type validBaseType = null;

            if (typeof(IDiModule).IsAssignableFrom(_typeInfo.Type))
            {
                validBaseType = typeof(IDiModule);
            }
            else if (_configuration.DiManagers.ActiveDiManagerElement.DiManager.ModuleType.IsAssignableFrom(_typeInfo.Type))
            {
                validBaseType = _configuration.DiManagers.ActiveDiManagerElement.DiManager.ModuleType;
            }
            else
            {
                _isDiManagerInactive = true;

                // If this is a native module, lets see if there is any non-active dependency manager module,
                // for which the type is correct.

                IDiManagerElement ownerDiManagerElement = null;

                var validModuleTypes = new List <Type>();
                foreach (var diManagerElement in _configuration.DiManagers.AllDiManagers)
                {
                    validModuleTypes.Add(diManagerElement.DiManager.ModuleType);

                    if (diManagerElement == _configuration.DiManagers.ActiveDiManagerElement)
                    {
                        continue;
                    }

                    if (diManagerElement.DiManager.ModuleType.IsAssignableFrom(_typeInfo.Type))
                    {
                        ownerDiManagerElement = diManagerElement;
                        validBaseType         = diManagerElement.DiManager.ModuleType;
                        break;
                    }
                }

                if (ownerDiManagerElement == null)
                {
                    var errorMessage = new StringBuilder();
                    errorMessage.Append($"Invalid type for module: '{_typeInfo.TypeCSharpFullName}'.The type used as a module should be one of the following types or a subclass of any of these types: '{typeof(IDiModule).FullName}'");

                    foreach (var valdModuleType in validModuleTypes)
                    {
                        errorMessage.Append($", '{valdModuleType.FullName}'");
                    }

                    errorMessage.Append(".");

                    throw new ConfigurationParseException(this, errorMessage.ToString());
                }

                LogHelper.Context.Log.DebugFormat("Note, the module '{0}' is disabled since dependency injection manager '{1}' that handles the module is not the active dependency injection manager. The active dependency injection manager is '{2}'.",
                                                  _typeInfo.TypeCSharpFullName, ownerDiManagerElement.Name, _configuration.DiManagers.ActiveDiManagerElement.Name);
            }

            if (validBaseType != null && !_isDiManagerInactive && this.Enabled)
            {
                DiModule = _createInstanceFromTypeAndConstructorParameters.CreateInstance(this, validBaseType, _typeInfo.Type, _parameters?.AllParameters ?? new IParameterElement[0]);
                LogHelper.Context.Log.InfoFormat("Created an instance of dependency injection module: {0}.", _typeInfo.TypeCSharpFullName);
            }
        }
        public override void ValidateAfterChildrenAdded()
        {
            base.ValidateAfterChildrenAdded();

            Instance = (TInstantiatedType)_createInstanceFromTypeAndConstructorParameters.CreateInstance(this, typeof(TInstantiatedType), ValueTypeInfo.Type, _parameters?.AllParameters ?? new IParameterElement[0]);
        }