Bug 34708 - DependencyService.Register<>() method should also allow supplying the implementation class as a parameter
Summary: DependencyService.Register<>() method should also allow supplying the impleme...
Status: CLOSED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.5.0
Hardware: All All
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-10-08 21:46 UTC by Matt Jaeger
Modified: 2015-10-09 21:15 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
CLOSED ANSWERED

Description Matt Jaeger 2015-10-08 21:46:59 UTC
Currently in order to use the DependencyService, you must implement your custom interface, and the only way to register it with the DependencyService is via the "[assembly: Dependency(typeof(Class))]" annotation.

This prevents defining the real implementation class for all platforms within the PCL and a mock implementation class within my unit test project. This is helpful for interfaces that are not platform specific, but I would like to use the DependencyService to provide a mock implementation during unit tests.

Currently in order to achieve this, I would have to copy the implementation class for every platform even if it is the same and export it from each platform dll. I would much rather define the implementation class once in the PCL, and then register the real implementation in my PCL App.xaml.cs and then provide and register a mock implementation in my unit test project.
Comment 1 Rui Marinho 2015-10-09 06:40:14 UTC
You can register using the static helper and not the attribute.

https://developer.xamarin.com/api/type/Xamarin.Forms.DependencyService/

DependecyService.Register<T>(T,TIMPL)
Comment 2 Matt Jaeger 2015-10-09 21:15:27 UTC
Completely missed that. Thanks!