Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static List<ConexioEntitySyncContext> FindMatches(IEnumerable<TC> conexioEntities,
- List<TE> externalEntities, SynchronizationUnitOfWork unitOfWork)
- {
- List<ConexioEntitySyncContext> result = new List<ConexioEntitySyncContext>();
- int maxScore;
- int usefullScore = typeof(T).GetCustomAttribute<ConexioUsefullScore>().MinScorePercentage;
- Dictionary<TC, int> scores;
- foreach (TE externalEntity in externalEntities)
- {
- maxScore = 0;
- scores = new Dictionary<TC, int>();
- foreach (TC conexioEntity in conexioEntities)
- {
- int matchingScore = GetMatchingScore(externalEntity as T, conexioEntity as T);
- scores.Add(conexioEntity,matchingScore);
- }
- maxScore = scores.Values.Max();
- ConexioEntitySyncContext matchingContext = new ConexioEntitySyncContext();
- if (maxScore > usefullScore)
- {
- TC matchingConexioEntity = scores.Where(s => s.Value == maxScore).FirstOrDefault().Key;
- if (result.Where(c => c.ConexioEntity.Equals(matchingConexioEntity)).FirstOrDefault() == null)
- {
- matchingContext = new ConexioEntitySyncContext();
- matchingContext.ConexioEntity = matchingConexioEntity;
- }
- else
- {
- matchingContext = result.Where(c => c.ConexioEntity.Equals(matchingConexioEntity)).FirstOrDefault();
- }
- matchingContext.ExternalEntities.Add(externalEntity);
- //if (maxScore < usefullScore)
- //{
- // matchingContext.HandledType = HandledType.NotUsefull;
- //}
- externalEntity.ConexioEntity = matchingConexioEntity;
- externalEntity.ConexioID = matchingConexioEntity.ID;
- }
- else
- {
- var newConexioEntity = externalEntity as T;
- var insertConexioEntity = Mapper.Map<TC>(newConexioEntity);
- insertConexioEntity.IsNew = true;
- matchingContext.ConexioEntity = insertConexioEntity;
- matchingContext.ExternalEntities = new List<TE>();
- matchingContext.ExternalEntities.Add(externalEntity);
- externalEntity.ConexioEntity = insertConexioEntity;
- externalEntity.ConexioID = newConexioEntity.ID;
- unitOfWork.RepositoryAsync<TC>().Insert(insertConexioEntity);
- }
- result.Add(matchingContext);
- unitOfWork.RepositoryAsync<TE>().Update(externalEntity);
- }
- //TODO: Fix extra DB entries
- unitOfWork.SaveChanges();
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement