Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
* Implement Select, Delete with Predicate<T> argument in non memory implementations
* Merge FilterMethod into BusinessObjectProvider
* Make TypeUtility internal static
* Make _getId and _getGeometry delegates private, use GetGeometry and GetId
  • Loading branch information
FObermaier committed Mar 16, 2021
1 parent 1340ff5 commit 82f9097
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 205 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<Copyright>Copyright 2014-$([System.DateTime]::UtcNow.Year) SharpMap - Team</Copyright>
<Authors>SharpMap - Team</Authors>
<NoWarn>1701;1702;2100;2235</NoWarn>
<NoWarn>1701;1702;2100;2235;NU5118</NoWarn>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<RepositoryUrl>https://github.com/SharpMap/SharpMap.BusinessObjects.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ public IGeometryFactory Factory
set { _factory = value; }
}

/// <inheritdoc />
public override IEnumerable<T> Select(Envelope box)
{
var envGeom = Factory.ToGeometry(box);
return Select(envGeom);
}

/// <inheritdoc />
public override IEnumerable<T> Select(IGeometry geom)
{
var dbGeometry = geom.ToDbGeometry();
Expand All @@ -117,6 +119,29 @@ where u.DbGeometry.Intersects(dbGeometry)

}

/// <inheritdoc />
public override IEnumerable<T> Select(Predicate<T> match)
{
using (var c = Context)
{
var qry = from u in c.Set<T>()
where match(u)
select u;
return Select(qry);
}
}

/// <inheritdoc />
public override T Select(uint id)
{
using (var c = Context)
{
return c.Set<T>().Find((int)id);
}
}


/// <inheritdoc />
public override Envelope GetExtents()
{
if (CachedExtents == null)
Expand All @@ -142,32 +167,43 @@ private static Envelope ToEnvelope(IEnumerable<DbGeometry> dbGeometries)
return res;
}

public override T Select(uint id)
/// <inheritdoc />
public override void Update(IEnumerable<T> businessObjects)
{
using (var c = Context)
{
return c.Set<T>().Find((int)id);
c.Set<T>().AddOrUpdate(new List<T>(businessObjects).ToArray());
c.SaveChanges();
}
}

public override void Update(IEnumerable<T> businessObjects)
/// <inheritdoc />
public override void Delete(IEnumerable<T> businessObjects)
{
using (var c = Context)
{
c.Set<T>().AddOrUpdate(new List<T>(businessObjects).ToArray());
c.Set<T>().RemoveRange(businessObjects);
c.SaveChanges();
}
}

public override void Delete(IEnumerable<T> businessObjects)
/// <inheritdoc />
public override void Delete(Predicate<T> match)
{
Delete(Select(match));
}

/// <inheritdoc />
public override void Insert(T businessObject)
{
using (var c = Context)
{
c.Set<T>().RemoveRange(businessObjects);
c.Set<T>().Add(businessObject);
c.SaveChanges();
}
}

/// <inheritdoc />
public override void Insert(IEnumerable<T> businessObjects)
{
using (var c = Context)
Expand All @@ -177,6 +213,7 @@ public override void Insert(IEnumerable<T> businessObjects)
}
}

/// <inheritdoc />
public override int Count
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -114,6 +116,12 @@ public override IEnumerable<T> Select(IGeometry geom)
return candidates.Where(candidate => p.Intersects(GetGeometry(candidate)));
}

/// <inheritdoc />
public override IEnumerable<T> Select(Predicate<T> match)
{
return _collection.AsQueryable().Where(u => match(u));
}

/// <summary>
/// Select a business object by its id
/// </summary>
Expand All @@ -138,6 +146,7 @@ public override void Update(IEnumerable<T> businessObjects)
}
}


/// <summary>
/// Delete the provided <paramref name="businessObjects"/>
/// </summary>
Expand All @@ -152,6 +161,22 @@ public override void Delete(IEnumerable<T> businessObjects)
}
}

public override void Delete(Predicate<T> match)
{
var items = _collection.AsQueryable().Where(u => match(u)).Select(t => GetId(t));
var filter = Builders<T>.Filter.In(t => GetId(t), items);
_collection.DeleteMany(filter);
}

/// <summary>
/// Insert the provided <paramref name="businessObject"/>
/// </summary>
/// <param name="businessObject">The features that need to be inserted</param>
public override void Insert(T businessObject)
{
_collection.InsertOne(businessObject);
}

