mercredi 19 avril 2017

Interfaces: Convert My existing concrete code to an abstract code

I am working on a UWP app. I have a PCL that has managers and services. My managers interact with my services and provide the output. In my services I use async await calls for interacting with my API. I've created a dummy solution. The code is as below:

My Dummy Managers:

public class AccountManager
{

    public string uniqueId { get; set; }

    public int GetAccountId()
    {
        Services.AccountServices HelloAccount = new Services.AccountServices();
        return HelloAccount.GenerateAccountId(uniqueId);
    }
}

public class DummyManager
{

    public ICollection<string> GetDeviceNames(int accountId)
    {
        Services.NameService MyNameService = new Services.NameService(accountId);
        return MyNameService.ProvideNames();
    }
}

My Dummy Services:

 internal class NameService
{
    public NameService(int Id)
    {
        AccountId = Id;
    }

    public int AccountId = 0;

    public ICollection<string> ProvideNames()
    {
        return new List<string>()
        {
            "Bob",
            "James",
            "Foo",
            "Bar"
        };
    }

}

 internal class AccountServices
{

    public int GenerateAccountId(string uniqueID)
    {
        return 11;
    }

}

Now that I have my services and managers the same structure as I use them, below is how I interact with my Public Managers and keeping the services internal:

In my UI MainPage CodeBehind:

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        DataServices.Managers.AccountManager Hello = new DataServices.Managers.AccountManager();
        Hello.uniqueId = "AsBbCc";    //fetched from another service.
        var id = Hello.GetAccountId();

        DataServices.Managers.DummyManager Dummy = new DataServices.Managers.DummyManager();
        var names = Dummy.GetDeviceNames(id);
    }


My Question is currently my MainPage is very Tightly coupled with my manager and even if I use the MVVM pattern, then my ViewModel would be Tightly coupled with my managers. How do I add a layer of abstraction? What out of these entities (Managers, services, DataBank) should be an Interface that helps to provide abstraction? I need help. I've uploaded a dummy solution for the same. Thanks :)

My Entire dummy solution for better understanding.

Aucun commentaire:

Enregistrer un commentaire