블로그 이미지
SQL Server VS. 석이 minsouk@hotmail.com MSSQL 쿼리성능 관련해 궁금한 사항이 있다면 언제나 누구나 TeamViewer + Line (네이버 japan 메신저) 에 minsouk1 추가 후 연락주세요~ 010-9967-0955 보미아빠

카테고리

보미아빠, 석이 (459)
밥벌이 (16)
싸이클 (1)
일상 (1)
Total232,316
Today0
Yesterday20

달력

« » 2019.5
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

공지사항

using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Serialization;


namespace CRUD
{
    class Program
    {
        static void Main(string[] args)
        {
            ServerDataTest();
            //LoadBalancerDataTest();
        }

        public static void LoadBalancerDataTest()
        {

            DataManager<ServerKey, LoadBalancerValue> dataManager = new DataManager<ServerKey, LoadBalancerValue>();

            dataManager.Insert(
                new ServerKey { ServerName = "ServerA" }, 
                new LoadBalancerValue { LoadBalancerPrivateIp = "pri1", LoadBalancerPublicIp = "pub1" });
            dataManager.Insert(                                                   
                new ServerKey { ServerName = "ServerB" }, 
                new LoadBalancerValue { LoadBalancerPrivateIp = "pri2", LoadBalancerPublicIp = "pub2" });
            dataManager.Update(                                                   
                new ServerKey { ServerName = "ServerB" }, 
                new LoadBalancerValue { LoadBalancerPrivateIp = "pri3" });
            dataManager.Insert(                                                   
                new ServerKey { ServerName = "ServerC" }, 
                new LoadBalancerValue { LoadBalancerPrivateIp = "pri3", LoadBalancerPublicIp = "pub3" });
            dataManager.Delete(
                new ServerKey { ServerName = "ServerA" });

            // SerializeObject
            string json = dataManager.GetJson();
            Console.WriteLine(json);

            // DeserializeObject
            var tempobject = JsonConvert.DeserializeObject<List<KeyValuePair<ServerKey, LoadBalancerValue>>>(json);
            dataManager.Data.Clear();

            foreach (var a in tempobject)
            {
                dataManager.Insert( 
                    new ServerKey { ServerName = a.Key.ServerName },
                    new LoadBalancerValue {
                        LoadBalancerPrivateIp = a.Value.LoadBalancerPrivateIp,
                        LoadBalancerPublicIp = a.Value.LoadBalancerPublicIp });
            }

            // data confirm 
            foreach (var a in dataManager.Data)
            {
                Console.WriteLine($"{a.Key.ServerName}, {a.Value.LoadBalancerPrivateIp}, {a.Value.LoadBalancerPublicIp}");
            }

            Console.ReadKey();
        }


        public static void ServerDataTest()
        {

            DataManager<ServerKey, ServerValue> dataManager = new DataManager<ServerKey, ServerValue>();

            dataManager.Insert(
                new ServerKey { ServerName = "ServerA" },
                new ServerValue { ServerPrivateIp = "pri1", ServerPublicIp = "pub1" });
            dataManager.Insert(
                new ServerKey { ServerName = "ServerB" },
                new ServerValue { ServerPrivateIp = "pri2", ServerPublicIp = "pub2" });
            dataManager.Insert(
                new ServerKey { ServerName = "ServerB" },
                new ServerValue { ServerPrivateIp = "pri3", ServerPublicIp = "pub5" });
            dataManager.Update(
                new ServerKey { ServerName = "ServerB" }, 
                new ServerValue { ServerPrivateIp = "pri3" });
            dataManager.Insert(
                new ServerKey { ServerName = "ServerC" },
                new ServerValue { ServerPrivateIp = "pri3", ServerPublicIp = "pub3" });
            dataManager.Delete(
                new ServerKey { ServerName = "ServerA" });

            // SerializeObject
            string json = dataManager.GetJson();
            Console.WriteLine(json);

            // DeserializeObject
            var tempobject = JsonConvert.DeserializeObject<List<KeyValuePair<ServerKey, ServerValue>>>(json);
            dataManager.Data.Clear();

            foreach (var a in tempobject)
            {
                dataManager.Insert(
                    new ServerKey { ServerName = a.Key.ServerName },
                    new ServerValue { ServerPrivateIp = a.Value.ServerPrivateIp, ServerPublicIp = a.Value.ServerPublicIp });
            }

            // data confirm 
            foreach (var a in dataManager.Data)
            {
                Console.WriteLine($"{a.Key.ServerName}, {a.Value.ServerPrivateIp}, {a.Value.ServerPublicIp}");
            }

            ServerValue sv = dataManager.Data[new ServerKey { ServerName = "ServerB" }];
            Console.WriteLine($"{sv.ServerPrivateIp}, {sv.ServerPublicIp}");
            Console.ReadKey();
        }
    }
    

