コード例 #1
0
        /// <summary>
        /// ctor
        /// </summary>
        public LangDataMapper()
        {
            //Stopwatch sw = new Stopwatch();
            //sw.Start();
            string    entityName = typeof(TEntity).Name;
            ModelRule modelRule  = ModelRule.Get <TModel>();
            ModelRule entityRule = ModelRule.Get <TEntity>();
            string    lang       = ResHelper.CurrentCultureName.ToLower();

            _converter = GetConverter <TModel, TEntity>();
            IMappingExpression <TModel, TEntity> expm2t = null;
            IMappingExpression <TEntity, TModel> expt2m = null;

            Mapper.Initialize(cfg =>
            {
                expm2t = cfg.CreateMap <TModel, TEntity>();
                expt2m = cfg.CreateMap <TEntity, TModel>();
            });
            if (_converter != null)
            {
                expt2m.ProjectUsing(_converter.EntityToModel);
                // expm2t.ConstructUsing(_converter.ModelToEntity.Compile()); 不知为何这句无效
            }
            else
            {
                if (typeof(IMultiLanguage).IsAssignableFrom(typeof(TEntity)))
                {
                    RefHelper.CallMethod(this, "MapperLangProperties", new Type[] { typeof(TModel), typeof(TEntity) });
                }
                foreach (var r in modelRule.CollectionRules)
                {
                    if (r.Attr.EntityType != null && Mapper.Configuration.FindTypeMapFor(r.ModelType, r.Attr.EntityType) == null)
                    {
                        Mapper.Initialize(cfg =>
                        {
                            cfg.CreateMap(r.Attr.EntityType, r.ModelType);
                            cfg.CreateMap(r.ModelType, r.Attr.EntityType);
                        });
                    }
                    expt2m.ForMember(r.Name, opt => opt.Ignore());
                }
                foreach (var r in entityRule.CollectionRules)
                {
                    expm2t.ForMember(r.Name, opt => opt.Ignore());
                }
            }
            //Debug.WriteLine("Mapper Elapsed == " + sw.ElapsedMilliseconds);
        }