From 389678042f8c7ba6a12c294a3e992ca4d422a4ae Mon Sep 17 00:00:00 2001 From: ajcvickers Date: Fri, 18 Apr 2014 14:31:08 -0700 Subject: [PATCH] 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. --- .../IdentityContext.cs | 22 ++++++++++++++----- .../TestIdentityFactory.cs | 11 +++++----- .../UserStoreTest.cs | 14 +++++++++--- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.AspNet.Identity.Entity/IdentityContext.cs b/src/Microsoft.AspNet.Identity.Entity/IdentityContext.cs index ea85f4ab96..4e37ea3bcc 100644 --- a/src/Microsoft.AspNet.Identity.Entity/IdentityContext.cs +++ b/src/Microsoft.AspNet.Identity.Entity/IdentityContext.cs @@ -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 { - public IdentityContext(EntityConfiguration config) : base(config) { } public IdentityContext() { } + public IdentityContext(IServiceProvider serviceProvider) : base(serviceProvider) { } } public class IdentityContext : EntityContext @@ -24,15 +26,25 @@ namespace Microsoft.AspNet.Identity.Entity public EntitySet Users { get; set; } public EntitySet 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 } diff --git a/test/Microsoft.AspNet.Identity.Entity.Test/TestIdentityFactory.cs b/test/Microsoft.AspNet.Identity.Entity.Test/TestIdentityFactory.cs index d2a71d764d..7d30c54c88 100644 --- a/test/Microsoft.AspNet.Identity.Entity.Test/TestIdentityFactory.cs +++ b/test/Microsoft.AspNet.Identity.Entity.Test/TestIdentityFactory.cs @@ -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) // { diff --git a/test/Microsoft.AspNet.Identity.Entity.Test/UserStoreTest.cs b/test/Microsoft.AspNet.Identity.Entity.Test/UserStoreTest.cs index fa2ec6cd54..a6f195e9a1 100644 --- a/test/Microsoft.AspNet.Identity.Entity.Test/UserStoreTest.cs +++ b/test/Microsoft.AspNet.Identity.Entity.Test/UserStoreTest.cs @@ -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)