public void TestBuildCallMethodNoReturnWithDbContext() { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); var prop1 = new PropertyMatch(true, PropertyMatch.TypeMatchLevels.Match, typeof(Dto).GetProperty(nameof(Dto.MyInt))); var prop2 = new PropertyMatch(true, PropertyMatch.TypeMatchLevels.Match, null, MatchSources.DbContext, context.GetType()); var method = typeof(Target1).GetMethod(nameof(Target1.SetMyIntAndAddEntityToDb)); var dto = new Dto { MyInt = 123 }; var target = new Target1(); //ATTEMPT var action = BuildCall.CallMethodReturnVoid(method, typeof(Dto), typeof(Target1), new List <PropertyMatch> { prop1, prop2 }); action.Invoke(dto, target, context); context.SaveChanges(); //VERIFY target.MyInt.ShouldEqual(123); context.NormalEntities.Count().ShouldEqual(1); } }
/// <summary> /// Finds all matches within given HTML. /// </summary> /// <param name="stringBuilder">The string builder.</param> /// <returns> /// List of all found matches /// </returns> protected IEnumerable<PropertyMatch> FindAllMatches(StringBuilder stringBuilder) { var matches = new List<PropertyMatch>(); var pattern = string.Concat("{{", identifier, "(:([^\\:\\{\\}]*))*}}"); foreach (Match match in Regex.Matches(stringBuilder.ToString(), pattern, RegexOptions.IgnoreCase)) { var propertyMatch = new PropertyMatch { GlobalMatch = match.Value }; if (match.Groups.Count > 2) { propertyMatch.Parameters = new string[match.Groups[2].Captures.Count]; var i = 0; foreach (Capture capture in match.Groups[2].Captures) { propertyMatch.Parameters[i] = capture.Value; i++; } } matches.Add(propertyMatch); } return matches; }
/// <summary> /// Finds all matches within given HTML. /// </summary> /// <param name="stringBuilder">The string builder.</param> /// <returns> /// List of all found matches /// </returns> protected IList <PropertyMatch> FindAllMatches(StringBuilder stringBuilder) { var matches = new List <PropertyMatch>(); var pattern = string.Concat("{{", identifier, "(:([^\\:\\{\\}]*))*}}"); foreach (Match match in Regex.Matches(stringBuilder.ToString(), pattern, RegexOptions.IgnoreCase)) { var propertyMatch = new PropertyMatch { GlobalMatch = match.Value }; if (match.Groups.Count > 2) { propertyMatch.Parameters = new string[match.Groups[2].Captures.Count]; var i = 0; foreach (Capture capture in match.Groups[2].Captures) { propertyMatch.Parameters[i] = capture.Value; i++; } } matches.Add(propertyMatch); } return(matches); }
public static void RegexProperties(ref string source, PropertyMatch matchMethod) { Regex regex = new Regex("\\$\\([a-zA-Z_:][a-zA-Z0-9.\\-_:]*\\)"); Match match = regex.Match(source); foreach (Capture capture in match.Captures) { matchMethod(ref source, capture); } }
private static PropertyMatch FindMatch(ParameterInfo parameter, IEnumerable <PropertyInfo> propertiesToCheck, MatchNameAndType propMatcher) { PropertyMatch bestMatch = null; foreach (var propertyInfo in propertiesToCheck) { var match = propMatcher(parameter.Name, parameter.ParameterType, propertyInfo); if (bestMatch == null || bestMatch.Score < match.Score) { bestMatch = match; } } return(bestMatch); }
public void TestBuildCallMethodNoReturnAgain() { //SETUP var prop = new PropertyMatch(true, PropertyMatch.TypeMatchLevels.Match, typeof(Dto).GetProperty(nameof(Dto.MyInt))); var method = typeof(Target1).GetMethod(nameof(Target1.SetMyInt)); var dto = new Dto { MyInt = 123 }; var target = new Target1(); //ATTEMPT var action = BuildCall.CallMethodReturnVoid(method, typeof(Dto), typeof(Target1), new List<PropertyMatch>{prop}); action.Invoke(dto, target); //VERIFY target.MyInt.ShouldEqual(123); }
private static PropertyMatch SetDefaultProperty(PropertyMatch match, Type ownerType) { if (match.IsDefault) { match.Name = defaultPropertyMap.GetOrAdd(ownerType, key => { return(ownerType .GetCustomAttributes(true) .OfType <DefaultPropertyAttribute>() .Select(x => x.Name) .FirstOrDefault() ?? ""); }); } return(match); }
public void TestBuildCallCtor() { //SETUP var prop1 = new PropertyMatch(true, PropertyMatch.TypeMatchLevels.Match, typeof(Dto).GetProperty(nameof(Dto.MyInt))); var prop2 = new PropertyMatch(true, PropertyMatch.TypeMatchLevels.Match, typeof(Dto).GetProperty(nameof(Dto.MyString))); var ctor = typeof(Target1).GetConstructors().Single(x => x.GetParameters().Length == 2); var dto = new Dto { MyInt = 123, MyString = "Hello" }; //ATTEMPT var func = BuildCall.CallConstructor(ctor, typeof(Dto), new List<PropertyMatch> { prop1, prop2 }); var newInstance = func.Invoke(dto); //VERIFY ((int)newInstance.MyInt).ShouldEqual(123); ((string)newInstance.MyString).ShouldEqual("Hello"); }
public void TestBuildCallMethodWithReturn() { //SETUP var prop = new PropertyMatch(true, PropertyMatch.TypeMatchLevels.Match, typeof(Dto).GetProperty(nameof(Dto.MyString))); var method = typeof(Target1).GetMethod(nameof(Target1.SetMyString)); var dto = new Dto { MyString = "Hello" }; var target = new Target1(); //ATTEMPT var action = BuildCall.CallMethodReturnStatus(method, typeof(Dto), typeof(Target1), new List<PropertyMatch> {prop}); var status = action.Invoke(dto, target); //VERIFY target.MyString.ShouldEqual("Hello"); ((string)status.Message).ShouldEqual("OK"); }
public void TestBuildCallChangePubDateDto() { //SETUP var prop = new PropertyMatch(true, PropertyMatch.TypeMatchLevels.Match, typeof(Tests.Dtos.ChangePubDateDto).GetProperty(nameof(Tests.Dtos.ChangePubDateDto.PublishedOn))); var method = typeof(Book).GetMethod(nameof(Book.UpdatePublishedOn)); var dto = new Tests.Dtos.ChangePubDateDto { PublishedOn = new DateTime(2000,1,1)}; var target = DddEfTestData.CreateDummyBooks(1).Single(); //ATTEMPT var action = BuildCall.CallMethodReturnVoid(method, typeof(Tests.Dtos.ChangePubDateDto), typeof(Book), new List<PropertyMatch>{prop}); action.Invoke(dto, target); //VERIFY target.PublishedOn.ShouldEqual(new DateTime(2000, 1, 1)); }
public void TestBuildCallStatic() { //SETUP var prop1 = new PropertyMatch(true, PropertyMatch.TypeMatchLevels.Match, typeof(Dto).GetProperty(nameof(Dto.MyInt))); var prop2 = new PropertyMatch(true, PropertyMatch.TypeMatchLevels.Match, typeof(Dto).GetProperty(nameof(Dto.MyString))); var method = typeof(Target1).GetMethod(nameof(Target1.Create)); var dto = new Dto { MyInt = 123, MyString = "Hello" }; //ATTEMPT var action = BuildCall.CallStaticCreator(method, typeof(Dto), new List<PropertyMatch> { prop1, prop2}); var status = action.Invoke(dto); //VERIFY ((string)status.Message).ShouldEqual("Static"); ((int)status.Result.MyInt).ShouldEqual(123); ((string)status.Result.MyString).ShouldEqual("Hello"); }
public void SetUp() { accessor = ReflectionHelper.GetAccessor <Address>(x => x.DistanceFromOffice); match = new PropertyMatch(accessor); }
/// <summary> /// 执行数据归档。 /// </summary> /// <param name="context">表示数据归档的上下文。</param> public void Archive(AggregationArchiveContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } _orignalDataDbSettingName = context.OrignalDataDbSettingName; _backUpDbSettingName = context.BackUpDbSettingName; var updatedTimeCondition = new PropertyMatch(EntityStampExtension.UpdatedTimeProperty, PropertyOperator.LessEqual, context.DateOfArchiving); bool needProgress = this.ProgressChanged != null; if (needProgress) { this.OnProgressChanged("-------------- 开始数据归档 --------------"); } using (StampContext.DisableAutoSetStamps()) { foreach (var item in context.ItemsToArchive) { var aggtRootType = item.AggregationRoot; var archiveType = item.ArchiveType; if (archiveType == ArchiveType.Copy) { throw new NotSupportedException("Copy 操作暂不支持。"); } var repository = RepositoryFacade.Find(aggtRootType); #region 查询本次需要归档的总数据行数。 long totalCount = 0; if (needProgress) { totalCount = repository.CountBy(new CommonQueryCriteria { updatedTimeCondition }); this.OnProgressChanged($"目前,共有 {totalCount} 个聚合根 {aggtRootType.FullName} 需要归档。"); } #endregion #region 构造一个查完整聚合的条件对象 CommonQueryCriteria //设置 EagerLoadOptions,加载整个聚合。 var criteria = new CommonQueryCriteria() { updatedTimeCondition }; criteria.PagingInfo = new PagingInfo(1, context.BatchSize); var eagerLoadOptions = new EagerLoadOptions(); EagerLoadAggregationRecur(eagerLoadOptions, repository.EntityMeta); if (repository.SupportTree) { eagerLoadOptions.LoadWithTreeChildren(); } criteria.EagerLoad = eagerLoadOptions; #endregion //逐页迁移历史数据表。 var currentProcess = 0; while (true) { //获取最新的一页的数据。 var entitiesToMigrate = repository.GetBy(criteria); var count = entitiesToMigrate.Count; if (count == 0) { break; } using (var tranOriginal = RF.TransactionScope(_orignalDataDbSettingName)) using (var tranBackup = RF.TransactionScope(_backUpDbSettingName)) { //迁移到历史表。 this.BackupToHistory(repository, entitiesToMigrate); //实体删除 this.DeleteOriginalData(repository, entitiesToMigrate); //备份完成,才能同时提交两个库的事务。 tranOriginal.Complete(); tranBackup.Complete(); } currentProcess += count; if (needProgress) { this.OnProgressChanged($" 处理进度", decimal.Round(currentProcess / totalCount * 100, 2)); } } } } if (needProgress) { this.OnProgressChanged("-------------- 结束数据归档 --------------"); } }
public void PropertyMatched() { Assert.True(PropertyMatch.Matches("A Matchable Event", "matchable")); }