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 Microsoft.AspNet.DependencyInjection;
using Microsoft.AspNet.DependencyInjection.Fallback;
using Microsoft.Data.Entity;
using Microsoft.Data.InMemory;
using Microsoft.Data.Entity.Metadata;
@ -8,8 +10,8 @@ namespace Microsoft.AspNet.Identity.Entity
public class IdentityContext :
IdentityContext<EntityUser, EntityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
public IdentityContext(EntityConfiguration config) : base(config) { }
public IdentityContext() { }
public IdentityContext(IServiceProvider serviceProvider) : base(serviceProvider) { }
}
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<TRole> Roles { get; set; }
public IdentityContext() { }
public IdentityContext(EntityConfiguration config) : base(config) { }
public IdentityContext(IServiceProvider serviceProvider)
: 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)
{
//#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
builder.UseDataStore(new InMemoryDataStore());
builder.UseInMemoryStore(persist: true);
//#endif
}

View File

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

View File

@ -1,3 +1,5 @@
using Microsoft.AspNet.DependencyInjection;
using Microsoft.AspNet.DependencyInjection.Fallback;
using Microsoft.AspNet.Testing;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Metadata;
@ -119,13 +121,17 @@ namespace Microsoft.AspNet.Identity.Entity.Test
[Fact]
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" });
await db.SaveChangesAsync();
}
using (var db = new IdentityContext())
using (var db = new IdentityContext(provider))
{
var data = db.Users.ToList();
Assert.Equal(1, data.Count);
@ -151,7 +157,9 @@ namespace Microsoft.AspNet.Identity.Entity.Test
protected override void OnConfiguring(EntityConfigurationBuilder builder)
{
builder.UseDataStore(new InMemoryDataStore());
builder
.WithServices(s => s.AddInMemoryStore())
.UseInMemoryStore(persist: true);
}
protected override void OnModelCreating(ModelBuilder builder)