• 对象关系映射 EmitMapper 及Tuple的使用


      public TDestination Map<TSource, TDestination>(TSource tSource)
            {
                if (tSource == null)
                    return default(TDestination);
                
                var mapper = ObjectMapperManager.DefaultInstance.GetMapper<TSource, TDestination>();
                return mapper.Map(tSource);
            }
    
            public IEnumerable<TDestination> MapperGeneric<TSource, TDestination>(IEnumerable<TSource> tSources)
            {
                if (tSources == null)
                    return null;
    
                IList<TDestination> tDestinations = new List<TDestination>();
                foreach (var tSource in tSources)
                {
                    tDestinations.Add(Map<TSource, TDestination>(tSource));
                }
                return tDestinations;
            }
    
            public IList<TDestination> MapperGeneric<TSource, TDestination>(IList<TSource> tSources)
            {
                if (tSources == null)
                    return null;
    
                IList<TDestination> tDestinations = new List<TDestination>();
                foreach (var tSource in tSources)
                {
                    tDestinations.Add(Map<TSource, TDestination>(tSource));
                }
                return tDestinations;
            }
    using EmitMapper.EmitBuilders;
    using EmitMapper.Mappers;
    using EmitMapper.MappingConfiguration;
    using EmitObjectMapper.Mappers;
    using System;
    using System.Collections.Generic;
    using System.Reflection.Emit;
    
    namespace EmitMapper
    {
      public class ObjectMapperManager
      {
        public static ObjectMapperManager _defaultInstance = (ObjectMapperManager) null;
        private static int _instanceCount = 0;
        private int instanceCount = 0;
        private Dictionary<MapperKey, int> objectsMapperIds = new Dictionary<MapperKey, int>();
        private List<ObjectsMapperDescr> objectsMappersList = new List<ObjectsMapperDescr>();
    
        public static ObjectMapperManager DefaultInstance
        {
          get
          {
            if (ObjectMapperManager._defaultInstance == null)
            {
              lock (typeof (ObjectMapperManager))
              {
                if (ObjectMapperManager._defaultInstance == null)
                  ObjectMapperManager._defaultInstance = new ObjectMapperManager();
              }
            }
            return ObjectMapperManager._defaultInstance;
          }
        }
    
        public ObjectMapperManager()
        {
          lock (typeof (ObjectMapperManager))
          {
            ++ObjectMapperManager._instanceCount;
            this.instanceCount = ObjectMapperManager._instanceCount;
          }
        }
    
        public ObjectsMapper<TFrom, TTo> GetMapper<TFrom, TTo>()
        {
          return new ObjectsMapper<TFrom, TTo>(this.GetMapperImpl(typeof (TFrom), typeof (TTo), (IMappingConfigurator) DefaultMapConfig.Instance));
        }
    
        public ObjectsMapper<TFrom, TTo> GetMapper<TFrom, TTo>(IMappingConfigurator mappingConfigurator)
        {
          return new ObjectsMapper<TFrom, TTo>(this.GetMapperImpl(typeof (TFrom), typeof (TTo), mappingConfigurator));
        }
    
        public ObjectsMapperBaseImpl GetMapperImpl(Type from, Type to, IMappingConfigurator mappingConfigurator)
        {
          return this.GetMapperInt(from, to, mappingConfigurator).mapper;
        }
    
        internal ObjectsMapperDescr GetMapperInt(Type from, Type to, IMappingConfigurator mappingConfigurator)
        {
          lock (this)
          {
            if (to == null)
              to = typeof (object);
            if (from == null)
              from = typeof (object);
            MapperKey local_0 = new MapperKey(from, to, mappingConfigurator.GetConfigurationName());
            int local_2;
            if (this.objectsMapperIds.TryGetValue(local_0, out local_2))
              return this.objectsMappersList[local_2];
            ObjectsMapperDescr local_1 = new ObjectsMapperDescr((ObjectsMapperBaseImpl) null, local_0, 0);
            this.AddMapper(local_1);
            string local_3 = this.GetMapperTypeName(from, to);
            ObjectsMapperBaseImpl local_4;
            if (MapperPrimitiveImpl.IsSupportedType(to))
              local_4 = (ObjectsMapperBaseImpl) new MapperPrimitiveImpl(this, from, to, mappingConfigurator);
            else if (MapperForCollectionImpl.IsSupportedType(to))
            {
              ObjectsMapperDescr local_5 = this.GetMapperInt(MapperForCollectionImpl.GetSubMapperTypeFrom(from), MapperForCollectionImpl.GetSubMapperTypeTo(to), mappingConfigurator);
              local_4 = (ObjectsMapperBaseImpl) MapperForCollectionImpl.CreateInstance(local_3 + (object) this.GetNextMapperId(), this, from, to, local_5, mappingConfigurator);
            }
            else
              local_4 = this.BuildObjectsMapper(local_3 + (object) this.GetNextMapperId(), from, to, mappingConfigurator);
            local_1.mapper = local_4;
            return local_1;
          }
        }
    
        private ObjectsMapperBaseImpl BuildObjectsMapper(string MapperTypeName, Type from, Type to, IMappingConfigurator mappingConfigurator)
        {
          TypeBuilder typeBuilder = DynamicAssemblyManager.DefineMapperType(MapperTypeName);
          CreateTargetInstanceBuilder.BuildCreateTargetInstanceMethod(to, typeBuilder);
          MappingBuilder mappingBuilder = new MappingBuilder(this, from, to, typeBuilder, mappingConfigurator);
          mappingBuilder.BuildCopyImplMethod();
          ObjectsMapperBaseImpl objectsMapperBaseImpl = (ObjectsMapperBaseImpl) Activator.CreateInstance(typeBuilder.CreateType());
          objectsMapperBaseImpl.Initialize(this, from, to, mappingConfigurator, mappingBuilder.storedObjects.ToArray());
          return objectsMapperBaseImpl;
        }
    
        private ObjectsMapperDescr GetMapperByKey(MapperKey key)
        {
          return this.objectsMappersList[this.objectsMapperIds[key]];
        }
    
        private int AddMapper(ObjectsMapperDescr descr)
        {
          descr.id = this.objectsMappersList.Count;
          this.objectsMappersList.Add(descr);
          this.objectsMapperIds.Add(descr.key, descr.id);
          return descr.id;
        }
    
        private int GetNextMapperId()
        {
          return this.objectsMapperIds.Count;
        }
    
        private bool IsMapperCreated(MapperKey key)
        {
          return this.objectsMapperIds.ContainsKey(key);
        }
    
        private string GetMapperTypeKey(Type from, Type to, string mapperName)
        {
          return this.GetMapperTypeName(from, to) + (mapperName ?? "");
        }
    
        private string GetMapperTypeName(Type from, Type to)
        {
          return "ObjectsMapper" + (object) this.instanceCount + "_" + (from == null ? "null" : from.FullName) + "_" + (to == null ? "null" : to.FullName);
        }
      }
    }

            private int _rootId;
            /// <summary>
            /// 活动来源ID
            /// </summary>
            public int RootId
            {
                get { return this._rootId; }
                set { this._rootId = value; }
            }

         private List<string> _rootId;
            /// <summary>
            /// 活动来源ID
            /// </summary>
            public List<string> RootId
            {
                get { return this._rootId; }
                set
                {
                    if (this._rootId == null)
                    {
                        this._rootId = new List<string>();
                    }
                    this._rootId = value;
                }
            }

     public string Tip{get;set;}

     private bool _isSelected = false;
        public bool IsSelected
        {

        get
        {
    _isSelected=Tip == "1" ? true : false;
    return _isSelected;
        }
        set
        {
    _isSelected = value;
        }
        } 
            private Dictionary<int, string> _shortUrl;
            public Dictionary<int, string> ShortUrl {
                get
                {
                    _shortUrl= new Dictionary<int, string>()
                    {
                        {1,"t.cn"},
                        {2,"dwz.cn"},
                        {3,"qq.cn.hn"},
                        {4,"jd.cn.hn"},
                        {5,"tb.cn.hn"},
                        {6,"sina.lt"},
                        {7,"tinyurl.com"},
                        {8,"qr.net"},
                        {9,"goo.gl"},
                        {10,"is.gd"},
                        {11,"j.mp"},
                        {12,"bit.ly"}
                    };
                    return _shortUrl;
                }
                set { _shortUrl = value; }
            }

    Tuple的使用:

            public Tuple<string, string> DevileryPeriod()
            {
                switch (this.Cycle)
                {
                    case 1:
                        return new Tuple<string, string>("全天", $"{this.SendTime:yyyy-MM-dd} 09:00-21:00");
                    case 4:
                        return new Tuple<string, string>("上午", $"{this.SendTime:yyyy-MM-dd} 09:00-12:00");
                    case 8:
                        return new Tuple<string, string>("下午", $"{this.SendTime:yyyy-MM-dd} 12:00-18:00");
                    case 2:
                        return new Tuple<string, string>("晚上", $"{this.SendTime:yyyy-MM-dd} 18:00-21:00");
                    default:
                        return new Tuple<string, string>("全天", $"{this.SendTime:yyyy-MM-dd} 09:00-21:00");
                }
    
            }
    //DliveryWays = t.DevileryPeriod().Item1,
    class Program
      {
        static void Main()
        {
          Tuple<string, string> name = new Tuple<string, string>("Jochen", "Rindt");
          Console.WriteLine(name.ToString());
    
          var result = Divide(5, 2);
          Console.WriteLine("result of division: {0}, reminder: {1}", result.Item1, result.Item2);
    
          AnyElementNumber();
        }
    
        static void AnyElementNumber()
        {
          var tuple = Tuple.Create<string, string, string, int, int, int, double, Tuple<int, int>>(
              "Stephanie", "Alina", "Nagel", 2009, 6, 2, 1.37, Tuple.Create<int, int>(52, 3490));
          Console.WriteLine(tuple.Item1);
        }
    
        public static Tuple<int, int> Divide(int dividend, int divisor)
        {
          int result = dividend / divisor;
          int reminder = dividend % divisor;
    
          return Tuple.Create<int, int>(result, reminder);
        }
      }
  • 相关阅读:
    《笨办法学python》 第14课手记
    《笨办法学Python》 第13课手记
    杭电2009----求数列的和
    杭电2008----数值统计
    杭电2007----平方和与立方和
    杭电2006----求奇数的乘积
    杭电2005----第几天?
    杭电2004---- 成绩转换
    杭电2003----求绝对值
    杭电2002----计算球体积
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/5244057.html
Copyright © 2020-2023  润新知