Move extensions to Builder

Also add DbContext concrete to AddEntity extension
This commit is contained in:
Hao Kung 2014-05-15 16:04:26 -07:00
parent 6432655d26
commit 81bc90e550
9 changed files with 89 additions and 77 deletions

View File

@ -1,28 +0,0 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Entity;
using Microsoft.Data.Entity;
namespace Microsoft.Framework.DependencyInjection
{
public static class EntityServiceCollectionExtensions
{
public static ServiceCollection AddEntity<TUser>(this ServiceCollection services)
where TUser : User
{
services.AddScoped<IUserStore<TUser>, UserStore<TUser>>();
services.AddScoped<UserManager<TUser>>();
return services;
}
public static ServiceCollection AddEntity<TUser, TContext>(this ServiceCollection services)
where TUser : User where TContext : DbContext
{
services.AddEntity<TUser>();
services.AddScoped<DbContext, TContext>();
return services;
}
}
}

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Identity.Entity;
using Microsoft.Data.Entity;
using Microsoft.Framework.DependencyInjection;
namespace Microsoft.AspNet.Identity
@ -26,5 +27,13 @@ namespace Microsoft.AspNet.Identity
builder.Services.AddScoped<UserManager<TUser>>();
return builder;
}
public static IdentityBuilder<TUser, IdentityRole> AddEntity<TUser, TContext>(this IdentityBuilder<TUser, IdentityRole> builder)
where TUser : User where TContext : DbContext
{
builder.Services.AddScoped<IUserStore<TUser>, UserStore<TUser, TContext>>();
builder.Services.AddScoped<UserManager<TUser>>();
builder.Services.AddScoped<TContext>();
return builder;
}
}
}

View File

@ -0,0 +1,28 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Entity;
using Microsoft.Data.Entity;
namespace Microsoft.Framework.DependencyInjection
{
public static class IdentityEntityServiceCollectionExtensions
{
//public static IdentityBuilder<TUser, IdentityRole> AddEntity<TUser>(this ServiceCollection services)
// where TUser : User
//{
// services.AddScoped<IUserStore<TUser>, UserStore<TUser>>();
// services.AddScoped<UserManager<TUser>>();
// return services;
//}
//public static ServiceCollection AddEntity<TUser, TContext>(this ServiceCollection services)
// where TUser : User where TContext : DbContext
//{
// services.AddEntity<TUser>();
// services.AddScoped<DbContext, TContext>();
// return services;
//}
}
}

View File

