Exemplo n.º 1
0
        public void Change_Database_Setting_Properties()
        {
            string info1 = "DataProvider changed";
            string info2 = "DataConnectionString changed";
            string result = String.Empty;
            DatabaseSettings settings = new DatabaseSettings();
            settings.PropertyChanged += (s, e) =>
            {
                if (e.PropertyName ==
                    ReflectionUtility.GetPropertyName(() => ((DatabaseSettings)s).DataProvider))
                {
                    result = info1;
                }
                else if(e.PropertyName ==
                    ReflectionUtility.GetPropertyName(() => ((DatabaseSettings)s).DataConnectionString))
                {
                    result = info2;
                }

            };

            settings.DataProvider = "Set Provider";
            Console.Out.WriteLine(settings.DataProvider);
            Assert.AreEqual(info1, result);

            settings.DataConnectionString = "Set Connection String";
            Console.Out.WriteLine(settings.DataConnectionString);
            Assert.AreEqual(info2, result);
        }
Exemplo n.º 2
0
 protected DataProviderManagerBase(DatabaseSettings settings)
 {
     AspectF.Define.MustBeNonNull(settings).Do(() =>
     {
         this.Settings = settings;
     });
 }
        public virtual void SaveSettings(DatabaseSettings settings)
        {
            if (settings == null)
                throw new ArgumentNullException("settings");

            string filePath = MapToPhysicalPath(FilePath);
            if (!File.Exists(filePath))
            {
                using (File.Create(filePath))
                {
                    //we use 'using' to close the file after it's created
                }
            }

            var text = ToFormatedString(settings);
            File.WriteAllText(filePath, text);
        }
 public NoSqlDataProviderManager(DatabaseSettings settings)
     : base(settings)
 {
 }
        protected virtual string ToFormatedString(DatabaseSettings settings)
        {
            if (settings == null)
                return "";

            return string.Format("DataProvider: {0}{2}DataConnectionString: {1}{2}",
                                 settings.DataProvider,
                                 settings.DataConnectionString,
                                 Environment.NewLine
                );
        }
        /// <summary>
        /// Convert setting file to DatabaseSetting object
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        protected virtual DatabaseSettings ParseSettings(string text)
        {
            DatabaseSettings settings = new DatabaseSettings();
            if (String.IsNullOrEmpty(text))
                return settings;

            var items = new List<string>();
            using (var reader = new StringReader(text))
            {
                string str;
                while ((str = reader.ReadLine()) != null)
                    items.Add(str);
            }

            foreach (var item in items)
            {
                var keyPair = item.Split(Separator);
                if (keyPair.Count() == 0)
                    continue;
                if (keyPair.Count() != 2)
                    throw new CommonException("Database setting is not correct.");
                string key = keyPair[0];
                string value = keyPair[1];
                DatabaseEnum keyEnum;
                if (!Enum.TryParse(key, true, out keyEnum))
                    keyEnum = DatabaseEnum.Unknown;
                switch (keyEnum) // only get last data provider and data connection string
                {
                    case DatabaseEnum.DataProvider:
                        settings.DataProvider = value;
                        break;
                    case DatabaseEnum.DataConnectionString:
                        settings.DataConnectionString = value;
                        break;
                    default:
                        settings.UnknownItems.Add(key, value);
                        break;
                }
            }

            return settings;
        }