senzacionale
senzacionale

Reputation: 20906

C# DependencyService in Xamarin.Forms portable class

I would like to use DependencyService using Xamarin.Forms in a Portable Class Library. In this case, the PCL is my business logic, and thus, I don't have access to nor want to install Xamarin.Forms in this project.

What I'm trying to do in my business logic is:

_login = DependencyService.Get<ILogin>();

I am using SimpleContainer for registration of my services.

My question is thus, which Service Locator can I use in my business logic project?

Upvotes: 2

Views: 2751

Answers (2)

Pete
Pete

Reputation: 4746

If you don't want to install Xamarin.Forms in your PCL you can still program against your platform-specific implementations in your PCL.

Normally, like you mentioned, you could access the platform-specific class instance in your PCL project using something like:-

_login = DependencyService.Get<ILogin>();

However this creates a dependency on Xamarin.Forms.

To get around this you can implement your interface ILogin in the PCL.

You will then create a static class with either one of the following, depending if it is a global reference (with only one instance ever existing), or whether you want a new instance of your platform-specific class implementation instantiated for use within the PCL project:-

Option 1 : Create a single, global instance, that every call in your PCL goes through

To do this, add the following to your PCL:-

public static MyGlobal
{
    public static ILogin MyLoginGlobalInstance;
}

Then in your initialization of your platform-specific iOS and Android projects you can set this global property in your PCL matching a new instance created in each of the platform-specific projects, such as:-

MyPCLProject.MyGlobal.MyLoginGlobalInstance = new Login();

By creating it on the local platform-specific project, and setting this in the PCL, you can then access the instance within the PCL according to your interface as you have defined in ILogin along the lines of:-

MyPCLProject.MyGlobal.MyLoginGlobalInstance.{some interface member}

Option 2 : Create a new instance of your platform-specific class instance, every time in your PCL.

If however, you want to create a new instance of your platform-specific class everytime you need it, in your PCL, you can do the following.

In your PCL create the following:-

public static MyGlobal
{
    public static Func<ILogin> CreateNewInstance_Login;
}

Then, in your platform-specific project you can initialize this function with something along the lines of:-

MyPCLProject.MyGlobal.CreateNewInstance_Login = new Func<ILogin>(()=>
{
    return new Login();
}

Then to consume and create a new instance each time within your PCL you create a new instance by calling:-

ILogin objMyLoginInstance = MyPCLProject.MyGlobal.CreateNewInstance_Login();

objMyLoginInstance.{some interface member}

Upvotes: 7

Stephane Delcroix
Stephane Delcroix

Reputation: 16222

It's unclear what you're trying to achieve exactly, but I take that, because of your choices in project design, the assembly implementing the services does not depend on Xamarin.Forms and thus you can't register them, from within the assembly, with the [Dependency] attribute.

As an alternative, you can register services in the DependencyService using DependencyService.Register<T> ();. Once the service is register, you can access it with a call to DependencyService.Get<T> ();

Depending on your solution architecture, it might help you or not.

Upvotes: 2

Related Questions