From 9a114e558366bb659d9c84e65307cab64e4155f2 Mon Sep 17 00:00:00 2001 From: AaronJamesY Date: Wed, 25 Jan 2017 20:02:01 +0000 Subject: [PATCH] Added action manager to reduce code duplication --- .../AccountManagers/AccountManager.cs | 12 +++++ BankingBot/ActionManagers/ActionManager.cs | 42 ++++++++++++++++ .../LoginManagers/LloydsLoginManager.cs | 2 +- .../LoginManagers/LoginManager.cs | 49 +++++++++++++++++++ BankingBot/BankingBot.csproj | 6 ++- BankingBot/Client.cs | 2 +- BankingBot/LoginManagers/LoginManager.cs | 49 ------------------- 7 files changed, 109 insertions(+), 53 deletions(-) create mode 100644 BankingBot/ActionManagers/AccountManagers/AccountManager.cs create mode 100644 BankingBot/ActionManagers/ActionManager.cs rename BankingBot/{ => ActionManagers}/LoginManagers/LloydsLoginManager.cs (98%) create mode 100644 BankingBot/ActionManagers/LoginManagers/LoginManager.cs delete mode 100644 BankingBot/LoginManagers/LoginManager.cs diff --git a/BankingBot/ActionManagers/AccountManagers/AccountManager.cs b/BankingBot/ActionManagers/AccountManagers/AccountManager.cs new file mode 100644 index 0000000..2d16c87 --- /dev/null +++ b/BankingBot/ActionManagers/AccountManagers/AccountManager.cs @@ -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 + { + } +} diff --git a/BankingBot/ActionManagers/ActionManager.cs b/BankingBot/ActionManagers/ActionManager.cs new file mode 100644 index 0000000..d64c30c --- /dev/null +++ b/BankingBot/ActionManagers/ActionManager.cs @@ -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; + } + } +} diff --git a/BankingBot/LoginManagers/LloydsLoginManager.cs b/BankingBot/ActionManagers/LoginManagers/LloydsLoginManager.cs similarity index 98% rename from BankingBot/LoginManagers/LloydsLoginManager.cs rename to BankingBot/ActionManagers/LoginManagers/LloydsLoginManager.cs index 0c6a722..e902991 100644 --- a/BankingBot/LoginManagers/LloydsLoginManager.cs +++ b/BankingBot/ActionManagers/LoginManagers/LloydsLoginManager.cs @@ -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 diff --git a/BankingBot/ActionManagers/LoginManagers/LoginManager.cs b/BankingBot/ActionManagers/LoginManagers/LoginManager.cs new file mode 100644 index 0000000..ffdab4d --- /dev/null +++ b/BankingBot/ActionManagers/LoginManagers/LoginManager.cs @@ -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; + //} + } +} diff --git a/BankingBot/BankingBot.csproj b/BankingBot/BankingBot.csproj index 7caa1b3..5e07ae3 100644 --- a/BankingBot/BankingBot.csproj +++ b/BankingBot/BankingBot.csproj @@ -46,6 +46,8 @@ + + @@ -57,8 +59,8 @@ - - + + diff --git a/BankingBot/Client.cs b/BankingBot/Client.cs index db3623f..5e2d749 100644 --- a/BankingBot/Client.cs +++ b/BankingBot/Client.cs @@ -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; diff --git a/BankingBot/LoginManagers/LoginManager.cs b/BankingBot/LoginManagers/LoginManager.cs deleted file mode 100644 index d5573c4..0000000 --- a/BankingBot/LoginManagers/LoginManager.cs +++ /dev/null @@ -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; - } - } -}