Added action manager to reduce code duplication

This commit is contained in:
AaronJamesY 2017-01-25 20:02:01 +00:00
parent 76c51ee8b5
commit 9a114e5583
7 changed files with 109 additions and 53 deletions

View file

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankingBot.ActionManagers.AccountManagers
{
class AccountManager
{
}
}

View file

@ -0,0 +1,42 @@
using BankingBot.Attributes;
using BankingBot.Contracts;
using BankingBot.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankingBot.ActionManagers
{
public abstract class ActionManager
{
protected IBrowserBot BrowserBot { get; private set; }
public ActionManager(IBrowserBot browserBot)
{
BrowserBot = browserBot;
}
protected Type GetActionTypeFromInterface(object identifyingType, Type interfaceType)
{
var provider = ProviderIdentifier.GetProviderFromType(identifyingType.GetType());
// Get all types implementing the given interface
var typesImplementingInterface = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p =>
interfaceType.IsAssignableFrom(p) &&
p != interfaceType);
foreach (var type in typesImplementingInterface)
{
var typeProvider = ProviderIdentifier.GetProviderFromType(type);
if (typeProvider == provider)
return type;
}
return null;
}
}
}

View file

@ -5,7 +5,7 @@ using BankingBot.Contracts;
using BankingBot.LoginCredentials;
using OpenQA.Selenium;
namespace BankingBot.LoginManagers
namespace BankingBot.ActionManagers.LoginManagers
{
[ProviderIdentifier(Enums.Provider.Lloyds)]
public class LloydsLoginManager : IProviderLoginManager

View file

@ -0,0 +1,49 @@
using System;
using System.Linq;
using BankingBot.Attributes;
using BankingBot.Contracts;
namespace BankingBot.ActionManagers.LoginManagers
{
public class LoginManager : ActionManager, ILoginManager
{
public LoginManager(IBrowserBot browserBot)
: base(browserBot)
{ }
public void Login(ILoginCredentials credentials)
{
//var provLoginManagerType = GetProviderLoginManagerType(credentials);
//var provLoginManager = (IProviderLoginManager)Activator.CreateInstance(provLoginManagerType, BrowserBot);
//provLoginManager.Login(credentials);
var provLoginManagerType = GetActionTypeFromInterface(credentials, typeof(IProviderLoginManager));
var provLoginManager = (IProviderLoginManager)Activator.CreateInstance(provLoginManagerType, BrowserBot);
provLoginManager.Login(credentials);
}
//private Type GetProviderLoginManagerType(ILoginCredentials credentials)
//{
// // Get all "provider login manager" classes in the assembly
// // (any class that implements IProviderLoginManager)
// var providerLoginManagerTypes = AppDomain.CurrentDomain.GetAssemblies()
// .SelectMany(s => s.GetTypes())
// .Where(p =>
// typeof(IProviderLoginManager).IsAssignableFrom(p) &&
// p != typeof(IProviderLoginManager));
// var credentialsProvider = credentials.GetProvider();
// foreach (var type in providerLoginManagerTypes)
// {
// // Get associated provider for each "provider login manager" class
// var provider = ProviderIdentifier.GetProviderFromType(type);
// if (provider == credentialsProvider)
// {
// return type;
// }
// }
// return null;
//}
}
}

View file

@ -46,6 +46,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ActionManagers\AccountManagers\AccountManager.cs" />
<Compile Include="ActionManagers\ActionManager.cs" />
<Compile Include="Attributes\ProviderIdentifier.cs" />
<Compile Include="BrowserBot.cs" />
<Compile Include="Client.cs" />
@ -57,8 +59,8 @@
<Compile Include="Enums\Provider.cs" />
<Compile Include="LoginCredentials\LloydsLoginCredentials.cs" />
<Compile Include="LoginCredentials\LoginCredentials.cs" />
<Compile Include="LoginManagers\LloydsLoginManager.cs" />
<Compile Include="LoginManagers\LoginManager.cs" />
<Compile Include="ActionManagers\LoginManagers\LloydsLoginManager.cs" />
<Compile Include="ActionManagers\LoginManagers\LoginManager.cs" />
<Compile Include="Models\Account.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Responses\LoginResponse.cs" />

View file

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BankingBot.Contracts;
using BankingBot.LoginManagers;
using BankingBot.ActionManagers.LoginManagers;
using BankingBot.Models;
using OpenQA.Selenium;

View file

@ -1,49 +0,0 @@
using System;
using System.Linq;
using BankingBot.Attributes;
using BankingBot.Contracts;
using OpenQA.Selenium;
namespace BankingBot.LoginManagers
{
public class LoginManager : ILoginManager
{
private readonly IBrowserBot _browserBot;
public LoginManager(IBrowserBot browserBot)
{
_browserBot = browserBot;
}
public void Login(ILoginCredentials credentials)
{
var provLoginManagerType = GetProviderLoginManagerType(credentials);
var provLoginManager = (IProviderLoginManager)Activator.CreateInstance(provLoginManagerType, _browserBot);
provLoginManager.Login(credentials);
}
private Type GetProviderLoginManagerType(ILoginCredentials credentials)
{
// Get all "provider login manager" classes in the assembly
// (any class that implements IProviderLoginManager)
var providerLoginManagerTypes = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p =>
typeof(IProviderLoginManager).IsAssignableFrom(p) &&
p != typeof(IProviderLoginManager));
var credentialsProvider = credentials.GetProvider();
foreach (var type in providerLoginManagerTypes)
{
// Get associated provider for each "provider login manager" class
var provider = ProviderIdentifier.GetProviderFromType(type);
if (provider == credentialsProvider)
{
return type;
}
}
return null;
}
}
}