    class ServerKey : IEquatable <ServerKey>
    {
        
        public string ServerName
        {
            get; set;
        }

        public bool Equals(ServerKey other)
        {
            if (ServerName.Equals(other.ServerName))
                return true;
            else
                return false;
        }

        public override int GetHashCode()
        {
            return ServerName.GetHashCode();
            // return _X.GetHashCode() ^ _Y.GetHashCode();

        }
    }
    
    class ServerValue
    {
        public string ServerPrivateIp { get; set; } = "";
        public string ServerPublicIp { get; set; } = "";
    }

    class LoadBalancerValue
    {
        public string LoadBalancerPrivateIp { get; set; } = "";
        public string LoadBalancerPublicIp { get; set; } = "";
    }

    class DataManager <K, V> 
    {
        private object lockobj = new object(); 

        public Dictionary<K, V> Data { get; set; } = new Dictionary<K, V>();

        public DataManager() { }

        public void Insert(K key, V value)
        {
            lock (lockobj)
            {
                if (!Data.ContainsKey(key))
                    Data.Add(key, value);
                else
                    Update(key, value);
            }
        }

        public void Update(K key, V value)
        {
            lock (lockobj)
            {
                V oldValue;
                if (Data.TryGetValue(key, out oldValue))
                {
                    var newProperies = value.GetType().GetProperties();
                    foreach (var n in newProperies)
                    {
                        var oldProperties = oldValue.GetType().GetProperties();
                        foreach (var o in oldProperties)
                        {
                            if (o.Name == n.Name)
                            {
                                string oldPropertyValue = oldValue.GetType().GetProperty(o.Name).GetValue(oldValue, null).ToString();
                                if (value.GetType().GetProperty(n.Name).GetValue(value, null).Equals(""))
                                {
                                    value.GetType().GetProperty(n.Name).SetValue(value, oldPropertyValue, null);
                                }
                            }
                        }
                    }
                }
                Data[key] = value;
            }
        }

        public void Delete(K key)
        {
            lock (lockobj)
            {
                if (Data.ContainsKey(key))
                {
                    Data.Remove(key);
                }
            }
        }

        public string GetJson()
        {
            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.Formatting = Formatting.Indented;
            settings.ContractResolver = new DictionaryAsArrayResolver();
            return JsonConvert.SerializeObject(Data, settings);
        }
    }

    class DictionaryAsArrayResolver : DefaultContractResolver
    {
        protected override JsonContract CreateContract(Type objectType)
        {
            if (objectType.GetInterfaces().Any(i => i == typeof(IDictionary) ||
               (i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>))))
            {
                return base.CreateArrayContract(objectType);
            }
            return base.CreateContract(objectType);
        }
    }
}

Posted by 보미아빠

 

if (DesignMode) return;

......우울하다...이걸 몰라 얼마나 많은 삽질을 했던가......

Posted by 보미아빠

console.progressbar

분류없음 / 2019.03.13 15:27

https://gist.github.com/DanielSWolf/0ab6a96899cc5377bf54

Posted by 보미아빠

최근에 달린 댓글

최근에 받은 트랙백

글 보관함