ID DI (Update Identity to use EF DI/config)

Constructors that take IServiceProvider so it can be injected and also kept the parameterless constructor which will create its own DI container as before--not sure if this is desirable for Identity or not, but can be changed later.
This commit is contained in:
ajcvickers 2014-04-18 14:31:08 -07:00
parent da7140b9d0
commit 389678042f
3 changed files with 34 additions and 13 deletions

View File

@ -1,4 +1,6 @@
using System; using System;
using Microsoft.AspNet.DependencyInjection;
using Microsoft.AspNet.DependencyInjection.Fallback;
using Microsoft.Data.Entity; using Microsoft.Data.Entity;
using Microsoft.Data.InMemory; using Microsoft.Data.InMemory;
using Microsoft.Data.Entity.Metadata; using Microsoft.Data.Entity.Metadata;
@ -8,8 +10,8 @@ namespace Microsoft.AspNet.Identity.Entity
public class IdentityContext : public class IdentityContext :
IdentityContext<EntityUser, EntityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim> IdentityContext<EntityUser, EntityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{ {
public IdentityContext(EntityConfiguration config) : base(config) { }
public IdentityContext() { } public IdentityContext() { }
public IdentityContext(IServiceProvider serviceProvider) : base(serviceProvider) { }
} }
public class IdentityContext<TUser, TRole, TKey, TUserLogin, TUserRole, TUserClaim> : EntityContext public class IdentityContext<TUser, TRole, TKey, TUserLogin, TUserRole, TUserClaim> : EntityContext
@ -24,15 +26,25 @@ namespace Microsoft.AspNet.Identity.Entity
public EntitySet<TUser> Users { get; set; } public EntitySet<TUser> Users { get; set; }
public EntitySet<TRole> Roles { get; set; } public EntitySet<TRole> Roles { get; set; }
public IdentityContext() { } public IdentityContext(IServiceProvider serviceProvider)
public IdentityContext(EntityConfiguration config) : base(config) { } : base(serviceProvider) { }
public IdentityContext()
: this(new ServiceCollection()
.AddEntityFramework(
//#if NET45
// s => s.AddSqlServer()
//#else
s => s.AddInMemoryStore()
//#endif
).BuildServiceProvider()) { }
protected override void OnConfiguring(EntityConfigurationBuilder builder) protected override void OnConfiguring(EntityConfigurationBuilder builder)
{ {
//#if NET45 //#if NET45
// builder.UseSqlServer(@"Server=(localdb)\v11.0;Database=IdentityDb;Trusted_Connection=True;"); // builder.SqlServerConnectionString(@"Server=(localdb)\v11.0;Database=IdentityDb;Trusted_Connection=True;");
//#else //#else
builder.UseDataStore(new InMemoryDataStore()); builder.UseInMemoryStore(persist: true);
//#endif //#endif
} }

View File

@ -1,4 +1,6 @@
using Microsoft.AspNet.Testing; using Microsoft.AspNet.Testing;
using Microsoft.AspNet.DependencyInjection;
using Microsoft.AspNet.DependencyInjection.Fallback;
using Microsoft.Data.Entity; using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Metadata; using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Storage; using Microsoft.Data.Entity.Storage;
@ -16,12 +18,11 @@ namespace Microsoft.AspNet.Identity.Entity.Test
{ {
public static EntityContext CreateContext() public static EntityContext CreateContext()
{ {
var configuration = new EntityConfigurationBuilder() var serviceProvider = new ServiceCollection()
//.UseModel(model) .AddEntityFramework(s => s.AddInMemoryStore())
.UseDataStore(new InMemoryDataStore()) .BuildServiceProvider();
.BuildConfiguration();
var db = new IdentityContext(configuration); var db = new IdentityContext(serviceProvider);
// var sql = db.Configuration.DataStore as SqlServerDataStore; // var sql = db.Configuration.DataStore as SqlServerDataStore;
// if (sql != null) // if (sql != null)
// { // {

View File

@ -1,3 +1,5 @@
using Microsoft.AspNet.DependencyInjection;
using Microsoft.AspNet.DependencyInjection.Fallback;
using Microsoft.AspNet.Testing; using Microsoft.AspNet.Testing;
using Microsoft.Data.Entity; using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Metadata; using Microsoft.Data.Entity.Metadata;
@ -119,13 +121,17 @@ namespace Microsoft.AspNet.Identity.Entity.Test
[Fact] [Fact]
public async Task Can_share_instance_between_contexts_with_sugar_experience2() public async Task Can_share_instance_between_contexts_with_sugar_experience2()
{ {
using (var db = new IdentityContext()) // TODO: Should be possible to do this without creating the provider externally, but
// that is currently not working. Will be investigated.
var provider = new ServiceCollection().AddEntityFramework(s => s.AddInMemoryStore()).BuildServiceProvider();
using (var db = new IdentityContext(provider))
{ {
db.Users.Add(new EntityUser { UserName = "John Doe" }); db.Users.Add(new EntityUser { UserName = "John Doe" });
await db.SaveChangesAsync(); await db.SaveChangesAsync();
} }
using (var db = new IdentityContext()) using (var db = new IdentityContext(provider))
{ {
var data = db.Users.ToList(); var data = db.Users.ToList();
Assert.Equal(1, data.Count); Assert.Equal(1, data.Count);
@ -151,7 +157,9 @@ namespace Microsoft.AspNet.Identity.Entity.Test
protected override void OnConfiguring(EntityConfigurationBuilder builder) protected override void OnConfiguring(EntityConfigurationBuilder builder)
{ {
builder.UseDataStore(new InMemoryDataStore()); builder
.WithServices(s => s.AddInMemoryStore())
.UseInMemoryStore(persist: true);
} }
protected override void OnModelCreating(ModelBuilder builder) protected override void OnModelCreating(ModelBuilder builder)