using System; using System.Collections.Generic; using System.Linq; using Application.Ringtoets.Storage.DbContext; using Core.Common.Base.Geometry; using Ringtoets.Common.Data; namespace Application.Ringtoets.Storage.Converters { /// /// This class is able to convert a model to a /// entity and back. /// public class ReferenceLineConverter : IEntityConverter> { /// /// Creates a new from the . /// /// Collection of containing /// the geometry of the . /// A new instance with its geometry taken from the /// . public ReferenceLine ConvertEntityToModel(ICollection entityCollection) { if (entityCollection == null) { throw new ArgumentNullException("entityCollection"); } var line = new ReferenceLine(); var geometry = entityCollection.Select(entity => new Point2D(decimal.ToDouble(entity.X), decimal.ToDouble(entity.Y))); line.SetGeometry(geometry); return line; } /// /// Updates the by adding the geometry from to it. /// /// The used as source. /// The target collection. public void ConvertModelToEntity(ReferenceLine modelObject, ICollection entityCollection) { if (modelObject == null) { throw new ArgumentNullException("modelObject"); } if (entityCollection == null) { throw new ArgumentNullException("entityCollection"); } entityCollection.Clear(); foreach (Point2D point in modelObject.Points) { entityCollection.Add(new ReferenceLinePointEntity { X = Convert.ToDecimal(point.X), Y = Convert.ToDecimal(point.Y), Order = entityCollection.Count }); } } } }