tugberkugurlu / AspNet.Identity.RavenDB

Fully asynchronous, new and sweet ASP.NET Identity implementation for RavenDB
MIT License
42 stars 28 forks source link

RavenDb & uniquness #3

Closed tungchau closed 10 years ago

tungchau commented 11 years ago

Hi, I think the functions FindAsync/CreateAsync in RavenUserStore are not stable. In my previous projects, I had a lot of troubles with stale data, even with static indexes. IMHO, a Raven "style" will be better. Let's treat RavenUserLogin as independent document

public class RavenUserLogin
    {
        const string KEY_TEMPLATE = "RavenUserLogin/{0}/{1}";
        public RavenUserLogin()
        {
        }

        public RavenUserLogin(UserLoginInfo loginInfo, string userId)
        {
            Id = GenerateKey(loginInfo);
            UserId = userId;
        }

        public string Id { get; set; }

        public string UserId { get; set; }        

        public static string GenerateKey(UserLoginInfo loginInfo)
        {
            return string.Format(KEY_TEMPLATE, loginInfo.LoginProvider, loginInfo.ProviderKey);
        }
    }

Then,

public async Task<TUser> FindAsync(UserLoginInfo login)
        {
            var lg = await DocumentSession
                .Include<RavenUserLogin>(x=>x.UserId)
                .LoadAsync<RavenUserLogin>(RavenUserLogin.GenerateKey(login));
            return (lg != null) ? await DocumentSession.LoadAsync<TUser>(lg.UserId) : null;            
        }

Best, T.C

tugberkugurlu commented 10 years ago

Hi @tungchau,

That's actually a nice idea. Can you send a PR?