@ -12,20 +12,22 @@ namespace Microsoft.AspNet.Identity.Entity
{
public IdentitySqlContext() { }
public IdentitySqlContext(IServiceProvider serviceProvider) : base(serviceProvider) { }
public IdentitySqlContext(ImmutableDbContextOptions options) : base(options) { }
public IdentitySqlContext(IServiceProvider serviceProvider, ImmutableDbContextOptions options) : base(serviceProvider, options) { }
}
public class IdentitySqlContext<TUser> : DbContext
where TUser : User
{
public DbSet<TUser> Users { get; set; }
public DbSet<IdentityUserClaim> UserClaims { get; set; }
//public DbSet<TRole> Roles { get; set; }
public IdentitySqlContext(IServiceProvider serviceProvider)
: base(serviceProvider) { }
public IdentitySqlContext() { }
public IdentitySqlContext(IServiceProvider serviceProvider) : base(serviceProvider) { }
public IdentitySqlContext(ImmutableDbContextOptions options) : base(options) { }
public IdentitySqlContext(IServiceProvider serviceProvider, ImmutableDbContextOptions options) : base(serviceProvider, options) { }
protected override void OnConfiguring(DbContextOptions builder)
{

View File

@ -13,23 +13,22 @@ using Microsoft.Data.Entity;
namespace Microsoft.AspNet.Identity.Entity
{
// Real Sql implementation
public class SqlUserStore :
UserStore<User>
public class UserStore<TUser> : UserStore<TUser, DbContext> where TUser : User
{
public SqlUserStore(DbContext context) : base(context) { }
public UserStore(DbContext context) : base(context) { }
}
public class UserStore<TUser> :
public class UserStore<TUser, TContext> :
//IUserRoleStore<TUser>,
IUserPasswordStore<TUser>,
IQueryableUserStore<TUser>,
IUserClaimStore<TUser>
where TUser : User
where TContext : DbContext
{
private bool _disposed;
public UserStore(DbContext context)
public UserStore(TContext context)
{
if (context == null)
{
@ -39,7 +38,7 @@ namespace Microsoft.AspNet.Identity.Entity
AutoSaveChanges = true;
}
public DbContext Context { get; private set; }
public TContext Context { get; private set; }
/// <summary>
/// If true will call SaveChanges after CreateAsync/UpdateAsync/DeleteAsync

View File

@ -0,0 +1,18 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Identity.Security;
using Microsoft.Framework.DependencyInjection;
namespace Microsoft.AspNet.Identity
{
public static class IdentityBuilderExtensions
{
public static IdentityBuilder<TUser, IdentityRole> AddSecurity<TUser>(this IdentityBuilder<TUser, IdentityRole> builder)
where TUser : class
{
builder.Services.AddScoped<SignInManager<TUser>>();
return builder;
}
}
}

View File

@ -8,7 +8,7 @@ namespace Microsoft.Framework.DependencyInjection
{
public static class IdentityServiceCollectionExtensions
{
public static ServiceCollection AddIdentity<TUser, TRole>(this ServiceCollection services)
public static IdentityBuilder<TUser, TRole> AddIdentity<TUser, TRole>(this ServiceCollection services)
where TUser : class
where TRole : class
{
@ -16,25 +16,26 @@ namespace Microsoft.Framework.DependencyInjection
services.Add(IdentityServices.GetDefaultRoleServices<TRole>());
services.AddTransient<IOptionsSetup<IdentityOptions>, IdentityOptionsSetup>();
services.AddSingleton<IOptionsAccessor<IdentityOptions>, OptionsAccessor<IdentityOptions>>();
return services;
return new IdentityBuilder<TUser, TRole>(services);
}
public static ServiceCollection AddIdentity<TUser, TRole>(this ServiceCollection services, Action<IdentityBuilder<TUser, TRole>> actionBuilder)
public static IdentityBuilder<TUser, TRole> AddIdentity<TUser, TRole>(this ServiceCollection services, Action<IdentityBuilder<TUser, TRole>> actionBuilder)
where TUser : class
where TRole : class
{
services.AddIdentity<TUser, TRole>();
actionBuilder(new IdentityBuilder<TUser, TRole>(services));
return services;
var builder = new IdentityBuilder<TUser, TRole>(services);
actionBuilder(builder);
return builder;
}
public static ServiceCollection AddIdentity<TUser>(this ServiceCollection services)
public static IdentityBuilder<TUser, IdentityRole> AddIdentity<TUser>(this ServiceCollection services)
where TUser : class
{
return services.AddIdentity<TUser, IdentityRole>();
}
public static ServiceCollection AddIdentity<TUser>(this ServiceCollection services, Action<IdentityBuilder<TUser, IdentityRole>> actionBuilder)
public static IdentityBuilder<TUser, IdentityRole> AddIdentity<TUser>(this ServiceCollection services, Action<IdentityBuilder<TUser, IdentityRole>> actionBuilder)
where TUser : class
{
return services.AddIdentity<TUser, IdentityRole>(actionBuilder);

View File

@ -22,14 +22,6 @@ namespace Microsoft.AspNet.Identity.Entity.Test
public class SqlUserStoreTest
{
public class ApplicationUser : User { }
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IServiceProvider services, IUserStore<ApplicationUser> store, IOptionsAccessor<IdentityOptions> options) : base(services, store, options) { }
}
public class ApplicationRoleManager : RoleManager<EntityRole>
{
public ApplicationRoleManager(IServiceProvider services, IRoleStore<EntityRole> store) : base(services, store) { }
}
public class ApplicationDbContext : IdentitySqlContext<ApplicationUser>
{
@ -39,25 +31,17 @@ namespace Microsoft.AspNet.Identity.Entity.Test
[Fact]
public async Task EnsureStartupUsageWorks()
{
IBuilder builder = new Microsoft.AspNet.Builder.Builder(new ServiceCollection().BuildServiceProvider());
//builder.UseServices(services => services.AddIdentity<ApplicationUser>(s =>
// s.AddEntity<ApplicationDbContext>()
//{
EnsureDatabase();
IBuilder builder = new Builder.Builder(new ServiceCollection().BuildServiceProvider());
builder.UseServices(services =>
{
services.AddEntityFramework();
services.AddInstance<DbContext>(CreateAppContext());
services.AddIdentity<ApplicationUser>(s =>
{
s.AddEntity();
s.AddUserManager<ApplicationUserManager>();
});
services.AddEntityFramework().AddSqlServer();
services.AddIdentity<ApplicationUser>().AddEntity<ApplicationUser, ApplicationDbContext>();
});
var userStore = builder.ApplicationServices.GetService<IUserStore<ApplicationUser>>();
var userManager = builder.ApplicationServices.GetService<ApplicationUserManager>();
var userManager = builder.ApplicationServices.GetService<UserManager<ApplicationUser>>();
Assert.NotNull(userStore);
Assert.NotNull(userManager);
@ -88,14 +72,16 @@ namespace Microsoft.AspNet.Identity.Entity.Test
var serviceProvider = services.BuildServiceProvider();
var db = new IdentitySqlContext(serviceProvider);
// TODO: Recreate DB, doesn't support String ID or Identity context yet
db.Database.EnsureCreated();
// TODO: CreateAsync DB?
return db;
}
public static void EnsureDatabase()
{
CreateContext();
}
public static ApplicationDbContext CreateAppContext()
{
var services = new ServiceCollection();

View File

@ -29,7 +29,7 @@ namespace Microsoft.AspNet.Identity.Security.Test
[InlineData(false)]
public async Task VerifyAccountControllerSignIn(bool isPersistent)
{
IBuilder app = new Microsoft.AspNet.Builder.Builder(new ServiceCollection().BuildServiceProvider());
IBuilder app = new Builder.Builder(new ServiceCollection().BuildServiceProvider());
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie
@ -48,10 +48,7 @@ namespace Microsoft.AspNet.Identity.Security.Test
services.AddIdentity<ApplicationUser, IdentityRole>(s =>
{
s.AddInMemory();
s.AddUserManager<ApplicationUserManager>();
s.AddRoleManager<ApplicationRoleManager>();
});
services.AddTransient<ApplicationSignInManager>();
}).AddSecurity<ApplicationUser>();
});
// Act
@ -60,8 +57,8 @@ namespace Microsoft.AspNet.Identity.Security.Test
UserName = "Yolo"
};
const string password = "Yol0Sw@g!";
var userManager = app.ApplicationServices.GetService<ApplicationUserManager>();
var signInManager = app.ApplicationServices.GetService<ApplicationSignInManager>();
var userManager = app.ApplicationServices.GetService<UserManager<ApplicationUser>>();
var signInManager = app.ApplicationServices.GetService<SignInManager<ApplicationUser>>();
IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password));
var result = await signInManager.PasswordSignInAsync(user.UserName, password, isPersistent, false);