简体   繁体   中英

C# Interface? categories methods with the same name

how to do something like this. So the scenario is I have 2 "DoLogin" methods in a different platform (Mobile Version, Desktop Version)

I want to make the code more readable and look something like this

Ex.

if I wanted to login to a desktop version.

webDriver.Desktop.Dologin(accountModel)

If I wanted to login to a mobile version.

webDriver.Mobile.DoLogin(accountModel)

this is what I currently have.

public static class Desktop
{
    public static void DoLogin(this ChromeDriver webDriver, AccountModel account)
    {

    }
}

public static class Mobile
{
    public static void DoLogin(this ChromeDriver webDriver, AccountModel account)
    {

    }
}

EDIT

This is what I'm currently doing to organize it.

public static class Desktop
    {
        public static void DesktopDoLogin(this ChromeDriver webDriver, AccountModel account)
        {

        }
    }

    public static class Mobile
    {
        public static void MobileDoLogin(this ChromeDriver webDriver, AccountModel account)
        {

        }
    }

whenever I call a method let say desktop what I do is.

webDriver.DesktopDoLogin(account)

or

webDriver.MobileDoLogin(account)

I guess whats important is it works.

I personally don't like using extension methods. This is a preference. I would have most likely implemented it as follows. I have never used Selenium before, so I'm not sure what interface WebDriver is/implements but you can get the gist of the pattern.

public interface IPlatform
{
    void Login<T>(T model);
}

public class Desktop : IPlatform
{
    private readonly WebDriver _webDriver;
    public Desktop(WebDriver driver)
    {
        _webDriver = driver;
    }
    public void Login<T>(T model)
    {
        // do login here
    }
}
// usage
IPlatform desktop = new Desktop(/*chromedriver*/); // or inject
desktop.Login<AccountModel>(model);

It seems to me that you can get this:

void Main()
{
    var webDriver = new ChromeDriver();

    webDriver.Desktop.DoLogin(new AccountModel());
    webDriver.Mobile.DoLogin(new AccountModel());
}

By doing this:

public interface IDoLogin
{
    void DoLogin(AccountModel account);
}

public class Desktop : IDoLogin
{
    private ChromeDriver _webDriver;

    public Desktop(ChromeDriver webDriver)
    {
        _webDriver = webDriver;
    }

    public void DoLogin(AccountModel account) { }
}

public class Mobile : IDoLogin
{
    private ChromeDriver _webDriver;

    public Mobile(ChromeDriver webDriver)
    {
        _webDriver = webDriver;
    }

    public void DoLogin(AccountModel account) { }
}

public class ChromeDriver
{
    public Desktop Desktop;
    public Mobile Mobile;

    public ChromeDriver()
    {
        this.Desktop = new Desktop(this);
        this.Mobile = new Mobile(this);
    }
}

You can do it easily using some static voids like that:-

Public class desktop {
    Public static void DoLogin(AccountModel val){
        //code for desktop
    }
}

Public class mobile {
    Public static void DoLogin(AccountModel val){
        //code for mobile
    }
}

Public class webDriver{
    Public static desktop Desktop;
    Public static mobile Mobile;
}

//usage
webDriver.mobile.DoLogin(...);
//or
mobile.DoLogin();

Why not using Interface?

Create one common interface with DoLogin Method, and Implement that interface in Desktop and Mobile class.

Common Interface:

public interface ICommon
 {
   void Login<T>(T model);
 }

Implement this interface in your Desktop and Mobile Class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM