/// <summary> /// 匹配映射比较 /// 2个实体类做映射,将一个实体类的数据复制到另一个实体类上 /// 在领域模型中BO与DTO的映射 /// 可以使用: /// 1.硬编码 /// 2.泛型+反射 /// 3.泛型+表达式树 /// </summary> public static void MapperCompare() { People p = new People() { Id = 1001, Number = 10, Name = "VesNing", Age = 28 }; var result1 = HardCodingMapper.Mapper(p); result1.ToString(); var result2 = ReflectionMapper.Mapper <People, PeopleCopy>(p); result2.ToString(); var result3 = SerializeMapper.Mapper(p); result3.ToString(); var result4 = ExpressionMapper.Mapper <People, PeopleCopy>(p); StopWatchRun((m) => { HardCodingMapper.Mapper(m); }, p, "硬编码"); StopWatchRun((m) => { ReflectionMapper.Mapper <People, PeopleCopy>(m); }, p, "泛型+反射"); StopWatchRun((m) => { SerializeMapper.Mapper(m); }, p, "NewtoneSoft序列化"); StopWatchRun((m) => { ExpressionMapper.Mapper <People, PeopleCopy>(m); }, p, "表达式树+字典缓存"); }
public static void Show() { { //Hard copy people to peopleCopy People people = new People() { Id = 11, Name = "Eleven", Age = 31 }; PeopleCopy peopleCopy = new PeopleCopy() { Id = people.Id, Name = people.Name, Age = people.Age }; //By reflection, fit different type of classes. var resultReflection = ReflectionMapper.Trans <People, PeopleCopy>(people); //By serialization var resultSerialization = SerializaMapper.Trans <People, PeopleCopy>(people); //By expression, 1. generic, 2. high performance. Could hard program dynamically and cache the result. var resultExpression = ExpressionGenericMapper <People, PeopleCopy> .Trans(people); var resultExpression1 = ExpressionGenericMapper <People, PeopleCopy> .Trans(people); } }
public void ReflectionMapTest() { var date = DateTime.Now; var clonnedClass = new MappedClass { DateTimeProp = date, IntProp = 5, StringProp = "qwe" }; var destClass = new DestClass { DateTimeProp = date, IntProp = 5, StringProp = "qwe" }; var mapper = new ReflectionMapper(); mapper.Register <MappedClass, DestClass>(); var mapped = mapper.Map <MappedClass, DestClass>(clonnedClass); mapped.Should().BeEquivalentTo(destClass); Assert.Pass(); }
public App() { DispatcherUnhandledException += App_DispatcherUnhandledException; TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; IMapperService mapper = new ReflectionMapper(); IUserDataService userData = new LocalUserDataService(mapper); userData.Load(); _mainWindowViewModel = new MainWindowViewModel(userData); _settingsWindowViewModel = new SettingsWindowViewModel(userData); InitializeTileDataProviders(userData); }
internal void ApplyIteratePropertyReferenceHandling(SqlTagContext ctx, SqlText sqlText) { if (ctx == null) { throw new ArgumentNullException("ctx"); } if (sqlText == null) { throw new ArgumentNullException("sqlText"); } if (_propertyPlaceholder != null) { var parsedPropertyName = ReflectionMapper.GetReflectedFullName(ctx, sqlText, this._propertyPlaceholder); this._currentPropertyName = parsedPropertyName; } }
/// <summary> /// NOTE: Seems as though this class was written to not have knowledge of BaseTag, I just don't understand why. /// </summary> /// <param name="tag"></param> public void RememberBinding(Bind tag) { if (tag == null) { return; } var bindExpression = GetBindExpression(tag.Name); var exists = bindExpression != null; if (bindExpression == null) { bindExpression = new BindingExpression(tag.Name); } bindExpression.Value = tag.Value; bindExpression.PropertyName = tag.Property; bindExpression.FullPropertyName = ReflectionMapper.GetReflectedFullName(this, tag, tag.Property); if (!exists) { _bindings.Add(bindExpression); } }
/// <summary> /// /// </summary> /// <param name="ctx"></param> /// <param name="tag"></param> /// <param name="parameterObject"></param> /// <param name="bodyContent"></param> /// <returns></returns> /// <remarks> /// Updated By: Richard Beacroft /// Updated Date: 11\10\2013 /// Description: Enables property iterate item paths to be replaced with full property paths, which will later be reflected. /// </remarks> public override int DoEndFragment(SqlTagContext ctx, SqlTag tag, object parameterObject, StringBuilder bodyContent) { IterateContext iterate = (IterateContext)ctx.GetAttribute(tag); if (iterate.IsCompleted) { return(INCLUDE_BODY); } var iterateTag = ((Iterate)tag); var propertyName = iterateTag.Property; if (propertyName == null) { propertyName = String.Empty; } // build full reflection path if (!ReflectionMapper.ReplacePropertyIndexerWithFullName(ctx, iterateTag, bodyContent)) { string find = propertyName + ReflectionMapper.ENUMERATOR_PLACEHOLDER; string replace = propertyName + "[" + iterate.Index + "]"; //Parameter-index-Dynamic StringHandler.Replace(bodyContent, find, replace); } if (iterate.IsFirst) { if (iterateTag.Open != null) { bodyContent.Insert(0, ctx.ReplaceBindingVariables(iterateTag.Open)); bodyContent.Insert(0, ' '); } } if (iterate.IsLast) { if (iterateTag.Close != null) { bodyContent.Append(ctx.ReplaceBindingVariables(iterateTag.Close)); } } else { if (iterateTag.Conjunction != null) { bodyContent.Append(ctx.ReplaceBindingVariables(iterateTag.Conjunction)); bodyContent.Append(' '); } } if (iterate.HasNext) { return(REPEAT_BODY); } iterate.IsCompleted = true; return(INCLUDE_BODY); }
internal string ReplaceIterateCurrentProperty(SqlText sqlText, string tokenValue) { return(ReflectionMapper.GetReflectedFullName(this, sqlText, tokenValue)); }
internal string ReplaceIterateCurrentProperty(BaseTag baseTag) { return(ReflectionMapper.GetReflectedFullName(this, baseTag, baseTag.Property)); }