forked from DapperLib/Dapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbGeometryHandler.cs
More file actions
59 lines (55 loc) · 2 KB
/
DbGeometryHandler.cs
File metadata and controls
59 lines (55 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using Microsoft.SqlServer.Types;
using System;
using System.Data;
using System.Data.Entity.Spatial;
using System.Data.SqlClient;
using System.Data.SqlTypes;
namespace Dapper.EntityFramework
{
/// <summary>
/// Type-handler for the DbGeometry spatial type.
/// </summary>
public class DbGeometryHandler : SqlMapper.TypeHandler<DbGeometry>
{
/// <summary>
/// Create a new handler instance.
/// </summary>
protected DbGeometryHandler() { /* create new */ }
/// <summary>
/// Default handler instance.
/// </summary>
public static readonly DbGeometryHandler Default = new DbGeometryHandler();
/// <summary>
/// Assign the value of a parameter before a command executes.
/// </summary>
/// <param name="parameter">The parameter to configure.</param>
/// <param name="value">Parameter value.</param>
public override void SetValue(IDbDataParameter parameter, DbGeometry value)
{
object parsed = null;
if (value != null)
{
parsed = SqlGeometry.STGeomFromWKB(new SqlBytes(value.AsBinary()), value.CoordinateSystemId);
}
parameter.Value = parsed ?? DBNull.Value;
if (parameter is SqlParameter)
{
((SqlParameter)parameter).UdtTypeName = "geometry";
}
}
/// <summary>
/// Parse a database value back to a typed value.
/// </summary>
/// <param name="value">The value from the database.</param>
/// <returns>The typed value.</returns>
public override DbGeometry Parse(object value)
{
if (value == null || value is DBNull) return null;
if (value is SqlGeometry geo)
{
return DbGeometry.FromBinary(geo.STAsBinary().Value);
}
return DbGeometry.FromText(value.ToString());
}
}
}