Dependencies in Xamarin Forms

Dependency Injection and Service Location, in the .NET world, is becoming widely accepted as a flexible way to link resources between classes dynamically. One of the many frameworks out on the market provide a very flexible way to locate dependencies or build up an object via constructor/property injection. Some of these containers also contain a bootstrapper component that make registration in the application easier.

There are two components to this: the type registration, and the type location. Let’s take a look at both of these separately.

Type Registration

There are a variety of ways to do this. Outside of defining the registrations manually, there are other frameworks out there that make defining dependencies easier. For instance, I’ve recently created a product called TEF (typed extensibility framework), which is similar to MEF, but for types. By marking a type as a dependency using assembly attributes, or by marking the type with an exportable attribute, the system extracts these types from the assembly and makes them available for registration in the container. Below is an example of how you can mark a type as exportable:

public interface ISomeType { .. }

[ExportType(typeof(ISomeType))]
public class SomeType : ISomeType { .. }

OR

<assembly:ExportDependency(GetType(ISomeType), GetType(SomeType))>

The system can then parse the assemblies in the application, extracting out all of the individual types. I go through this example because the DependencyService registers components in the assembly in a very similar way. It follows a similar design to TEF for registration; however, it only supports assembly attributes as in the following example:

[assembly: Xamarin.Forms.Dependency (typeof (SomeType))]

Marking all of your dependencies will automatically import them into the dependency service.

Type Retrieval

The Xamarin Forms Dependency Service has a similarity to ASP.NET MVC’s IDependencyResolver, which has the same purpose of supplying types to the various components.

This is not the same as a dependency injection container; it is a service locator designed to locate references. For instance, if you have this page:

public partial class DependencyPage : ContentPage
	{
	private ICacheService _cache = null;
	private IDependentService _dependent = null;

	public DependencyPage (ICacheService cache, IDependentService dependent)
	{
		InitializeComponent ();

		_cache = cache;
		_dependent = dependent;
	}

}

Notice it has two constructor dependencies. The following will produce an error with the dependency service.

var page = DependencyService.Get (DependencyFetchTarget.NewInstance);

With a Dependency Injection container, the above code would actually work because the container can handle cascading dependencies. Instead, to use DependencyService, it would have to be created this way:

var page = new DependencyPage(
    DependencyService.Get (DependencyFetchTarget.NewInstance), 
    DependencyService.Get (DependencyFetchTarget.NewInstance)
);

for the page to work. If anyone has any tips, or if there are inconsistencies, please post a comment and I’ll update the post.

Advertisements

2 thoughts on “Dependencies in Xamarin Forms

  1. Pingback: Using TEF in Xamarin Forms | On All Things Web

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s