Source

Camino / src / Empi.Camino / Configuration / CaminoSettingsProvider.cs

using System;
using System.Configuration;
using Empi.Camino.Data.Retailer;
using System.Linq;
using Empi.Camino.Data;

namespace Empi.Camino.Configuration
{
    class CaminoSettingsProvider: SettingsProvider
    {
       
        public override string ApplicationName
        {
            get
            {
                return (System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
        {
            SettingsPropertyValueCollection settingsPropertyValueCollection =new SettingsPropertyValueCollection();
            //MP:should operate on a more Common RepositoryName 
            RepositoryBase<Empi.Camino.Data.IConfigurationBase> retailerDataConfiguration = RepositoryFactory.GetCachedRetailerDataRepository<Empi.Camino.Data.IConfigurationBase>();

            foreach(SettingsProperty settingsProperty in collection)
            {
				//MP: DataContextFactory should also be applied
                var conf=retailerDataConfiguration.SingleOrDefault(c=> c.Key==settingsProperty.Name, (Type)context["ConfigurationTableType"]);
                if(conf!=null)
                {
                    var settpropvalue=new SettingsPropertyValue(settingsProperty);
                    settpropvalue.SerializedValue=conf.Value;
                    
                    settingsPropertyValueCollection.Add(settpropvalue);
                }
            }

            return settingsPropertyValueCollection;
        }

        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
        {
            //MP:should operate on a more Common RepositoryName
            RepositoryBase<Empi.Camino.Data.IConfigurationBase> retailerDataConfiguration = RepositoryFactory.GetCachedRetailerDataRepository<Empi.Camino.Data.IConfigurationBase>();

            foreach (var settingsPropertyValue in from item in collection.Cast<SettingsPropertyValue>() where item.IsDirty select item)
            {
				//MP: DataContextFactory should also be applied
                var conf = retailerDataConfiguration.SingleOrDefault(c => c.Key == settingsPropertyValue.Name, (Type)context["ConfigurationTableType"]);
                if (conf != null)
                {
                    conf.Value = settingsPropertyValue.SerializedValue.ToString();
                }
                else
                {
                    Empi.Camino.Data.IConfigurationBase propValue = retailerDataConfiguration.CreateInstance((Type)context["ConfigurationTableType"]);
                    propValue.Key = settingsPropertyValue.Name;
                    propValue.Value = settingsPropertyValue.SerializedValue.ToString();
                }
            }

            retailerDataConfiguration.SaveAll();
        }
    }
}