Getting Started with Dependency Injection in Xamarin.Forms

Поділитися
Вставка

КОМЕНТАРІ • 47

  • @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

  • @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.

  • @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

  • @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

  • @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!

  • @akramtalmoudi6374
    @akramtalmoudi6374 2 роки тому

    how to implement interface from shared code in android ??

  • @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

  • @GellClan
    @GellClan 3 роки тому +1

    Thx for this, very helpful video.

  • @oscarestrada4509
    @oscarestrada4509 Рік тому

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

  • @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 :))

  • @john-blair
    @john-blair Рік тому

    Doesn't seem to do constructor injection?

    • @JamesMontemagno
      @JamesMontemagno  Рік тому

      .NET MAUI's does -> ua-cam.com/video/xx1mve2AQr4/v-deo.html

  • @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

  • @dotnetdevni
    @dotnetdevni 3 роки тому

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

  • @zarokoshin4090
    @zarokoshin4090 3 роки тому

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

  • @mestutosisptheophile8991
    @mestutosisptheophile8991 3 роки тому

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

    • @JamesMontemagno
      @JamesMontemagno  3 роки тому

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

  • @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

  • @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

  • @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/

  • @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!

  • @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

  • @zeetsoft
    @zeetsoft 3 роки тому

    Very helpful playlist. Thank you so much.

  • @oOserkanCakmakOo
    @oOserkanCakmakOo 3 роки тому

    Thanks James

  • @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/

  • @williamprogramer4168
    @williamprogramer4168 3 роки тому

    ⭐⭐⭐⭐⭐

  • @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...

  • @anttipellinen
    @anttipellinen 3 роки тому +1

    First!