Тёмный

Getting Started with Dependency Injection in Xamarin.Forms 

James Montemagno
Подписаться 139 тыс.
Просмотров 17 тыс.
50% 1

Наука

Опубликовано:

 

5 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 47   
@SolidSnake2025
@SolidSnake2025 3 года назад
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
@davejarcher
@davejarcher 3 года назад
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! 🙌
@marna_li
@marna_li 3 года назад
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 года назад
So, the great part with .NET MAUI is that it will be using Microsoft.Extensions.DependencyInjection, which should really streamline this.
@zarokoshin4090
@zarokoshin4090 3 года назад
Thanks James !! Just what i was looking for, the notification message ! :)
@kosteash
@kosteash 3 года назад
wow, tnx James :) I thought all the time DepependecyService works only with CustomControls in Android and iOS
@zeetsoft
@zeetsoft 3 года назад
Very helpful playlist. Thank you so much.
@JamesMontemagno
@JamesMontemagno 3 года назад
Glad you enjoyed it!
@GellClan
@GellClan 3 года назад
Thx for this, very helpful video.
@oOserkanCakmakOo
@oOserkanCakmakOo 3 года назад
Thanks James
@jeffybanjo
@jeffybanjo 3 года назад
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 года назад
Take a look at channel9.msdn.com/Shows/XamarinShow/SnackBar--Toast-Xamarin-Community-Toolkit
@cemalgulbeyaz
@cemalgulbeyaz 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.
@atreides07
@atreides07 3 года назад
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 года назад
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/
@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
@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
@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
@akramtalmoudi6374
@akramtalmoudi6374 2 года назад
how to implement interface from shared code in android ??
@dotnetdevni
@dotnetdevni 3 года назад
U should show how to get it From an abstract class i only no cause dotnetcore
@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
@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/
@greeksinmars
@greeksinmars 3 года назад
Why not use Autofac ? If I have parameters in constructors ?
@JamesMontemagno
@JamesMontemagno 3 года назад
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...
@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
@adinwashere
@adinwashere 3 года назад
This is similar but different to Dependency Injection in other C# project types (MVC, WebAPI, Razor Pages...)
@JamesMontemagno
@JamesMontemagno 3 года назад
.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 года назад
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 года назад
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 года назад
@@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 2 года назад
Doesn't seem to do constructor injection?
@JamesMontemagno
@JamesMontemagno 2 года назад
.NET MAUI's does -> ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-xx1mve2AQr4.html
@williamprogramer4168
@williamprogramer4168 3 года назад
⭐⭐⭐⭐⭐
@anttipellinen
@anttipellinen 3 года назад
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
Далее
Песня РАСПУТИН на русском!🔥
00:56
Android Dependency Injection - Introduction
6:56
Просмотров 32 тыс.
From Code-Behind Code to MVVM with XAML and C#
19:19
Просмотров 14 тыс.
I Remade YouTube From Scratch Using Just Bash
17:51
Просмотров 26 тыс.
CED: часть 1
23:37
Просмотров 107 тыс.
iPhone Standby mode dock, designed with @overwerk
0:27
Умный обзор умного iPhone 16 / 16 Pro
21:21