/// <summary>
/// Insert the provided <paramref name="businessObjects"/>
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ namespace SharpMap.Data.Providers.Business
/// <typeparam name="T">The type of the business object</typeparam>
public abstract class BaseBusinessObjectSource<T> : IBusinessObjectSource<T>
{
// ReSharper disable InconsistentNaming
protected static readonly TypeUtility<T>.MemberGetDelegate<uint> _getId;
protected static TypeUtility<T>.MemberGetDelegate<IGeometry> _getGeometry;
// ReSharper restore InconsistentNaming
// ReSharper disable once InconsistentNaming
private static readonly TypeUtility<T>.MemberGetDelegate<uint> _getId;
private static readonly TypeUtility<T>.MemberGetDelegate<IGeometry> _getGeometry;
// ReSharper restore InconsistentNaming

/// <summary>
/// Static constructor
Expand Down Expand Up @@ -61,6 +61,13 @@ static BaseBusinessObjectSource()
/// <returns></returns>
public abstract IEnumerable<T> Select(IGeometry geom);

/// <summary>
/// Select a set of features based on <paramref name="match"/>
/// </summary>
/// <param name="match">A predicate function</param>
/// <returns></returns>
public abstract IEnumerable<T> Select(Predicate<T> match);

/// <summary>
/// Select a set of features based on <paramref name="query"/>
/// </summary>
Expand Down Expand Up @@ -93,11 +100,8 @@ public virtual IEnumerable<T> Select(IQueryable<T> query)
/// <summary>
/// Attribute-based deletion according to provided <paramref name="match"/>
/// </summary>
/// <param name="match"><typeparamref name="Predicate<T>"/> identifying business objects to be deleted</param>
public virtual void Delete(Predicate<T> match)
{
throw new NotImplementedException();
}
/// <param name="match"><typeparamref name="T"/> identifying business objects to be deleted</param>
public virtual void Delete(Predicate<T> match) => Delete(Select(match));

/// <summary>
/// Insert the provided <paramref name="businessObjects"/>
Expand All @@ -109,10 +113,7 @@ public virtual void Delete(Predicate<T> match)
/// Insert provided <paramref name="businessObject"/> and expand extents
/// </summary>
/// <param name="businessObject">The business object to be inserted</param>
public virtual void Insert(T businessObject)
{
throw new NotImplementedException();
}
public abstract void Insert(T businessObject);

/// <summary>
/// Method to get the geometry of a specific feature
Expand Down Expand Up @@ -162,7 +163,7 @@ public virtual Envelope GetExtents()
/// <returns>The the first business object matching the predicate</returns>
public virtual T Find(Predicate<T> match)
{
throw new NotImplementedException();
return FindAll(match).FirstOrDefault();
}

/// <summary>
Expand All @@ -172,7 +173,7 @@ public virtual T Find(Predicate<T> match)
/// <returns>An array of business objects matching the predicate</returns>
public virtual T[] FindAll(Predicate<T> match)
{
throw new NotImplementedException();
throw new NotSupportedException();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,11 +24,17 @@ namespace SharpMap.Data.Providers.Business
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class BusinessObjectIdentifierAttribute : BusinessObjectAttributeAttribute
{
/// <summary>
/// Creates an instance of this class setting <see cref="BusinessObjectAttributeAttribute.OrdinalValue"/> to <c>0</c>.
/// </summary>
public BusinessObjectIdentifierAttribute()
{
OrdinalValue = 0;
}

/// <summary>
/// Gets a value indicating if this column/property is unique within the set.
/// </summary>
public override bool IsUnique { get { return true; } set { throw new NotSupportedException(); } }
}

Expand All @@ -46,6 +52,9 @@ public class BusinessObjectGeometryAttribute : Attribute
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = false)]
public class BusinessObjectAttributeAttribute : Attribute
{
/// <summary>
/// Gets a value indicating the position in the array of properties
/// </summary>
protected int OrdinalValue = 9999;

/// <summary>
Expand Down Expand Up @@ -83,4 +92,4 @@ public class BusinessObjectAttributeAttribute : Attribute
public virtual bool Ignore { get; set; }
}

}
}

This file was deleted.

Loading

0 comments on commit 82f9097

Please sign in to comment.