Skip to content

Commit

Permalink
Introduced interface for DbContext.
Browse files Browse the repository at this point in the history
References #19
  • Loading branch information
jspuij committed Aug 16, 2019
1 parent d39cc44 commit e4afb07
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/Microsoft.Restier.EntityFramework/EntityFrameworkApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Microsoft.Restier.EntityFramework
#if EF7
[CLSCompliant(false)]
#endif
public class EntityFrameworkApi<T> : ApiBase where T : DbContext
public class EntityFrameworkApi<T> : ApiBase, IDbContextProvider where T : DbContext
{
/// <summary>
/// Initializes a new instance of the <see cref="EntityFrameworkApi{T}" /> class.
Expand All @@ -54,14 +54,19 @@ public EntityFrameworkApi(IServiceProvider serviceProvider) : base(serviceProvid
/// <summary>
/// Gets the underlying DbContext for this API.
/// </summary>
protected T DbContext
protected virtual T DbContext
{
get
{
return (T)this.ServiceProvider.GetService<DbContext>();
}
}

/// <summary>
/// Gets the underlying DbContext for this API.
/// </summary>
DbContext IDbContextProvider.DbContext => DbContext;


/// <summary>
/// Configures the API services for this API. Descendants may override this method to register
Expand Down
20 changes: 20 additions & 0 deletions src/Microsoft.Restier.EntityFramework/IDbContextProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Restier.EntityFramework
{
/// <summary>
/// Interface to implement by Api classes to get the context.
/// </summary>
public interface IDbContextProvider
{
/// <summary>
/// Gets the underlying DbContext for this API.
/// </summary>
DbContext DbContext { get; }
}
}
5 changes: 2 additions & 3 deletions src/Microsoft.Restier.EntityFramework/Model/ModelProducer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public Task<IEdmModel> GetModelAsync(ModelContext context, CancellationToken can
{
Ensure.NotNull(context, nameof(context));

var dbContext = (context.Api as IDbContextProvider).DbContext;
#if EF7
var dbContext = context.GetApiService<DbContext>();
context.ResourceSetTypeMap.AddRange(dbContext.GetType().GetProperties()
.Where(e => e.PropertyType.FindGenericType(typeof(DbSet<>)) != null)
.ToDictionary(e => e.Name, e => e.PropertyType.GetGenericArguments()[0]));
Expand All @@ -57,8 +57,7 @@ public Task<IEdmModel> GetModelAsync(ModelContext context, CancellationToken can
e => ((ICollection<PropertyInfo>)
e.FindPrimaryKey().Properties.Select(p => e.ClrType.GetProperty(p.Name)).ToList())));
#else
var dbContext = context.GetApiService<DbContext>();


var efModel = (dbContext as IObjectContextAdapter).ObjectContext.MetadataWorkspace;

// @robertmclaws: The query below actually returns all registered Containers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Expression ReplaceQueryableSource(QueryExpressionContext context, bool em
return null;
}

var dbContext = context.QueryContext.GetApiService<DbContext>();
var dbContext = (context.QueryContext.Api as IDbContextProvider).DbContext;
var dbSetProperty = dbContext.GetType().GetProperties()
.FirstOrDefault(prop => prop.Name == context.ModelReference.EntitySet.Name);
if (dbSetProperty == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task InitializeAsync(SubmitContext context, CancellationToken cance
throw new ArgumentNullException(nameof(context));
}

var dbContext = context.GetApiService<DbContext>();
var dbContext = (context.Api as IDbContextProvider).DbContext;

foreach (var entry in context.ChangeSet.Entries.OfType<DataModificationItem>())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal class SubmitExecutor : ISubmitExecutor
/// <returns>The task object that represents this asynchronous operation.</returns>
public async Task<SubmitResult> ExecuteSubmitAsync(SubmitContext context, CancellationToken cancellationToken)
{
var dbContext = context.GetApiService<DbContext>();
var dbContext = (context.Api as IDbContextProvider).DbContext;
await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
return new SubmitResult(context.ChangeSet);
}
Expand Down

0 comments on commit e4afb07

Please sign in to comment.