public IBox <TM> ToBox() { if (Entries == null) { throw new InvalidOperationException("Entries has not been set"); } EnsureTypesAreValid(); var converter = new NHibernateBoxConverter <TE, TM>(_repoFinder); var box = converter.ToBox(Entries); //custom boxing for (int i = 0; i < box.Count(); i++) { CustomBoxing(Entries.Skip(i).First(), box.Skip(i).First()); } return(box); }
public void Transform(IBox <TM> box) { EnsureTypesAreValid(); var converter = new NHibernateBoxConverter <TE, TM>(_repoFinder); var entries = converter.ToEntity(box); var entriez = new List <TE>(); //persist (set tu null which to skip) var repo = _repoFinder.CreateRepository <TE>(); foreach (var entry in entries) { var e = FindExistingEntity.Invoke(repo, entry); if (e != null) { switch (ImportStrategy) { case BoxImporterStrategy.ErrorExisting: throw new Exception("Error strategy fired"); break; case BoxImporterStrategy.SkipExisting: _log.Debug(e.Id + " skipped"); entriez.Add(null); break; case BoxImporterStrategy.UpdateExisting: var mapper = ObjectMapperManager.DefaultInstance .GetMapper <TE, TE>(new DefaultMapConfig() .IgnoreMembers <TE, TE>(new[] { "Id" })); mapper.Map(entry, e); var entryx = repo.Update(e); _log.Debug(entryx.Id + " updated"); entriez.Add(entryx); break; case BoxImporterStrategy.RewriteExisting: repo.Delete(e); entry.Id = 0; var entryy = repo.Save(entry); _log.Debug(entryy.Id + " rewritten"); entriez.Add(entryy); break; default: throw new Exception("Unknown import strategy"); break; } } else { entry.Id = 0; var entryy = repo.Save(entry); entriez.Add(entryy); } } if (box.Count() != entriez.Count) { throw new InvalidOperationException("Entyty vs Box count mismatch"); } var result = new List <TE>(); //custom unboxing for (int i = 0; i < box.Count(); i++) { if (entriez[i] != null) { CustomUnboxing(entriez[i], box.Skip(i).First()); result.Add(entriez[i]); } else { result.Add(entries[i]); } } Entries = result; }