Skip to content

Commit 74e5a99

Browse files
committed
Driver update to v-2.0 beta
1 parent bbab9a0 commit 74e5a99

14 files changed

+456
-391
lines changed

src/AspNet.Identity.MongoDB.sln.DotSettings

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_DECLARATIONS/@EntryValue">1</s:Int64>
55
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_CODE/@EntryValue">1</s:Int64>
66
<s:Boolean x:Key="/Default/CodeStyle/CSharpUsing/AddImportsToDeepestScope/@EntryValue">True</s:Boolean>
7-
<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=Shared/@EntryIndexedValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;Profile name="Shared"&gt;&lt;AspOptimizeRegisterDirectives&gt;True&lt;/AspOptimizeRegisterDirectives&gt;&lt;HtmlReformatCode&gt;True&lt;/HtmlReformatCode&gt;&lt;RemoveCodeRedundancies&gt;True&lt;/RemoveCodeRedundancies&gt;&lt;CSArrangeThisQualifier&gt;True&lt;/CSArrangeThisQualifier&gt;&lt;CSMakeFieldReadonly&gt;True&lt;/CSMakeFieldReadonly&gt;&lt;CSUseVar&gt;&lt;BehavourStyle&gt;CAN_CHANGE_TO_IMPLICIT&lt;/BehavourStyle&gt;&lt;LocalVariableStyle&gt;ALWAYS_IMPLICIT&lt;/LocalVariableStyle&gt;&lt;ForeachVariableStyle&gt;ALWAYS_IMPLICIT&lt;/ForeachVariableStyle&gt;&lt;/CSUseVar&gt;&lt;CSOptimizeUsings&gt;&lt;OptimizeUsings&gt;True&lt;/OptimizeUsings&gt;&lt;EmbraceInRegion&gt;False&lt;/EmbraceInRegion&gt;&lt;RegionName&gt;&lt;/RegionName&gt;&lt;/CSOptimizeUsings&gt;&lt;CSShortenReferences&gt;True&lt;/CSShortenReferences&gt;&lt;CSharpFormatDocComments&gt;True&lt;/CSharpFormatDocComments&gt;&lt;CSReformatCode&gt;True&lt;/CSReformatCode&gt;&lt;VBOptimizeImports&gt;True&lt;/VBOptimizeImports&gt;&lt;VBShortenReferences&gt;True&lt;/VBShortenReferences&gt;&lt;VBReformatCode&gt;True&lt;/VBReformatCode&gt;&lt;VBFormatDocComments&gt;True&lt;/VBFormatDocComments&gt;&lt;JsInsertSemicolon&gt;True&lt;/JsInsertSemicolon&gt;&lt;JsReformatCode&gt;True&lt;/JsReformatCode&gt;&lt;JsFormatDocComments&gt;True&lt;/JsFormatDocComments&gt;&lt;CssReformatCode&gt;True&lt;/CssReformatCode&gt;&lt;XMLReformatCode&gt;True&lt;/XMLReformatCode&gt;&lt;/Profile&gt;</s:String>
8-
<s:String x:Key="/Default/CodeStyle/CodeCleanup/SilentCleanupProfile/@EntryValue">Shared</s:String>
7+
8+
99
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

src/AspNet.Identity.MongoDB/AspNet.Identity.MongoDB.csproj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@
4949
<SpecificVersion>False</SpecificVersion>
5050
<HintPath>..\packages\MongoDB.Driver.Core.2.0.0-beta3\lib\net45\MongoDB.Driver.Core.dll</HintPath>
5151
</Reference>
52-
<Reference Include="MongoDB.Driver.Legacy, Version=2.0.0.741, Culture=neutral, processorArchitecture=MSIL">
53-
<SpecificVersion>False</SpecificVersion>
54-
<HintPath>..\packages\mongocsharpdriver.2.0.0-beta3\lib\net45\MongoDB.Driver.Legacy.dll</HintPath>
55-
</Reference>
5652
<Reference Include="System" />
5753
<Reference Include="System.Core" />
5854
<Reference Include="System.Xml.Linq" />
@@ -62,6 +58,8 @@
6258
<Reference Include="System.Xml" />
6359
</ItemGroup>
6460
<ItemGroup>
61+
<Compile Include="UsersContext.cs" />
62+
<Compile Include="RolesContext.cs" />
6563
<Compile Include="IdentityContext.cs" />
6664
<Compile Include="IdentityRole.cs" />
6765
<Compile Include="IdentityUser.cs" />

src/AspNet.Identity.MongoDB/IdentityContext.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@
33
using System;
44
using global::MongoDB.Driver;
55

