Skip to content
This repository was archived by the owner on Aug 27, 2020. It is now read-only.

IDependencyService

Mark Smith edited this page Sep 2, 2016 · 4 revisions

IDependencyService

The IDependencyService interface is a simple representation of a Service Locator. The library provides a default implementation (DependencyServiceWrapper which wraps the Xamarin.Forms static DependencyService. However you can create your own and register it through the XamUInfrastructure.Init method.

Methods

  • Get<T> : returns a T object.
  • Register<T> : registers a T` object.
  • Register<T,TImpl> : registers a TImplobject as aT` abstraction.

Example

Here is a sample implementation that wraps the Microsoft Unity container:

public class UnityWrapper : IDependencyService
{
    UnityContainer container;

    public UnityWrapper(UnityContainer container)
    {
       this.container = container;
    }

    public T Get<T>() where T : class
    {
        return container.Resolve<T>();
    }

    public void Register<T> () where T : class, new()
    {
        container.RegisterType<T>();
    }

    public void Register<T, TImpl> () 
        where T : class
        where TImpl : class, T, new()
    {
        container.RegisterType<T,TImpl>();
    }
}