DependencyService, allow developer to define construction of class

AdamPAdamP AUUniversity ✭✭✭✭✭
edited October 20 in Xamarin.Forms Evolution

Summary

Allow the developer to define how the DependencyService will construct a class. This will involve a new Register method being created, and passing through a function, that can create the implementation.

API Changes

No existing API's will be modified, this will be an additional API.

public static void Register<T>(Func<T> implementation) where T : class

Intended Use Case

This will still keep the DependencyService class very light, but allow manual constructor injection, so that those developing small apps and don't want to bring in a 3rd party IoC container, but still use Constructor Injection, will be able to do so.

e.g.

Register<IInterface>(() => new Implementation(DependencyService.Get<IDependency>()));

or

Register<IInterface>(() => new Implementation(new Dependency()));

For anything more complex, moving to a IoC container with more features, would then be the recommended approach.

0
0 votes

Open · Last Updated

Posts

  • JoeMankeJoeManke USMember ✭✭✭

    I made my own container that does exactly this a couple years ago. If I may offer a suggestion, another couple convenience methods along the same lines:

        public static void Register<T>(T singleton)
        {
            Register(() => singleton);
        }
    
        public static void Register<TRegister, TConstruct>() where TConstruct : TRegister, new()
        {
            Register<TRegister>(() => new TConstruct());
        }
    
  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @JoeManke - the existing DependencyService, does a singleton or per request check on the Get, rather than on the Register. Hence we just want to define the ability to construct. Also I wanted to make sure it was a Func that is passed, not an instance, so that it is only created when first called, not on register.

    But I think the main thing here is to keep this as simple as possible. I don't want to replace IoC containers, just make the Xamarin.Forms one support CI at a bare minimum.

  • MichaelRumplerMichaelRumpler ATMember ✭✭✭✭

    I suggest not to reinvent the wheel and use the proved concepts of either XLabs.Ioc or ASP.NET Core (src, concept).

    Both can be installed via NuGet (without XF dependency).

    I don't use the XF.DependencyService because of its limited functionality and also some comments from Xamarin that they don't recommend it themselves and don't invest any effort in it.

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    It would be reinventing the wheel if I was suggesting it was created from scratch but it's already here, and while it is very simple, it's used extensively in their samples, and lots of people, hence use it.

    This is just a very small update to add a final missing piece. Not suggesting it would be taken any further than that, but may help the many people who use the inbuilt to start using CI.

Sign In or Register to comment.