Create a gist now

Instantly share code, notes, and snippets.

MigrationResolver.cs
/*
The MIT License (MIT)
Copyright (c) 2015 Agile Sports Technologies, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
namespace Hudl.Users.Migration.Control
{
// Prior to refactoring for the migration, higher-layer classes (e.g. MVC controllers)
// called domains directly. Those domain references were replaced with calls to this
// resolver, which conditionally returns an implementation after looking at the user
// ID being operated on and the current migration state.
//
// It's a bit heavy on the interfaces, mostly just to make it work with the Unity DI
// container that we use throughout the code.
public class MigrationResolver : IMigrationResolver
{
private readonly IMigrationUserCreationDomain _mongoUserCreationDomain;
private readonly IMigrationUserRolesDomain _mongoUserRolesDomain;
private readonly IMigrationUserDomain _mongoUserDomain;
private readonly IMigrationUserUpdateDomain _mongoUserUpdateDomain;
private readonly IMigrationUserCredentialsDomain _mongoUserCredentialsDomain;
private readonly IMigrationUserCreationDomain _sqlUserCreationDomain;
private readonly IMigrationUserRolesDomain _sqlUserRolesDomain;
private readonly IMigrationUserDomain _sqlUserDomain;
private readonly IMigrationUserUpdateDomain _sqlUserUpdateDomain;
private readonly IMigrationUserCredentialsDomain _sqlUserCredentialsDomain;
private readonly IHybridUserLookupDomain _hybridUserLookupDomain;
private readonly IHybridUserMergeDomain _hybridUserMergeDomain;
private readonly IHybridUserMetaDomain _hybridUserMetaDomain;
private readonly IHybridUserSearchDomain _hybridUserSearchDomain;
public MigrationResolver(
IMongoUserCreationDomain mongoUserCreationDomain,
IMongoUserRolesDomain mongoUserRolesDomain,
IMongoUserDomain mongoUserDomain,
IMongoUserUpdateDomain mongoUserUpdateDomain,
IMongoUserCredentialsDomain mongoUserCredentialsDomain,
ISqlUserCreationDomain sqlUserCreationDomain,
ISqlUserRolesDomain sqlUserRolesDomain,
ISqlUserDomain sqlUserDomain,
ISqlUserUpdateDomain sqlUserUpdateDomain,
ISqlUserCredentialsDomain sqlUserCredentialsDomain,
IHybridUserLookupDomain hybridUserLookupDomain,
IHybridUserMergeDomain hybridUserMergeDomain,
IHybridUserMetaDomain hybridUserMetaDomain,
IHybridUserSearchDomain hybridUserSearchDomain
)
{
_mongoUserCreationDomain = mongoUserCreationDomain;
_mongoUserRolesDomain = mongoUserRolesDomain;
_mongoUserDomain = mongoUserDomain;
_mongoUserUpdateDomain = mongoUserUpdateDomain;
_mongoUserCredentialsDomain = mongoUserCredentialsDomain;
_sqlUserCreationDomain = sqlUserCreationDomain;
_sqlUserRolesDomain = sqlUserRolesDomain;
_sqlUserDomain = sqlUserDomain;
_sqlUserUpdateDomain = sqlUserUpdateDomain;
_sqlUserCredentialsDomain = sqlUserCredentialsDomain;
_hybridUserSearchDomain = hybridUserSearchDomain;
_hybridUserMergeDomain = hybridUserMergeDomain;
_hybridUserMetaDomain = hybridUserMetaDomain;
_hybridUserLookupDomain = hybridUserLookupDomain;
}
public IMigrationUserCreationDomain GetUserCreationDomain()
{
return UserMigrationState.Instance.ShouldNewUsersBeCreatedInMongo()
? _mongoUserCreationDomain
: _sqlUserCreationDomain;
}
public IMigrationUserMergeDomain GetUserMergeDomain()
{
return _hybridUserMergeDomain;
}
public IMigrationUserMetaDomain GetUserMetaDomain()
{
return _hybridUserMetaDomain;
}
public IMigrationUserRolesDomain GetUserRolesDomain(string userId)
{
UserMigrationState.Instance.ThrowIfUserIsLocked(userId);
return UserMigrationState.Instance.IsUserMigrated(userId) ? _mongoUserRolesDomain : _sqlUserRolesDomain;
}
public IMigrationUserSearchDomain GetUserSearchDomain()
{
return _hybridUserSearchDomain;
}
public IMigrationUserDomain GetUserDomain(string userId)
{
return UserMigrationState.Instance.IsUserMigrated(userId) ? _mongoUserDomain : _sqlUserDomain;
}
public IMigrationUserLookupDomain GetUserLookupDomain()
{
return _hybridUserLookupDomain;
}
public IMigrationUserUpdateDomain GetUserUpdateDomain(string userId)
{
UserMigrationState.Instance.ThrowIfUserIsLocked(userId);
return UserMigrationState.Instance.IsUserMigrated(userId) ? _mongoUserUpdateDomain : _sqlUserUpdateDomain;
}
public IMigrationUserCredentialsDomain GetUserCredentialsDomain(string userId)
{
return UserMigrationState.Instance.IsUserMigrated(userId) ? _mongoUserCredentialsDomain : _sqlUserCredentialsDomain;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment