Getting Started with Dependency Injection in Xamarin.Forms

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ส.ค. 2024
  • What do you do when you want to create testable code or when you need to access native features? You need an interface to implement! Then you need a way to inject that implementation (dependency) to access it from your shared code (or anywhere). You will do this pretty often with mobile application development and luckily for us Xamarin.Forms has it all built in! Today' we take a look at Dependency Injection for Xamarin.Forms to create testable services and also access native features.
    Full Xamarin Beginner's Playlist - • Xamarin & Xamarin.Form...
    👕 Buy some swag! - jamesmontemagno.myspreadshop....
    Chapters:
    00:00 - Intro, what is dependency injection?
    03:20 - Dependency injection 101 - Create, register, resolve
    13:25 - Accessing native features with dependency injection
    17:30 - Wrap-up, IoC, other DI libraries
    Links:
    * My Coffee App GitHub - github.com/jamesmontemagno/my...
    * Xamarin.Forms Dependency Service - docs.microsoft.com/xamarin/xa...
    Looking to use ASP.NET Core style DI with Host Builder? This is the .NET MAUI way as well! Take a look at my blog: montemagno.com/add-asp-net-co...
    What is on my hat? It is the CLE clothing logo because I am from Cleveland! Checkout their awesome CLE merch: cleclothingco.myshopify.com/
    What is that art on my wall? It is an original piece from the French street artist Gregos of La Butte Montmartre: / bcez1onhiqx
    Follow:
    👨‍💻 GitHub: github.com/jamesmontemagno
    🦜 Twitter: / jamesmontemagno
    🔴 Twitch: / jamesmontemagno
    📄 Website: www.montemagno.com
    📰 Newsletter: newsletter.montemagno.com/
    My Setup:
    * Blue Spark Microphone - amzn.to/3qgtYkq
    * Blue Pop Filter - amzn.to/3jEWM3r
    * Rode Microphone Arm - amzn.to/2Z68AlE
    * Sony MDR7306 Headphones - amzn.to/372jxta
    * Stream Deck - amzn.to/373Uk1n
    * Elgato Cam Link - amzn.to/3a9eGbh
    * GoPro Hero - amzn.to/374lm90
    * MX Master 2S Mouse - amzn.to/3d7J2gj
    * Tecware Phantom Keyboard - amzn.to/3aUP4y9
    Disclaimer: This channel, videos, and streams are created in my spare time and are a product of me... James Montemagno! They are NOT officially affiliated or endorsed by Microsoft (my employer) in any way. Opinions and views are my own.
    #DependencyInjection #XamarinForms #DI #IOC
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 47

  • @davejarcher
    @davejarcher 3 ปีที่แล้ว +1

    Thanks James! It’s great to get some clarity for both platform specific and custom interfaces, as well as understanding the best approach given the imminent launch of Maui.
    Thanks again!

  • @oscarestrada4509
    @oscarestrada4509 ปีที่แล้ว

    I'm so grateful for your videos. Thank you! 🙌

  • @zarokoshin4090
    @zarokoshin4090 3 ปีที่แล้ว

    Thanks James !! Just what i was looking for, the notification message ! :)

  • @SolidSnake2025
    @SolidSnake2025 3 ปีที่แล้ว +5

    I would not call it Dependency “Injection” at how it works it’s more like Dependency “Retrieval”. In net core they made a great job and here, well, it is cool and works, but it is not automatically satisfied in the constructor.
    All in all your videos are just great and neat. You gained a new subscriber

  • @GellClan
    @GellClan 3 ปีที่แล้ว +1

    Thx for this, very helpful video.

  • @zeetsoft
    @zeetsoft 2 ปีที่แล้ว

    Very helpful playlist. Thank you so much.

  • @oOserkanCakmakOo
    @oOserkanCakmakOo 3 ปีที่แล้ว

    Thanks James

  • @marna_li
    @marna_li 3 ปีที่แล้ว +2

    I prefer using Microsoft.Extensions.DependencyInjection, with the Generic Host. It takes some more plumbing, but it works well.
    The main drawbacks of Xamarin.Forms are that there are different service providers for Dependencies and for XF infrastructure - different containers. This means that you cannot inject your own services into, for instance, a view or a markup extensions. A workaround is to declare static that is being set in App.cs.
    I don't know whether, or not, Maui will consolidate all to one container and service provider now when they use the Host Builder.

    • @JamesMontemagno
      @JamesMontemagno  3 ปีที่แล้ว +3

      So, the great part with .NET MAUI is that it will be using Microsoft.Extensions.DependencyInjection, which should really streamline this.

  • @kosteash
    @kosteash 3 ปีที่แล้ว +1

    wow, tnx James :)
    I thought all the time DepependecyService works only with CustomControls in Android and iOS

  • @cemalgulbeyaz
    @cemalgulbeyaz 3 ปีที่แล้ว +3

    What if i want to implement 2 (or more) instances of service from interface, how can i use this? Example: FileLogger and MailLogger form ILogger or MS-SQL and SQLite from IDatabase.

    • @JamesMontemagno
      @JamesMontemagno  3 ปีที่แล้ว

      You could used a typed interface most likely to get a specific one back. Normally this isn't the case you have an interface for a specific service. When you ask for an interface implementation back you get a single one. You could not use interfaces and just register the class itself.

  • @diegoronkkomaki6858
    @diegoronkkomaki6858 3 ปีที่แล้ว

    Nice video! Question though: If I'm calling DependancyService.Get() inside the class itself, then how can you inject the dependancy later, when unit testing the class, for example? I might be missing something but it seems like the injection part is missing in this kind of implementation of the dependancy injection.

    • @JamesMontemagno
      @JamesMontemagno  3 ปีที่แล้ว

      You would register a different implementation in your unit test

  • @jeffybanjo
    @jeffybanjo 3 ปีที่แล้ว +1

    Beautiful presentation as usual, thanks greatly. May I humbly request you make an IOS version of the Toast please or point us to materials on that, its greatly required! Thank you

    • @JamesMontemagno
      @JamesMontemagno  3 ปีที่แล้ว +1

      Take a look at channel9.msdn.com/Shows/XamarinShow/SnackBar--Toast-Xamarin-Community-Toolkit

  • @Samossam
    @Samossam 3 ปีที่แล้ว

    What is the blog article you mention regarding IoC for Windows?

    • @JamesMontemagno
      @JamesMontemagno  3 ปีที่แล้ว

      I think i was talking about this one montemagno.com/add-asp-net-cores-dependency-injection-into-xamarin-apps-with-hostbuilder/

  • @jothiprakash5805
    @jothiprakash5805 2 ปีที่แล้ว

    I have created my interfaces and classes in another .net standard class library and added refernce to xamarin forms project
    and DependencyService.Get returns null

  • @stephenhauck
    @stephenhauck 3 ปีที่แล้ว

    So I have two questions ...
    1) Should I be using .NET Core DI instead of Xamarin for future proofing ?
    2) How do I implement a view model locator with Xamarin DI ?

    • @JamesMontemagno
      @JamesMontemagno  3 ปีที่แล้ว

      1.) Doesn't matter, they are so easy to swap beteen.
      2.) Tons of examples out there probably like -> github.com/codemillmatt/XamFormsVMNav

  • @mestutosisptheophile8991
    @mestutosisptheophile8991 2 ปีที่แล้ว

    how could i use dependecyService to print string ??? thanks alot.

    • @JamesMontemagno
      @JamesMontemagno  2 ปีที่แล้ว

      You don't need that at all just use Console.WriteLine or Debug.WriteLine

  • @akramtalmoudi6374
    @akramtalmoudi6374 2 ปีที่แล้ว

    how to implement interface from shared code in android ??

  • @winged11
    @winged11 3 ปีที่แล้ว

    Hi
    I would like to be an app developer
    Where should I start , and what should I learn first ?

    • @JamesMontemagno
      @JamesMontemagno  3 ปีที่แล้ว

      Learn C# first and then you can build anything! dotnet.microsoft.com/learn/videos

  • @greeksinmars
    @greeksinmars 3 ปีที่แล้ว

    Why not use Autofac ? If I have parameters in constructors ?

    • @JamesMontemagno
      @JamesMontemagno  3 ปีที่แล้ว +5

      So, would probably look at using Microsoft.Extensions -> montemagno.com/add-asp-net-cores-dependency-injection-into-xamarin-apps-with-hostbuilder/ this will be in to .NET MAUI and has it built in. Autofac can totally also be used like I said at the end. I like to show off what is included in the box.

    • @greeksinmars
      @greeksinmars 3 ปีที่แล้ว

      @@JamesMontemagno Thanks James...

  • @dotnetdevni
    @dotnetdevni 3 ปีที่แล้ว

    U should show how to get it
    From an abstract class i only no cause dotnetcore

  • @williamprogramer4168
    @williamprogramer4168 3 ปีที่แล้ว

    ⭐⭐⭐⭐⭐

  • @john-blair
    @john-blair ปีที่แล้ว

    Doesn't seem to do constructor injection?

    • @JamesMontemagno
      @JamesMontemagno  ปีที่แล้ว

      .NET MAUI's does -> th-cam.com/video/xx1mve2AQr4/w-d-xo.html

  • @adinwashere
    @adinwashere 3 ปีที่แล้ว

    This is similar but different to Dependency Injection in other C# project types (MVC, WebAPI, Razor Pages...)

    • @JamesMontemagno
      @JamesMontemagno  3 ปีที่แล้ว +2

      .NET MAUI will actually use the same DI as those! YOu can use it today if you desire -> montemagno.com/add-asp-net-cores-dependency-injection-into-xamarin-apps-with-hostbuilder/

    • @adinwashere
      @adinwashere 3 ปีที่แล้ว

      @@JamesMontemagno oh. Cool!

  • @pharazfadaei
    @pharazfadaei 3 ปีที่แล้ว +1

    I don't see how this way of getting services from DependencyService can be considered dependency injection. There is no injection happening here. This seems more of a service locator to me.

    • @JamesMontemagno
      @JamesMontemagno  3 ปีที่แล้ว +1

      When I think of Dependency Injection it is a broad topic: service locator, container, inversion of control, dependency resolution, construction injection... aka there are a lot of words and different features that developers like. I like to document what is in the box for devs to use. As i mentioned at the end there are a lot of libraries out there and even a way to use the new HostBuilder model(which .NET MAUI will use) in your Xamarin.Forms apps today montemagno.com/add-asp-net-cores-dependency-injection-into-xamarin-apps-with-hostbuilder/

    • @pharazfadaei
      @pharazfadaei 3 ปีที่แล้ว +1

      @@JamesMontemagno I read your blog post and I also believe that if proper dependency injection is required, generic hosts are the way to go. However, afaik service locator pattern is not an approach to dependency injection. Dependency injection and service locator are two different approaches to dependency resolution and decoupling. I believe this is the reason "dependency injection" is not mentioned in the official documentation of DependencyService at all. Here is a great article that explains the differences between the two patterns: martinfowler.com/articles/injection.html#ServiceLocatorVsDependencyInjection

    • @johnybravo1677
      @johnybravo1677 3 ปีที่แล้ว

      Lol, u're right, since DependencyService in Xamarin => is just a Service Locator and nothing more :)
      So actually it's not DI :))

  • @atreides07
    @atreides07 3 ปีที่แล้ว +2

    what is the point of injecting not through a constructor? DI is needed for convenient testing. How to cover an object with tests, where dependencies are resolved not through the constructor/property but through DependecyService.Get inside object? Covering code with DependecyService with tests is more difficult and imposes a lot of restrictions than code with a constructor

    • @JamesMontemagno
      @JamesMontemagno  3 ปีที่แล้ว +1

      When I think of Dependency Injection it is a broad topic: service locator, container, inversion of control, dependency resolution, construction injection... aka there are a lot of words and different features that developers like. I like to document what is in the box for devs to use. As i mentioned at the end there are a lot of libraries out there and even a way to use the new HostBuilder model(which .NET MAUI will use) in your Xamarin.Forms apps today montemagno.com/add-asp-net-cores-dependency-injection-into-xamarin-apps-with-hostbuilder/

  • @anttipellinen
    @anttipellinen 3 ปีที่แล้ว +1

    First!

  • @DigitalElectronicSchool
    @DigitalElectronicSchool 3 ปีที่แล้ว

    HI MR JAMES
    var imageData = await AzureStorage.GetFileAsync(ContainerType.images, urlp);
    ImgX.Source = ImageSource.FromStream(() => new MemoryStream(imageData));
    THAT IS FOR IMAGE>>
    WHAT ABOUT VIDEO FROM AZURE BLOB STORAGE
    HOW CAN I PLAY VIDEO FROM AZURE BLOB STORAGE SECURE

    • @JamesMontemagno
      @JamesMontemagno  3 ปีที่แล้ว

      for technical support -> docs.microsoft.com/en-us/answers/products/dotnet