public IEnumerable <Frame> Search(string projectName, ICriterion criterion) { var converter = new FrameDomainEntityConverter(); using (var context = ContextFactory.GetContext(projectName)) { var query = context.FrameEntities.AsExpandable(); var visitor = new FrameCriterionVisitor(); var exp = (Expression <Func <FrameEntity, bool> >)criterion.Accept(visitor); var frames = query.Where(exp).ToList(); var converted = frames.Select(x => converter.ToDomain(x, context)).ToList(); return(converted); } }
/// <summary> /// Gets the frame. /// </summary> /// <param name="projectName">Name of the project.</param> /// <param name="position">The position.</param> /// <param name="threadId">The thread identifier.</param> /// <returns>Frame.</returns> /// <exception cref="IndexOutOfRangeException"></exception> public Frame GetFrame(string projectName, Position position, int threadId) { using (var context = ContextFactory.GetContext(projectName)) { var first = context.FrameEntities.FirstOrDefault(x => x.PosHi == position.High && x.PosLo == position.Low && x.ThreadId == threadId); if (first == null) { throw new IndexOutOfRangeException( $"Count not find a frame with position: {position} and threadid: {threadId}"); } var converter = new FrameDomainEntityConverter(); return(converter.ToDomain(first, context)); } }
/// <summary> /// Upserts the frame. /// </summary> /// <param name="projectName">Name of the project.</param> /// <param name="frames">The frames.</param> public void UpsertFrames(string projectName, IEnumerable <Frame> frames) { frames = frames.ToList(); using (var context = ContextFactory.GetContext(projectName)) { foreach (var frame in frames) { var converter = new FrameDomainEntityConverter(); var source = converter.ToEntity(frame, context); var target = context.FrameEntities.FirstOrDefault(x => x.PosHi == source.PosHi && x.PosLo == source.PosLo && x.ThreadId == source.ThreadId); if (target != null) { CopyValues(source, target); } else { context.FrameEntities.Add(source); } } try { context.SaveChanges(); } catch (DbEntityValidationException e) { var sb = new StringBuilder(); var errors = e.EntityValidationErrors.SelectMany(x => x.ValidationErrors) .Select(x => $"Validation Error: {x.PropertyName} - {x.ErrorMessage}"); sb.AppendLine($"There were validation errors when trying to persist the request:"); var message = String.Join(Environment.NewLine, errors); sb.AppendLine(message); throw new ApplicationException(sb.ToString()); } } }