Esempio n. 1
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. 2
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));
        }