From 82f909726dba308997dfc7c5dd4306a4c455606f Mon Sep 17 00:00:00 2001 From: Felix Obermaier Date: Tue, 16 Mar 2021 14:41:09 +0100 Subject: [PATCH] Clean up * Implement Select, Delete with Predicate argument in non memory implementations * Merge FilterMethod into BusinessObjectProvider * Make TypeUtility internal static * Make _getId and _getGeometry delegates private, use GetGeometry and GetId --- Directory.Build.props | 2 +- .../Business/EF6BusinessObjectSource.cs | 49 +++++++-- .../Business/MongoDbBusinessObjectSource.cs | 25 +++++ ...essBase.cs => BaseBusinessObjectSource.cs} | 31 +++--- .../Business/BusinessObjectAttributes.cs | 13 ++- .../Business/BusinessObjectFilterProvider.cs | 13 --- .../Business/BusinessObjectProvider.cs | 99 ++++++++++--------- .../Business/IBusinessObjectSource.cs | 15 ++- .../Business/InMemoryBusinessObjectSource.cs | 78 ++++++++------- .../Data/Providers/Business/TypeUtility.cs | 18 ++-- .../Layers/BusinessObjectLayer.cs | 71 ------------- .../NHibernating/BusinessObjectSource.cs | 27 ++++- 12 files changed, 236 insertions(+), 205 deletions(-) rename src/SharpMap.BusinessObjects/Data/Providers/Business/{BusinessObjectAccessBase.cs => BaseBusinessObjectSource.cs} (87%) delete mode 100644 src/SharpMap.BusinessObjects/Data/Providers/Business/BusinessObjectFilterProvider.cs diff --git a/Directory.Build.props b/Directory.Build.props index 77c90d4..fd30b7e 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -5,7 +5,7 @@ Copyright 2014-$([System.DateTime]::UtcNow.Year) SharpMap - Team SharpMap - Team - 1701;1702;2100;2235 + 1701;1702;2100;2235;NU5118 true https://github.com/SharpMap/SharpMap.BusinessObjects.git git diff --git a/src/SharpMap.BusinessObjects.EF6/Data/Providers/Business/EF6BusinessObjectSource.cs b/src/SharpMap.BusinessObjects.EF6/Data/Providers/Business/EF6BusinessObjectSource.cs index dc4e8d0..14f615f 100644 --- a/src/SharpMap.BusinessObjects.EF6/Data/Providers/Business/EF6BusinessObjectSource.cs +++ b/src/SharpMap.BusinessObjects.EF6/Data/Providers/Business/EF6BusinessObjectSource.cs @@ -97,12 +97,14 @@ public IGeometryFactory Factory set { _factory = value; } } + /// public override IEnumerable Select(Envelope box) { var envGeom = Factory.ToGeometry(box); return Select(envGeom); } + /// public override IEnumerable Select(IGeometry geom) { var dbGeometry = geom.ToDbGeometry(); @@ -117,6 +119,29 @@ where u.DbGeometry.Intersects(dbGeometry) } + /// + public override IEnumerable Select(Predicate match) + { + using (var c = Context) + { + var qry = from u in c.Set() + where match(u) + select u; + return Select(qry); + } + } + + /// + public override T Select(uint id) + { + using (var c = Context) + { + return c.Set().Find((int)id); + } + } + + + /// public override Envelope GetExtents() { if (CachedExtents == null) @@ -142,32 +167,43 @@ private static Envelope ToEnvelope(IEnumerable dbGeometries) return res; } - public override T Select(uint id) + /// + public override void Update(IEnumerable businessObjects) { using (var c = Context) { - return c.Set().Find((int)id); + c.Set().AddOrUpdate(new List(businessObjects).ToArray()); + c.SaveChanges(); } } - public override void Update(IEnumerable businessObjects) + /// + public override void Delete(IEnumerable businessObjects) { using (var c = Context) { - c.Set().AddOrUpdate(new List(businessObjects).ToArray()); + c.Set().RemoveRange(businessObjects); c.SaveChanges(); } } - public override void Delete(IEnumerable businessObjects) + /// + public override void Delete(Predicate match) + { + Delete(Select(match)); + } + + /// + public override void Insert(T businessObject) { using (var c = Context) { - c.Set().RemoveRange(businessObjects); + c.Set().Add(businessObject); c.SaveChanges(); } } + /// public override void Insert(IEnumerable businessObjects) { using (var c = Context) @@ -177,6 +213,7 @@ public override void Insert(IEnumerable businessObjects) } } + /// public override int Count { get diff --git a/src/SharpMap.BusinessObjects.MongoDB/Data/Providers/Business/MongoDbBusinessObjectSource.cs b/src/SharpMap.BusinessObjects.MongoDB/Data/Providers/Business/MongoDbBusinessObjectSource.cs index c595769..2dd5479 100644 --- a/src/SharpMap.BusinessObjects.MongoDB/Data/Providers/Business/MongoDbBusinessObjectSource.cs +++ b/src/SharpMap.BusinessObjects.MongoDB/Data/Providers/Business/MongoDbBusinessObjectSource.cs @@ -15,8 +15,10 @@ // along with SharpMap; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; using GeoAPI.Geometries; using MongoDB.Bson.Serialization; using MongoDB.Driver; @@ -114,6 +116,12 @@ public override IEnumerable Select(IGeometry geom) return candidates.Where(candidate => p.Intersects(GetGeometry(candidate))); } + /// + public override IEnumerable Select(Predicate match) + { + return _collection.AsQueryable().Where(u => match(u)); + } + /// /// Select a business object by its id /// @@ -138,6 +146,7 @@ public override void Update(IEnumerable businessObjects) } } + /// /// Delete the provided /// @@ -152,6 +161,22 @@ public override void Delete(IEnumerable businessObjects) } } + public override void Delete(Predicate match) + { + var items = _collection.AsQueryable().Where(u => match(u)).Select(t => GetId(t)); + var filter = Builders.Filter.In(t => GetId(t), items); + _collection.DeleteMany(filter); + } + + /// + /// Insert the provided + /// + /// The features that need to be inserted + public override void Insert(T businessObject) + { + _collection.InsertOne(businessObject); + } + /// /// Insert the provided /// diff --git a/src/SharpMap.BusinessObjects/Data/Providers/Business/BusinessObjectAccessBase.cs b/src/SharpMap.BusinessObjects/Data/Providers/Business/BaseBusinessObjectSource.cs similarity index 87% rename from src/SharpMap.BusinessObjects/Data/Providers/Business/BusinessObjectAccessBase.cs rename to src/SharpMap.BusinessObjects/Data/Providers/Business/BaseBusinessObjectSource.cs index cce421d..6ec17f8 100644 --- a/src/SharpMap.BusinessObjects/Data/Providers/Business/BusinessObjectAccessBase.cs +++ b/src/SharpMap.BusinessObjects/Data/Providers/Business/BaseBusinessObjectSource.cs @@ -28,10 +28,10 @@ namespace SharpMap.Data.Providers.Business /// The type of the business object public abstract class BaseBusinessObjectSource : IBusinessObjectSource { -// ReSharper disable InconsistentNaming - protected static readonly TypeUtility.MemberGetDelegate _getId; - protected static TypeUtility.MemberGetDelegate _getGeometry; -// ReSharper restore InconsistentNaming + // ReSharper disable once InconsistentNaming + private static readonly TypeUtility.MemberGetDelegate _getId; + private static readonly TypeUtility.MemberGetDelegate _getGeometry; + // ReSharper restore InconsistentNaming /// /// Static constructor @@ -61,6 +61,13 @@ static BaseBusinessObjectSource() /// public abstract IEnumerable Select(IGeometry geom); + /// + /// Select a set of features based on + /// + /// A predicate function + /// + public abstract IEnumerable Select(Predicate match); + /// /// Select a set of features based on /// @@ -93,11 +100,8 @@ public virtual IEnumerable Select(IQueryable query) /// /// Attribute-based deletion according to provided /// - /// identifying business objects to be deleted - public virtual void Delete(Predicate match) - { - throw new NotImplementedException(); - } + /// identifying business objects to be deleted + public virtual void Delete(Predicate match) => Delete(Select(match)); /// /// Insert the provided @@ -109,10 +113,7 @@ public virtual void Delete(Predicate match) /// Insert provided and expand extents /// /// The business object to be inserted - public virtual void Insert(T businessObject) - { - throw new NotImplementedException(); - } + public abstract void Insert(T businessObject); /// /// Method to get the geometry of a specific feature @@ -162,7 +163,7 @@ public virtual Envelope GetExtents() /// The the first business object matching the predicate public virtual T Find(Predicate match) { - throw new NotImplementedException(); + return FindAll(match).FirstOrDefault(); } /// @@ -172,7 +173,7 @@ public virtual T Find(Predicate match) /// An array of business objects matching the predicate public virtual T[] FindAll(Predicate match) { - throw new NotImplementedException(); + throw new NotSupportedException(); } /// diff --git a/src/SharpMap.BusinessObjects/Data/Providers/Business/BusinessObjectAttributes.cs b/src/SharpMap.BusinessObjects/Data/Providers/Business/BusinessObjectAttributes.cs index edc6635..eba39bb 100644 --- a/src/SharpMap.BusinessObjects/Data/Providers/Business/BusinessObjectAttributes.cs +++ b/src/SharpMap.BusinessObjects/Data/Providers/Business/BusinessObjectAttributes.cs @@ -1,4 +1,4 @@ -// Copyright 2014 - Felix Obermaier (www.ivv-aachen.de) +// Copyright 2014 - Felix Obermaier (www.ivv-aachen.de) // // This file is part of SharpMap.BusinessObjects. // SharpMap.BusinessObjects is free software; you can redistribute it and/or modify @@ -24,11 +24,17 @@ namespace SharpMap.Data.Providers.Business [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] public class BusinessObjectIdentifierAttribute : BusinessObjectAttributeAttribute { + /// + /// Creates an instance of this class setting to 0. + /// public BusinessObjectIdentifierAttribute() { OrdinalValue = 0; } + /// + /// Gets a value indicating if this column/property is unique within the set. + /// public override bool IsUnique { get { return true; } set { throw new NotSupportedException(); } } } @@ -46,6 +52,9 @@ public class BusinessObjectGeometryAttribute : Attribute [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = false)] public class BusinessObjectAttributeAttribute : Attribute { + /// + /// Gets a value indicating the position in the array of properties + /// protected int OrdinalValue = 9999; /// @@ -83,4 +92,4 @@ public class BusinessObjectAttributeAttribute : Attribute public virtual bool Ignore { get; set; } } -} \ No newline at end of file +} diff --git a/src/SharpMap.BusinessObjects/Data/Providers/Business/BusinessObjectFilterProvider.cs b/src/SharpMap.BusinessObjects/Data/Providers/Business/BusinessObjectFilterProvider.cs deleted file mode 100644 index 3938cd5..0000000 --- a/src/SharpMap.BusinessObjects/Data/Providers/Business/BusinessObjectFilterProvider.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace SharpMap.Data.Providers.Business -{ - public abstract class BusinessObjectFilterProvider - { - public delegate bool FilterMethod(object bo); - public FilterMethod FilterDelegate { get; set; } - } -} diff --git a/src/SharpMap.BusinessObjects/Data/Providers/Business/BusinessObjectProvider.cs b/src/SharpMap.BusinessObjects/Data/Providers/Business/BusinessObjectProvider.cs index d8518a0..56b2d61 100644 --- a/src/SharpMap.BusinessObjects/Data/Providers/Business/BusinessObjectProvider.cs +++ b/src/SharpMap.BusinessObjects/Data/Providers/Business/BusinessObjectProvider.cs @@ -1,4 +1,4 @@ -// Copyright 2014 - Felix Obermaier (www.ivv-aachen.de) +// Copyright 2014 - Felix Obermaier (www.ivv-aachen.de) // // This file is part of SharpMap.BusinessObjects. // SharpMap.BusinessObjects is free software; you can redistribute it and/or modify @@ -55,8 +55,13 @@ public static IProvider Create(IBusinessObjectSource source) } } + /// + /// + /// + /// [Serializable] - public class BusinessObjectProvider : BusinessObjectFilterProvider, IProvider + public class BusinessObjectProvider + : IProvider { [NonSerialized] @@ -86,7 +91,7 @@ static void Configure() { foreach (var tuple in GetPublicMembers(typeof(TFeature))) { - var memberName = tuple.Item1; + string memberName = tuple.Item1; var attributes = tuple.Item2; if (attributes.Ignore) continue; @@ -154,16 +159,22 @@ public BusinessObjectProvider(string connectionID, IBusinessObjectSource + /// Dispose this provider + /// public void Dispose() { - if (_source is IDisposable) - ((IDisposable)_source).Dispose(); + if (_source is IDisposable disposable) + disposable.Dispose(); } + /// public string ConnectionID { get; private set; } + /// public bool IsOpen { get; private set; } + /// public int SRID { get; set; } /// @@ -171,32 +182,40 @@ public void Dispose() /// public IBusinessObjectSource Source { get { return _source; } } + private static bool NoConstraint(TFeature feature) => true; + + /// + /// Gets or sets a value indicating a predicate to feature + /// + public Predicate FilterDelegate { get; set; } + + /// public Collection GetGeometriesInView(Envelope bbox) { var res = new Collection(); - foreach (TFeature feature in _source.Select(bbox)) + var match = FilterDelegate ?? NoConstraint; + foreach (var feature in _source.Select(bbox)) { - if (FilterDelegate == null || FilterDelegate(feature)) - { + if (match(feature)) res.Add(_source.GetGeometry(feature)); - } } return res; } + /// public Collection GetObjectIDsInView(Envelope bbox) { var res = new Collection(); - foreach (TFeature feature in _source.Select(bbox)) + var match = FilterDelegate ?? NoConstraint; + foreach (var feature in _source.Select(bbox)) { - if (FilterDelegate == null || FilterDelegate(feature)) - { + if (match(feature)) res.Add(_source.GetId(feature)); - } } return res; } + /// public IGeometry GetGeometryByID(uint oid) { var f = _source.Select(oid); @@ -205,14 +224,15 @@ public IGeometry GetGeometryByID(uint oid) return null; } + /// public void ExecuteIntersectionQuery(IGeometry geom, FeatureDataSet ds) { var resTable = (FeatureDataTable)SchemaTable.Copy(); resTable.BeginLoadData(); + var match = FilterDelegate ?? NoConstraint; foreach (var feature in _source.Select(geom)) { - if (FilterDelegate == null || FilterDelegate(feature)) - { + if (match(feature)) { var fdr = (FeatureDataRow)resTable.LoadDataRow(ToItemArray(feature), LoadOption.OverwriteChanges); fdr.Geometry = _source.GetGeometry(feature); } @@ -221,6 +241,7 @@ public void ExecuteIntersectionQuery(IGeometry geom, FeatureDataSet ds) ds.Tables.Add(resTable); } + /// public void ExecuteIntersectionQuery(Envelope box, FeatureDataSet ds) { var resTable = (FeatureDataTable)SchemaTable.Copy(); @@ -238,29 +259,11 @@ public void ExecuteIntersectionQuery(Envelope box, FeatureDataSet ds) ds.Tables.Add(resTable); } - //private static FeatureDataTable Copy(FeatureDataTable schemaTable) - //{ - // var res = new FeatureDataTable(); - // res.TableName = schemaTable.TableName; - // foreach (DataColumn dc in schemaTable.Columns) - // { - // var ndc = res.Columns.Add(dc.ColumnName, dc.DataType); - // ndc.AllowDBNull = dc.AllowDBNull; - // ndc.AutoIncrement = dc.AutoIncrement; - // ndc.AutoIncrementSeed = dc.AutoIncrementSeed; - // ndc.AutoIncrementStep = dc.AutoIncrementStep; - // ndc.Caption = dc.Caption; - // ndc.ColumnMapping = dc.ColumnMapping; - // ndc.DateTimeMode = dc.DateTimeMode; - // ndc.DefaultValue = dc.DefaultValue; - // ndc.MaxLength = dc.MaxLength; - // //ndc.Ordinal = dc.Ordinal; - // ndc.ReadOnly = dc.ReadOnly; - // ndc.Unique = dc.Unique; - // } - // return res; - //} - + /// + /// + /// + /// + /// public void ExecuteQueryable(IQueryable query, FeatureDataSet ds) { var resTable = (FeatureDataTable)SchemaTable.Copy(); @@ -274,14 +277,16 @@ public void ExecuteQueryable(IQueryable query, FeatureDataSet ds) ds.Tables.Add(resTable); } + /// public int GetFeatureCount() { return _source.Count; } + /// public FeatureDataRow GetFeature(uint rowId) { - var fdr = (FeatureDataRow)SchemaTable.NewRow(); + var fdr = SchemaTable.NewRow(); var f = _source.Select(rowId); fdr.ItemArray = ToItemArray(f); fdr.Geometry = _source.GetGeometry(f); @@ -290,28 +295,30 @@ public FeatureDataRow GetFeature(uint rowId) private static object[] ToItemArray(TFeature feature) { - var items = new List(); - foreach (var d in GetDelegates) - { - items.Add(d(feature)); - } - return items.ToArray(); + var items = new object[GetDelegates.Count]; + for (int i = 0; i < GetDelegates.Count; i++) + items[i] = GetDelegates[i](feature); + + return items; } + /// public Envelope GetExtents() { return _source.GetExtents(); } + /// public void Open() { IsOpen = true; } + /// public void Close() { IsOpen = false; } } -} \ No newline at end of file +} diff --git a/src/SharpMap.BusinessObjects/Data/Providers/Business/IBusinessObjectSource.cs b/src/SharpMap.BusinessObjects/Data/Providers/Business/IBusinessObjectSource.cs index 260d620..f84057c 100644 --- a/src/SharpMap.BusinessObjects/Data/Providers/Business/IBusinessObjectSource.cs +++ b/src/SharpMap.BusinessObjects/Data/Providers/Business/IBusinessObjectSource.cs @@ -1,4 +1,4 @@ -// Copyright 2013 - 2014 Felix Obermaier (www.ivv-aachen.de) +// Copyright 2013 - 2014 Felix Obermaier (www.ivv-aachen.de) // // This file is part of SharpMap.Data.Providers.Business. // SharpMap.Data.Providers.Business is free software; you can redistribute it and/or modify @@ -47,6 +47,13 @@ public interface IBusinessObjectSource /// A series of business objects IEnumerable Select(IGeometry geom); + /// + /// Select a set of features based on + /// + /// A predicate + /// A series of business objects + IEnumerable Select(Predicate match); + /// /// Select a set of features based on /// @@ -76,7 +83,7 @@ public interface IBusinessObjectSource /// /// Attribute-based deletion according to provided /// - /// identifying business objects to be deleted + /// A identifying business objects to be deleted void Delete(Predicate match); /// @@ -104,7 +111,7 @@ public interface IBusinessObjectSource /// The business object /// The id uint GetId(T businessObject); - + /// /// Gets the number of business objects in the store /// @@ -137,4 +144,4 @@ public interface IBusinessObjectSource T[] AsReadOnly(); } -} \ No newline at end of file +} diff --git a/src/SharpMap.BusinessObjects/Data/Providers/Business/InMemoryBusinessObjectSource.cs b/src/SharpMap.BusinessObjects/Data/Providers/Business/InMemoryBusinessObjectSource.cs index 51ae2bf..08ba24e 100644 --- a/src/SharpMap.BusinessObjects/Data/Providers/Business/InMemoryBusinessObjectSource.cs +++ b/src/SharpMap.BusinessObjects/Data/Providers/Business/InMemoryBusinessObjectSource.cs @@ -1,4 +1,4 @@ -// Copyright 2013 - 2014 Felix Obermaier (www.ivv-aachen.de) +// Copyright 2013 - 2014 Felix Obermaier (www.ivv-aachen.de) // // This file is part of SharpMap.Data.Providers.Business. // SharpMap.Data.Providers.Business is free software; you can redistribute it and/or modify @@ -16,10 +16,11 @@ // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA using System; +using System.Linq; using System.Collections.Generic; using GeoAPI.Geometries; -using NetTopologySuite.Geometries; using System.Collections; +using NetTopologySuite; namespace SharpMap.Data.Providers.Business { @@ -32,6 +33,7 @@ public class InMemoryBusinessObjectSource : BaseBusinessObjectSource private readonly Dictionary _businessObjects; private readonly string _title; + private IGeometryFactory _factory; /// /// Creates an instance of this class @@ -55,18 +57,6 @@ public override string Title { get { return _title; } } - ///// - ///// Collection of business objects for querying using Select(IQueryable query) - ///// or to directly edit attributes of one or more objects - ///// - //public ICollection Context - //{ - // get - // { - // lock (((ICollection)_businessObjects).SyncRoot) - // return _businessObjects.Values; - // } - //} /// /// Select a set of features based on @@ -75,7 +65,7 @@ public override string Title /// public override IEnumerable Select(Envelope box) { - return Select(new GeometryFactory().ToGeometry(box)); + return Select((_factory ?? NtsGeometryServices.Instance.CreateGeometryFactory()).ToGeometry(box)); } /// @@ -91,7 +81,7 @@ public override IEnumerable Select(IGeometry geom) { foreach (T value in _businessObjects.Values) { - var g = _getGeometry(value); + var g = GetGeometry(value); if (g != null && prep.Intersects(g)) { yield return value; @@ -100,6 +90,19 @@ public override IEnumerable Select(IGeometry geom) } } + /// + public override IEnumerable Select(Predicate match) + { + if (_businessObjects == null) + return Array.Empty(); + + var res = new List(); + lock (((ICollection)_businessObjects).SyncRoot) + res.AddRange(_businessObjects?.Values.Where(u => match(u))); + + return res; + } + /// /// Select a feature by its id /// @@ -108,10 +111,9 @@ public override IEnumerable Select(IGeometry geom) /// public override T Select(uint id) { - T res; lock (((ICollection)_businessObjects).SyncRoot) { - if (_businessObjects.TryGetValue(id, out res)) + if (_businessObjects.TryGetValue(id, out var res)) return res; } throw new ArgumentException("No feature with this id", id.ToString()); @@ -127,9 +129,9 @@ public override void Update(IEnumerable features) lock (((ICollection)_businessObjects).SyncRoot) { - foreach (T feature in features) + foreach (var feature in features) { - _businessObjects.Add(_getId(feature), feature); + _businessObjects.Add(GetId(feature), feature); } } } @@ -142,9 +144,9 @@ public override void Delete(IEnumerable features) { lock (((ICollection)_businessObjects).SyncRoot) { - foreach (T feature in features) + foreach (var feature in features) { - _businessObjects.Remove(_getId(feature)); + _businessObjects.Remove(GetId(feature)); } CachedExtents = null; } @@ -153,14 +155,14 @@ public override void Delete(IEnumerable features) /// /// Attribute-based deletion according to provided /// - /// identifying business objects to be deleted + /// identifying business objects to be deleted public override void Delete(Predicate match) { lock (((ICollection)_businessObjects).SyncRoot) { - foreach (T bo in FindAll(match)) + foreach (var bo in FindAll(match)) { - _businessObjects.Remove(_getId(bo)); + _businessObjects.Remove(GetId(bo) ); } CachedExtents = null; } @@ -182,6 +184,9 @@ public void Delete(IEnumerable oids) } } + /// + /// Clears the cache + /// public void Clear() { lock (((ICollection)_businessObjects).SyncRoot) @@ -200,9 +205,12 @@ public override void Insert(IEnumerable features) { lock (((ICollection)_businessObjects).SyncRoot) { - foreach (T feature in features) + foreach (var feature in features) { - _businessObjects.Add(_getId(feature), feature); + if (_factory == null) + _factory = GetGeometry(feature).Factory; + + _businessObjects.Add(GetId(feature), feature); } CachedExtents = null; } @@ -216,24 +224,19 @@ public override void Insert(T businessObject) { lock (((ICollection)_businessObjects).SyncRoot) { - _businessObjects.Add(_getId(businessObject), businessObject); + _businessObjects.Add(GetId(businessObject), businessObject); // expand to include var res = CachedExtents ?? new Envelope(); - var g = _getGeometry(businessObject); + var g = GetGeometry(businessObject); if (g != null && !g.IsEmpty) res.ExpandToInclude(g.EnvelopeInternal); CachedExtents = res; } } - [Obsolete("Use Insert(T businessObject)")] - public void InsertFeature(T feature) - { - Insert(feature); - } - + /// public override int Count { get @@ -243,6 +246,7 @@ public override int Count } } + /// public override Envelope GetExtents() { return CachedExtents ?? (CachedExtents = ComputeExtents()); @@ -255,7 +259,7 @@ private Envelope ComputeExtents() var res = new Envelope(); foreach (var bo in _businessObjects.Values) { - var g = _getGeometry(bo); + var g = GetGeometry(bo); if (g != null && !g.IsEmpty) res.ExpandToInclude(g.EnvelopeInternal); } @@ -311,4 +315,4 @@ public override T[] AsReadOnly() } } -} \ No newline at end of file +} diff --git a/src/SharpMap.BusinessObjects/Data/Providers/Business/TypeUtility.cs b/src/SharpMap.BusinessObjects/Data/Providers/Business/TypeUtility.cs index 88b6381..a17a402 100644 --- a/src/SharpMap.BusinessObjects/Data/Providers/Business/TypeUtility.cs +++ b/src/SharpMap.BusinessObjects/Data/Providers/Business/TypeUtility.cs @@ -1,4 +1,4 @@ -/* +/* * Copyright © 2013 - Felix Obermaier, Ingenieurgruppe IVV GmbH & Co. KG * * This file is part of SharpMap.BusinessObjects. @@ -27,13 +27,17 @@ namespace SharpMap.Data.Providers.Business { - public class TypeUtility + /// + /// A utility class to help with feature objects + /// + /// + internal static class TypeUtility { public delegate TMemberType MemberGetDelegate(TObjectType obj); - internal delegate object MemberGetDelegate(TObjectType obj); + public delegate object MemberGetDelegate(TObjectType obj); - internal static MemberGetDelegate GetMemberGetDelegate(string memberName) + public static MemberGetDelegate GetMemberGetDelegate(string memberName) { var objectType = typeof(TObjectType); @@ -52,7 +56,7 @@ internal static MemberGetDelegate GetMemberGetDelegate return (MemberGetDelegate) Delegate.CreateDelegate(typeof(MemberGetDelegate), mi); } - throw new Exception(String.Format( + throw new Exception(string.Format( "Property: '{0}' of Type: '{1}' does" + " not have a Public Get accessor", memberName, objectType.Name)); @@ -117,7 +121,7 @@ public static Type GetMemberType(string memberName) } } - internal class TypeUtility + internal static class TypeUtility { public static Type GetMemberType(Type objectType, string memberName) { @@ -139,4 +143,4 @@ public static Type GetMemberType(Type objectType, string memberName) throw new Exception("Member '" + memberName + "' not found in type '"+ objectType.Name + "'!"); } } -} \ No newline at end of file +} diff --git a/src/SharpMap.BusinessObjects/Layers/BusinessObjectLayer.cs b/src/SharpMap.BusinessObjects/Layers/BusinessObjectLayer.cs index 3f7312e..b0150ff 100644 --- a/src/SharpMap.BusinessObjects/Layers/BusinessObjectLayer.cs +++ b/src/SharpMap.BusinessObjects/Layers/BusinessObjectLayer.cs @@ -39,7 +39,6 @@ public class BusinessObjectLayer : Layer, ICanQueryLayer private IBusinessObjectSource _source; private IBusinessObjectRenderer _businessObjectRenderer; - private IGeometryFactory _targetFactory; [NonSerialized] private IProvider _provider; @@ -138,27 +137,6 @@ protected virtual void OnRendererChanged(EventArgs e) RendererChanged?.Invoke(this, e); } - /// - public override ICoordinateTransformation CoordinateTransformation - { - get - { - return base.CoordinateTransformation; - } - set - { - if (value == CoordinateTransformation) - return; - - if (value == null) - _targetFactory = null; - else - GeoAPI.GeometryServiceProvider.Instance.CreateGeometryFactory(Convert.ToInt32(value.TargetCS.AuthorityCode)); - - base.CoordinateTransformation = value; - } - } - /// /// Gets a provider /// @@ -306,55 +284,6 @@ public void ExecuteIntersectionQuery(IGeometry geometry, FeatureDataSet ds) /// public bool IsQueryEnabled { get; set; } -#if !SharpMap_v1_2 - protected Envelope ToTarget(Envelope envelope) - { - if (CoordinateTransformation == null) - return envelope; -#if !DotSpatialProjections - return GeometryTransform.TransformBox(envelope, CoordinateTransformation.MathTransform); -#else - return GeometryTransform.TransformBox(box, CoordinateTransformation.Source, CoordinateTransformation.Target); -#endif - } - - protected IGeometry ToTarget(IGeometry geometry) - { - if (CoordinateTransformation == null) - return geometry; -#if !DotSpatialProjections - return GeometryTransform.TransformGeometry(geometry, CoordinateTransformation.MathTransform, geometry.Factory); -#else - return GeometryTransform.TransformGeometry(geometry, CoordinateTransformation.Source, CoordinateTransformation.Target, targetFactory); -#endif - } - - protected Envelope ToSource(Envelope envelope) - { - if (ReverseCoordinateTransformation == null) - { - if (CoordinateTransformation == null) - return envelope; - - var mt = CoordinateTransformation.MathTransform; -#if !DotSpatialProjections - mt.Invert(); - var res = GeometryTransform.TransformBox(envelope, mt); - mt.Invert(); -#else - return GeometryTransform.TransformBox(envelope, CoordinateTransformation.Target, CoordinateTransformation.Source); -#endif - return res; - } - -#if !DotSpatialProjections - return GeometryTransform.TransformBox(envelope, ReverseCoordinateTransformation.MathTransform); -#else - return GeometryTransform.TransformBox(box, ReverseCoordinateTransformation.Source, ReverseCoordinateTransformation.Target); -#endif - } -#endif - /// /// Method to set after deserialization /// diff --git a/test/SharpMap.BusinessObjects.Tests/NHibernating/BusinessObjectSource.cs b/test/SharpMap.BusinessObjects.Tests/NHibernating/BusinessObjectSource.cs index baf70a4..1d24b6f 100644 --- a/test/SharpMap.BusinessObjects.Tests/NHibernating/BusinessObjectSource.cs +++ b/test/SharpMap.BusinessObjects.Tests/NHibernating/BusinessObjectSource.cs @@ -1,7 +1,10 @@ +using System; using System.Collections.Generic; +using System.Linq; using GeoAPI.Geometries; using NHibernate; using NHibernate.Criterion; +using NHibernate.Loader; using SharpMap.Data.Providers.Business; namespace SharpMap.Business.Tests.NHibernating @@ -31,7 +34,7 @@ public override IEnumerable Select(Envelope box) { foreach (var bo in session.CreateCriteria().List()) { - if (box.Intersects(_getGeometry(bo).EnvelopeInternal)) + if (box.Intersects(GetGeometry(bo).EnvelopeInternal)) yield return bo; } } @@ -45,12 +48,22 @@ public override IEnumerable Select(IGeometry geom) { foreach (var bo in session.CreateCriteria().List()) { - if (p.Intersects(_getGeometry(bo))) + if (p.Intersects(GetGeometry(bo))) yield return bo; } } } + public override IEnumerable Select(Predicate match) + { + using (var session = GetSession()) + { + + foreach (var bo in session.CreateCriteria().List().Where(u => match(u))) + yield return bo; + } + } + public override T Select(uint id) { using (var session = GetSession()) @@ -85,6 +98,14 @@ public override void Delete(IEnumerable businessObjects) } } + public override void Insert(T businessObject) + { + using (var session = GetSession()) + { + session.SaveOrUpdate(businessObject); + } + } + public override void Insert(IEnumerable businessObjects) { using (var session = GetSession()) @@ -121,7 +142,7 @@ public override Envelope GetExtents() { foreach (var bo in session.CreateCriteria().List()) { - _cached.ExpandToInclude(_getGeometry(bo).EnvelopeInternal); + _cached.ExpandToInclude(GetGeometry(bo).EnvelopeInternal); } } }