Esempio n. 1
0
        public ConfigInitializer(Type configInterface, object config)
        {
            config.AssertParameterNotNull(nameof(config));
            if (!configInterface.IsInterface || !configInterface.IsAssignableFrom(config.GetType()))
            {
                throw new Exception(@"Internal Error : Parameter ""configType"" must be the type of the config interface implemented by the dynamic type of the config instance.");
            }

            ConfigInterface = configInterface;
            Config          = config;
            ConfigHelper    = new ConfigTypeHelper(ConfigInterface);
        }
Esempio n. 2
0
        public void OnShutDown()
        {
            foreach (var configInterface in RegisteredConfigInterfaces)
            {
                var configHelper = new ConfigTypeHelper(configInterface);

                var configInstance = Container.Resolve(configInterface);
                var serializer     = new ConfigSerializer(configInstance.GetType());

                serializer.Serialize(configInstance, configHelper.GetConfigFilePath());
            }
        }
Esempio n. 3
0
        public void SetConfigPropertyInternal <TConfigInterface, TProperty>(Expression <Func <TConfigInterface, TProperty> > propertyExpression, TProperty propertyValue) where TConfigInterface : class
        {
            propertyExpression.AssertParameterNotNull(nameof(propertyExpression));
            if (!RegisteredConfigInterfaces.Contains(typeof(TConfigInterface)))
            {
                throw new Exception($"Error : The config type {typeof(TConfigInterface).Name} has not been registered.");
            }

            var configProperty = ReflectionUtils.GetPropertyInfo(propertyExpression);
            var configHelper   = new ConfigTypeHelper(typeof(TConfigInterface));

            var config = Container.Resolve <TConfigInterface>();

            configHelper.SetConfigPropertyInternal(config, configProperty, propertyValue);
        }
Esempio n. 4
0
        public void RegisterConfigInterface <TConfigInterface>() where TConfigInterface : class
        {
            var configTypeBuilder = new ConfigTypeBuilder(typeof(TConfigInterface));
            var configType        = configTypeBuilder.BuildConfigImplementation();

            var configHelper = new ConfigTypeHelper(typeof(TConfigInterface));
            var configFile   = configHelper.GetConfigFilePath();

            TConfigInterface configInstance = null;

            if (!File.Exists(configFile))
            {
                configInstance = (TConfigInterface)Activator.CreateInstance(configType);
                var initializer = new ConfigInitializer(typeof(TConfigInterface), configInstance);
                initializer.InitializeConfigInstance();
            }
            else
            {
                var configSerializer = new ConfigSerializer(configType);
                configInstance = (TConfigInterface)configSerializer.Deserialize(configFile);

                var initializer = new ConfigInitializer(typeof(TConfigInterface), configInstance);

                var xmlDoc = XDocument.Load(configFile);
                var root   = xmlDoc.Root;
                var xmlConfigProperties = root.Descendants().Where(desc => desc.Parent == root);

                foreach (var prop in typeof(TConfigInterface).GetProperties())
                {
                    if (!xmlConfigProperties.Any(node => node.Name == prop.Name))
                    {
                        initializer.InitializeConfigProperty(prop);
                    }
                }
            }

            var eventAggregatorFieldName = configHelper.GetConfigImplementationEventAggreagatorFieldName();

            configType.GetField(eventAggregatorFieldName).SetValue(configInstance, EventAggregator);

            Container.RegisterInstance <TConfigInterface>(configInstance);
            RegisteredConfigInterfaces.Add(typeof(TConfigInterface));
        }
Esempio n. 5
0
 public ConfigTypeBuilder(Type configInterface)
 {
     ConfigInterface = configInterface.AssertParameterNotNull(nameof(configInterface));
     AssertConfigInterface();
     ConfigHelper = new ConfigTypeHelper(ConfigInterface);
 }