Skip to content

Commit

Permalink
Make change set initializer public
Browse files Browse the repository at this point in the history
  • Loading branch information
chinadragon0515 committed Aug 2, 2016
1 parent 0b3cfed commit 2a11166
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 56 deletions.
2 changes: 2 additions & 0 deletions src/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@
[assembly: SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Sourcer", Scope = "type", Target = "Microsoft.Restier.Core.Query.IQueryExpressionSourcer")]
[assembly: SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Sourcer", Scope = "type", Target = "Microsoft.Restier.Providers.EntityFramework.Query.QueryExpressionSourcer")]
[assembly: SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Ef", Scope = "member", Target = "Microsoft.Restier.Providers.EntityFramework.ServiceCollectionExtensions.#AddEfProviderServices`1(Microsoft.Extensions.DependencyInjection.IServiceCollection)")]
[assembly: SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Ef", Scope = "member", Target = "Microsoft.Restier.Providers.EntityFramework.Submit.ChangeSetInitializer.#ConvertToEfValue(System.Type,System.Object)")]
[assembly: SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Etag", Scope = "member", Target = "Microsoft.Restier.Core.Submit.DataModificationItem.#ApplyEtag(System.Linq.IQueryable)")]
#endregion

#region CA1709 Identifiers case
[assembly: SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Ef", Scope = "member", Target = "Microsoft.Restier.Providers.EntityFramework.ServiceCollectionExtensions.#AddEfProviderServices`1(Microsoft.Extensions.DependencyInjection.IServiceCollection)")]
[assembly: SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Ef", Scope = "member", Target = "Microsoft.Restier.Providers.EntityFramework.Submit.ChangeSetInitializer.#ConvertToEfValue(System.Type,System.Object)")]
#endregion

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Microsoft.Restier.Providers.EntityFramework.Submit
/// <summary>
/// To prepare changed entries for the given <see cref="ChangeSet"/>.
/// </summary>
internal class ChangeSetInitializer : IChangeSetInitializer
public class ChangeSetInitializer : IChangeSetInitializer
{
/// <summary>
/// Asynchronously prepare the <see cref="ChangeSet"/>.
Expand Down Expand Up @@ -84,6 +84,65 @@ public async Task InitializeAsync(
}
}

/// <summary>
/// Convert a Edm type value to Entity Framework supported value type
/// </summary>
/// <param name="type">The type of the property defined in CLR class</param>
/// <param name="value">The value from OData deserializer and in type of Edm</param>
/// <returns>The converted value object</returns>
public virtual object ConvertToEfValue(Type type, object value)
{
// string[EdmType = Enum] => System.Enum
if (TypeHelper.IsEnum(type))
{
return Enum.Parse(TypeHelper.GetUnderlyingTypeOrSelf(type), (string)value);
}

// Edm.Date => System.DateTime[SqlType = Date]
if (value is Date)
{
var dateValue = (Date)value;
return (DateTime)dateValue;
}

// System.DateTimeOffset => System.DateTime[SqlType = DateTime or DateTime2]
if (value is DateTimeOffset && TypeHelper.IsDateTime(type))
{
var dateTimeOffsetValue = (DateTimeOffset)value;
return dateTimeOffsetValue.DateTime;
}

// Edm.TimeOfDay => System.TimeSpan[SqlType = Time]
if (value is TimeOfDay && TypeHelper.IsTimeSpan(type))
{
var timeOfDayValue = (TimeOfDay)value;
return (TimeSpan)timeOfDayValue;
}

// In case key is long type, when put an entity, key value will be from key parsing which is type of int
if (value is int && type == typeof(long))
{
return Convert.ToInt64(value, CultureInfo.InvariantCulture);
}

if (type == typeof(DbGeography))
{
var point = value as GeographyPoint;
if (point != null)
{
return point.ToDbGeography();
}

var s = value as GeographyLineString;
if (s != null)
{
return s.ToDbGeography();
}
}

return value;
}

private static async Task<object> FindEntity(
SubmitContext context,
DataModificationItem item,
Expand Down Expand Up @@ -120,7 +179,7 @@ private static async Task<object> FindEntity(
return etagEntity;
}

private static void SetValues(DbEntityEntry dbEntry, DataModificationItem item, Type entityType)
private void SetValues(DbEntityEntry dbEntry, DataModificationItem item, Type entityType)
{
if (item.IsFullReplaceUpdateRequest)
{
Expand Down Expand Up @@ -182,7 +241,7 @@ private static void SetValues(DbEntityEntry dbEntry, DataModificationItem item,
}
}

private static void SetValues(object instance, Type type, IReadOnlyDictionary<string, object> values)
private void SetValues(object instance, Type type, IReadOnlyDictionary<string, object> values)
{
foreach (KeyValuePair<string, object> propertyPair in values)
{
Expand Down Expand Up @@ -214,58 +273,5 @@ private static void SetValues(object instance, Type type, IReadOnlyDictionary<st
propertyInfo.SetValue(instance, value);
}
}

private static object ConvertToEfValue(Type type, object value)
{
// string[EdmType = Enum] => System.Enum
if (TypeHelper.IsEnum(type))
{
return Enum.Parse(TypeHelper.GetUnderlyingTypeOrSelf(type), (string)value);
}

// Edm.Date => System.DateTime[SqlType = Date]
if (value is Date)
{
var dateValue = (Date)value;
return (DateTime)dateValue;
}

// System.DateTimeOffset => System.DateTime[SqlType = DateTime or DateTime2]
if (value is DateTimeOffset && TypeHelper.IsDateTime(type))
{
var dateTimeOffsetValue = (DateTimeOffset)value;
return dateTimeOffsetValue.DateTime;
}

// Edm.TimeOfDay => System.TimeSpan[SqlType = Time]
if (value is TimeOfDay && TypeHelper.IsTimeSpan(type))
{
var timeOfDayValue = (TimeOfDay)value;
return (TimeSpan)timeOfDayValue;
}

// In case key is long type, when put an entity, key value will be from key parsing which is type of int
if (value is int && type == typeof(long))
{
return Convert.ToInt64(value, CultureInfo.InvariantCulture);
}

if (type == typeof(DbGeography))
{
var point = value as GeographyPoint;
if (point != null)
{
return point.ToDbGeography();
}

var s = value as GeographyLineString;
if (s != null)
{
return s.ToDbGeography();
}
}

return value;
}
}
}
10 changes: 10 additions & 0 deletions test/Microsoft.Restier.TestCommon/PublicApi.bsl
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,16 @@ public sealed class Microsoft.Restier.Providers.EntityFramework.Spatial.Geograph
public static Microsoft.Spatial.GeographyPoint ToGeographyPoint (DbGeography geography)
}

public class Microsoft.Restier.Providers.EntityFramework.Submit.ChangeSetInitializer : IChangeSetInitializer {
public ChangeSetInitializer ()

public virtual object ConvertToEfValue (System.Type type, object value)
[
AsyncStateMachineAttribute(),
]
public virtual System.Threading.Tasks.Task InitializeAsync (Microsoft.Restier.Core.Submit.SubmitContext context, System.Threading.CancellationToken cancellationToken)
}

public class Microsoft.Restier.Publishers.OData.Batch.RestierBatchChangeSetRequestItem : System.Web.OData.Batch.ChangeSetRequestItem, IDisposable {
public RestierBatchChangeSetRequestItem (System.Collections.Generic.IEnumerable`1[[System.Net.Http.HttpRequestMessage]] requests, System.Func`1[[Microsoft.Restier.Core.ApiBase]] apiFactory)

Expand Down

0 comments on commit 2a11166

Please sign in to comment.