6-
public class IdentityContext
7-
{
8-
public MongoCollection Users { get; set; }
9-
public MongoCollection Roles { get; set; }
6+
//public class IdentityContext
7+
//{
8+
// public MongoCollection Users { get; set; }
9+
// public MongoCollection Roles { get; set; }
1010

11-
public IdentityContext()
12-
{
13-
}
11+
// public IdentityContext()
12+
// {
13+
// }
1414

15-
public IdentityContext(MongoCollection users)
16-
{
17-
Users = users;
18-
}
15+
// public IdentityContext(MongoCollection users)
16+
// {
17+
// Users = users;
18+
// }
1919

20-
public IdentityContext(MongoCollection users, MongoCollection roles) : this(users)
21-
{
22-
Roles = roles;
23-
}
20+
// public IdentityContext(MongoCollection users, MongoCollection roles) : this(users)
21+
// {
22+
// Roles = roles;
23+
// }
2424

25-
[Obsolete("Use IndexChecks.EnsureUniqueIndexOnEmail")]
26-
public void EnsureUniqueIndexOnEmail()
27-
{
28-
IndexChecks.EnsureUniqueIndexOnEmail(Users);
29-
}
30-
}
25+
// [Obsolete("Use IndexChecks.EnsureUniqueIndexOnEmail")]
26+
// public void EnsureUniqueIndexOnEmail()
27+
// {
28+
// IndexChecks.EnsureUniqueIndexOnEmail(Users);
29+
// }
30+
//}
3131
}
Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
11
namespace AspNet.Identity.MongoDB
22
{
3-
using global::MongoDB.Driver;
4-
using global::MongoDB.Driver.Builders;
3+
using global::MongoDB.Driver;
54

6-
public class IndexChecks
7-
{
8-
public static void EnsureUniqueIndexOnUserName(MongoCollection users)
9-
{
10-
var userName = new IndexKeysBuilder<IdentityUser>().Ascending(t => t.UserName);
11-
var unique = new IndexOptionsBuilder().SetUnique(true);
12-
users.CreateIndex(userName, unique);
13-
}
5+
public static class IndexChecks
6+
{
7+
public static void EnsureUniqueIndexOnUserName<TUser>(this IMongoCollection<TUser> users)
8+
where TUser : IdentityUser
9+
{
10+
users.Indexes.CreateAsync(new IndexDefinitionBuilder<TUser>().Ascending(u => u.UserName),
11+
new CreateIndexOptions {Background = true, Unique = true});
1412

15-
public static void EnsureUniqueIndexOnRoleName(MongoCollection roles)
16-
{
17-
var roleName = new IndexKeysBuilder<IdentityRole>().Ascending(t => t.Name);
18-
var unique = new IndexOptionsBuilder().SetUnique(true);
19-
roles.CreateIndex(roleName, unique);
20-
}
13+
//var userName = new IndexKeysBuilder<IdentityUser>().Ascending(t => t.UserName);
14+
//var unique = new IndexOptionsBuilder().SetUnique(true);
15+
//users.CreateIndex(userName, unique);
16+
}
2117

22-
public static void EnsureUniqueIndexOnEmail(MongoCollection users)
23-
{
24-
var email = new IndexKeysBuilder<IdentityUser>().Ascending(t => t.Email);
25-
var unique = new IndexOptionsBuilder().SetUnique(true);
26-
users.CreateIndex(email, unique);
27-
}
28-
}
18+
public static void EnsureUniqueIndexOnRoleName<TRole>(this IMongoCollection<TRole> roles)
19+
where TRole : IdentityRole
20+
{
21+
roles.Indexes.CreateAsync(new IndexDefinitionBuilder<TRole>().Ascending(r => r.Name),
22+
new CreateIndexOptions {Background = true, Unique = true});
23+
24+
//var roleName = new IndexKeysBuilder<IdentityRole>().Ascending(t => t.Name);
25+
//var unique = new IndexOptionsBuilder().SetUnique(true);
26+
//roles.CreateIndex(roleName, unique);
27+
}
28+
29+
public static void EnsureUniqueIndexOnEmail<TUser>(this IMongoCollection<TUser> users)
30+
where TUser : IdentityUser
31+
{
32+
users.Indexes.CreateAsync(new IndexDefinitionBuilder<TUser>().Ascending(u => u.Email),
33+
new CreateIndexOptions { Background = true, Unique = true });
34+
35+
//var email = new IndexKeysBuilder<IdentityUser>().Ascending(t => t.Email);
36+
//var unique = new IndexOptionsBuilder().SetUnique(true);
37+
//users.CreateIndex(email, unique);
38+
}
39+
}
2940
}
Lines changed: 71 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,74 @@
11
namespace AspNet.Identity.MongoDB
22
{
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using global::MongoDB.Bson;
6-
using global::MongoDB.Driver.Builders;
7-
using global::MongoDB.Driver.Linq;
8-
using Microsoft.AspNet.Identity;
9-
10-
/// <summary>
11-
/// Note: Deleting and updating do not modify the roles stored on a user document. If you desire this dynamic
12-
/// capability, override the appropriate operations on RoleStore as desired for your application. For example you could
13-
/// perform a document modification on the users collection before a delete or a rename.
14-
/// </summary>
15-
/// <typeparam name="TRole"></typeparam>
16-
public class RoleStore<TRole> : IRoleStore<TRole>, IQueryableRoleStore<TRole>
17-
where TRole : IdentityRole
18-
{
19-
private readonly IdentityContext _Context;
20-
21-
public RoleStore(IdentityContext context)
22-
{
23-
_Context = context;
24-
}
25-
26-
public virtual IQueryable<TRole> Roles
27-
{
28-
get { return _Context.Roles.AsQueryable<TRole>(); }
29-
}
30-
31-
public virtual void Dispose()
32-
{
33-
// no need to dispose of anything, mongodb handles connection pooling automatically
34-
}
35-
36-
public virtual Task CreateAsync(TRole role)
37-
{
38-
return Task.Run(() => _Context.Roles.Insert(role));
39-
}
40-
41-
public virtual Task UpdateAsync(TRole role)
42-
{
43-
return Task.Run(() => _Context.Roles.Save(role));
44-
}
45-
46-
public virtual Task DeleteAsync(TRole role)
47-
{
48-
var queryById = Query<TRole>.EQ(r => r.Id, role.Id);
49-
return Task.Run(() => _Context.Roles.Remove(queryById));
50-
}
51-
52-
public virtual Task<TRole> FindByIdAsync(string roleId)
53-
{
54-
return Task.Run(() => _Context.Roles.FindOneByIdAs<TRole>(ObjectId.Parse(roleId)));
55-
}
56-
57-
public virtual Task<TRole> FindByNameAsync(string roleName)
58-
{
59-
var queryByName = Query<TRole>.EQ(r => r.Name, roleName);
60-
return Task.Run(() => _Context.Roles.FindOneAs<TRole>(queryByName));
61-
}
62-
}
3+
using System.Threading.Tasks;
4+
using global::MongoDB.Driver;
5+
using Microsoft.AspNet.Identity;
6+
7+
/// <summary>
8+
/// Note: Deleting and updating do not modify the roles stored on a user document. If you desire this dynamic
9+
/// capability, override the appropriate operations on RoleStore as desired for your application. For example you could
10+
/// perform a document modification on the users collection before a delete or a rename.
11+
/// </summary>
12+
/// <typeparam name="TRole"></typeparam>
13+
public class RoleStore<TRole> : IRoleStore<TRole>
14+
//IQueryableRoleStore<TRole>
15+
where TRole : IdentityRole
16+
{
17+
private readonly RolesContext<TRole> context;
18+
19+
public RoleStore(RolesContext<TRole> context)
20+
{
21+
this.context = context;
22+
}
23+
24+
//public virtual IQueryable<TRole> Roles
25+
//{
26+
// get { return context.Roles.AsQueryable<TRole>(); }
27+
//}
28+
29+
public virtual void Dispose()
30+
{
31+
// no need to dispose of anything, mongodb handles connection pooling automatically
32+
}
33+
34+
public virtual Task CreateAsync(TRole role)
35+
{
36+
// TODO: Unique index on role name
37+
38+
return context.Roles.InsertOneAsync(role);
39+
40+
//return Task.Run(() => context.Roles.Insert(role));
41+
}
42+
43+
public virtual Task UpdateAsync(TRole role)
44+
{
45+
// TODO: Unique index on role name
46+
return context.Roles.FindOneAndUpdateAsync(r => r.Id == role.Id,
47+
new ObjectUpdateDefinition<TRole>(role));
48+
//return Task.Run(() => context.Roles.Save(role));
49+
}
50+
51+
public virtual Task DeleteAsync(TRole role)
52+
{
53+
return context.Roles.FindOneAndDeleteAsync(r => r.Id == role.Id);
54+
55+
//var queryById = Query<TRole>.EQ(r => r.Id, role.Id);
56+
//return Task.Run(() => context.Roles.Remove(queryById));
57+
}
58+
59+
public virtual Task<TRole> FindByIdAsync(string roleId)
60+
{
61+
return context.Roles.Find(r => r.Id == roleId).FirstOrDefaultAsync();
62+
63+
//return Task.Run(() => context.Roles.FindOneByIdAs<TRole>(ObjectId.Parse(roleId)));
64+
}
65+
66+
public virtual Task<TRole> FindByNameAsync(string roleName)
67+
{
68+
return context.Roles.Find(r => r.Name == roleName).SingleOrDefaultAsync();
69+
70+
//var queryByName = Query<TRole>.EQ(r => r.Name, roleName);
71+
//return Task.Run(() => context.Roles.FindOneAs<TRole>(queryByName));
72+
}
73+
}
6374
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace AspNet.Identity.MongoDB
2+
{
3+
using global::MongoDB.Driver;
4+
5+
public class RolesContext<TRole>
6+
{
7+
public RolesContext(IMongoCollection<TRole> roles)
8+
{
9+
Roles = roles;
10+
}
11+
12+
public IMongoCollection<TRole> Roles { get; private set; }
13+
}
14+
}

0 commit comments

Comments
 (0)