Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
Adding info to README.INFO
  • Loading branch information
FObermaier committed Apr 10, 2017
1 parent 608d2c7 commit e3e4850
Show file tree
Hide file tree
Showing 32 changed files with 439 additions and 252 deletions.
87 changes: 86 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,87 @@
# SharpMap.BusinessObjects
SharpMap with business objects
With the help of this library you can use [`SharpMap`](https://github.com/SharpMap/SharpMap) with your business objects.
To achieve this you have to
1. Prepare your business objects by decorating fields and properties with some attributes (`BusinessObjectIdentifierAttribute`, `BusinessObjectGeometryAttribute`, `BusinessObjectAttributeAttribute`)
2. Create a `IBusinessObjectSource<T>`. There is an implementations for business objects you have in-memory (`InMemoryBusinessObjectSource<T>`) and example implementations for use with either [MongoDB](https://www.mongodb.com) or [Entity Framework 6](https://docs.microsoft.com/en-us/ef/ef6/). In the test project an example for using [NHibernate](http://nhibernate.info) is given, too.
3. Set up a `BusinessObjectProvider<T>` based on the the business object source you created above.
4. You can use that with
* the standard `VectorLayer` and `LabelLayer` or
* define a special `BusinessObjectLayer<T>` that allows for a special `IBusinessObjectRenderer<T>`.


### Prepare business objects
For your business objects you need to assign the following attributes:
* `BusinessObjectIdentifierAttribute`
Assign this attribute to the property or field that is the unique identifier for the business object.
It must be of `System.UInt32` type and must not be assigned more than once.
* `BusinessObjectGeometryAttribute`
Assign this attribute to the property or field that contains the spatial component of your business object. The type of the field must be `GeoAPI.Geometries.IGeometry`. It must not be assigned more than once.
* `BusinessObjectAttributeAttribute`
Assign this attribute to all other properties or fields relevant for query by `SharpMap.Layers.ICanQueryLayer.ExecuteIntersectionQuery`

As an example have a look at this simple point of interest class:
```C#
/// <summary>
/// A simple point of interest class
/// </summary>
public class PointOfInterest
{
/// <summary>
/// Gets or sets a value indicating the unique identifier
/// </summary>
[BusinessObjectIdentifier()]
public uint ID { get; set; }

/// <summary>
/// Gets or sets a value indicating the geometry
/// </summary>
[BusinessObjectGeometry()]
public IGeometry Geometry { get; set; }

/// <summary>
/// Gets or sets a value indicating the kind of PoI
/// </summary>
[BusinessObjectAttribute()]
public string Kind { get; set; }

/// <summary>
/// Gets or sets a value indicating the address of the PoI
/// </summary>
[BusinessObjectAttribute()]
public string Address { get; set; }

/// <summary>
/// Gets or sets a value indicating some comments associated with the PoI
/// </summary>
[BusinessObjectAttribute(AllowNull = true)]
public List<string> Comments { get; set; }
}

```


### Business object source and provider

Continuing the previous example, we simply create an in in memory object source and build a provider for it.
```C#
IEnumerable<PointOfInterest> pois = ...;

// Create source and insert items
var poiSource = new InMemoryBusinessObjectSource<PointOfInterest>();
poiSource.Insert(pois);

// Create provider
var poiProvider = new BusinessObjectProvider<PointOfInterest>(poiSource);
```

### Use of the provider in layers
You can simply use your provider along with a standard Vector- and/or LabelLayer.
```C#
var vl = new VectorLayer(poiProvider.ConnectionID, poiProvider);
```
Doing so, you have all styling and theming options you would have using those layers along with the standard data sources.
Using a `BusinessObjectLayer<T>` you can provide a custom `IBusinessObjectRenderer<T>` that has special knowledge on how to render the business objects.
An example for such a renderer is shown in the [`LinkWithLoad`](https://github.com/SharpMap/SharpMap.BusinessObjects/blob/master/src/SharpMap.BusinessObjects.Tests/Memory/LinkLoad.cs#L35) example.
Not providing a renderer has the same effect as using the `VectorLayer`.


Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,51 @@
using System.Linq;
using System.Reflection;
using GeoAPI.Geometries;
using NetTopologySuite.IO;

namespace SharpMap.Data.Providers.Business
{
public class EF6BusinessObjectRepository
/// <summary>
/// A utility class to initialize
/// </summary>
public class EF6BusinessObjectSource
{
private static bool Initialized;
private static bool _initialized;

internal static void Configure()
{
if (!Initialized)
if (!_initialized)
{
Initialized = true;
_initialized = true;
try
{
//SqlServerTypes.Utilities.LoadNativeAssemblies(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
SqlServerTypes.Utilities.LoadNativeAssemblies(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
}
catch (Exception ex)
{
throw new TypeInitializationException("EF6BusinessObjectRepository", ex);
throw new TypeInitializationException("EF6BusinessObjectSource", ex);
}
}
}
}

public class EF6BusinessObjectRepository<T> : BusinessObjectAccessBase<T>
/// <summary>
/// A bui
/// </summary>
/// <typeparam name="T"></typeparam>
public class EF6BusinessObjectSource<T> : BaseBusinessObjectSource<T>
where T:class, IEF6SpatialGeometryObject
{
static EF6BusinessObjectRepository()
static EF6BusinessObjectSource()
{
EF6BusinessObjectRepository.Configure();
EF6BusinessObjectSource.Configure();
}

private readonly Func<DbContext> _createContext;
private IGeometryFactory _factory;

public DbContext Context { get { return _createContext(); } }

public EF6BusinessObjectRepository(Func<DbContext> createContext)
public EF6BusinessObjectSource(Func<DbContext> createContext)
{

_createContext = createContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
using GeoAPI.Geometries;

namespace SharpMap.Data.Providers.Business
{
public abstract class EF6SpatialGeographyObjectBase : IEF6SpatialGeographyObject
{
private IGeometry _geometry;

/// <summary>
/// Gets or sets the Feature Id
/// </summary>
[BusinessObjectIdentifier]
public abstract uint Fid { get; }

/// <summary>
/// Gets or sets the DbGeometry object
/// </summary>
public abstract DbGeography DbGeometry { get; set; }

/// <summary>
/// Gets or sets the geometry object
/// </summary>
[NotMapped, BusinessObjectGeometry]
public IGeometry Geometry
{
get { return _geometry; }
set
{
if (ReferenceEquals(_geometry, value))
return;

_geometry = value;
DbGeometry = _geometry.ToDbGeography();
}
}

/// <summary>
/// Method to set the <see cref="DbGeometry"/> object
/// </summary>
/// <param name="dbGeometry">The DbGeometry</param>
protected abstract void SetDbGeometry(DbGeometry dbGeometry);

/// <summary>
/// Method to set the <see cref="Geometry"/> object.
/// </summary>
/// <param name="geometry">The geometry</param>
protected void SetGeometry(IGeometry geometry)
{
_geometry = geometry;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
using GeoAPI.Geometries;

namespace SharpMap.Data.Providers.Business
{
public abstract class EF6SpatialGeometryObjectBase : IEF6SpatialGeometryObject
{
private IGeometry _geometry;

/// <summary>
/// Gets or sets the Feature Id
/// </summary>
[BusinessObjectIdentifier]
public abstract uint Fid { get; }

/// <summary>
/// Gets or sets the DbGeometry object
/// </summary>
public abstract DbGeometry DbGeometry { get; set; }

/// <summary>
/// Gets or sets the geometry object
/// </summary>
[NotMapped, BusinessObjectGeometry]
public IGeometry Geometry
{
get { return _geometry; }
set
{
if (ReferenceEquals(_geometry, value))
return;

_geometry = value;
DbGeometry = _geometry.ToDbGeometry();
}
}

/// <summary>
/// Method to set the <see cref="DbGeometry"/> object
/// </summary>
/// <param name="dbGeometry">The DbGeometry</param>
protected abstract void SetDbGeometry(DbGeometry dbGeometry);

/// <summary>
/// Method to set the <see cref="Geometry"/> object.
/// </summary>
/// <param name="geometry">The geometry</param>
protected void SetGeometry(IGeometry geometry)
{
_geometry = geometry;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,60 +1,9 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
using GeoAPI.Geometries;
using System.Data.Entity.Spatial;

namespace SharpMap.Data.Providers.Business
{
public interface IEF6SpatialGeographyObject
: IEF6SpatialObject<DbGeography>
{
}

public abstract class EF6SpatialGeographyObjectBase : IEF6SpatialGeographyObject
{
private IGeometry _geometry;

/// <summary>
/// Gets or sets the Feature Id
/// </summary>
[BusinessObjectIdentifier]
public abstract uint Fid { get; }

/// <summary>
/// Gets or sets the DbGeometry object
/// </summary>
public abstract DbGeography DbGeometry { get; set; }

/// <summary>
/// Gets or sets the geometry object
/// </summary>
[NotMapped, BusinessObjectGeometry]
public IGeometry Geometry
{
get { return _geometry; }
set
{
if (ReferenceEquals(_geometry, value))
return;

_geometry = value;
DbGeometry = _geometry.ToDbGeography();
}
}

/// <summary>
/// Method to set the <see cref="DbGeometry"/> object
/// </summary>
/// <param name="dbGeometry">The DbGeometry</param>
protected abstract void SetDbGeometry(DbGeometry dbGeometry);

/// <summary>
/// Method to set the <see cref="Geometry"/> object.
/// </summary>
/// <param name="geometry">The geometry</param>
protected void SetGeometry(IGeometry geometry)
{
_geometry = geometry;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Data.Entity.Spatial;

namespace SharpMap.Data.Providers.Business
{
/// <summary>
/// Interface for spatial entities using <see cref="DbGeography"/> as geometry
/// </summary>
public interface IEF6SpatialGeometryObject
: IEF6SpatialObject<DbGeometry>
{
}
}
Loading

0 comments on commit e3e4850

Please sign in to comment.