Dependency Injection with .NET MAUI Explained! Full Beginner's Guide

Dependency Injection with .NET MAUI Explained! Full Beginner's Guide

James Montemagno

2 года назад

62,980 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@JamesMontemagno
@JamesMontemagno - 16.10.2022 13:18

Note: That [ICommand] is now [RelayCommand] from the .NET community toolkit!

Ответить
@user-wb4ep3in6r
@user-wb4ep3in6r - 20.12.2023 15:47

Hello James .. I have a 2D barcode scanning application written in xamarin forms. Now i am trying to convert into MAUI. I am getting lots of issues in it. In Xamarin forms i used Prism controls, ZXing for barcode scanning. Could you please suggest me the best way to do for me

Ответить
@user-cs8ii8tw4y
@user-cs8ii8tw4y - 08.12.2023 17:09

You should not be using the IServiceProvider for anything other than registering your model. What you are talking about at the 10 minute mark is the "Locator Pattern" and it is an anti-pattern. I recommend a bit of reading around the subject of Dependency Injection, Inversion of Control, Dependency Inversion. You don't appear to have a real grasp on why we use it, what its benefits are, how it should be used; which is a shame when you've put together such a nice video.

what you should have done was register your MainPage (and all of your other pages) with the Service provider, and in your main method resolve the MainPage.

Ответить
@DeakonJim
@DeakonJim - 24.11.2023 05:52

Might not get any reply but might as well try it, is it a must to register the pages as well along with the view models?

Ответить
@PsykoTiK-SJ
@PsykoTiK-SJ - 25.10.2023 20:13

Nice One as usual JM. 🫡😎🔥

QN:
Are u saying if i register my pages with DI, i don't have to add them as register route in the App.xaml.cs? Or i will still need that if i have them as Routes in the App.xaml?

Ответить
@jeff-uq2sz
@jeff-uq2sz - 10.08.2023 16:41

Wow this is amazing. Thank you for getting this out there! And thank you David!

Ответить
@enricoroselino7557
@enricoroselino7557 - 08.08.2023 10:24

yes but now my binding context = viewmodel need the IConnectivity because of viewmodel(IConnectivity connectivity)

Ответить
@mkgungor
@mkgungor - 12.07.2023 06:24

Hi
Could you make a video about how to make the .Net MAUI app auto start at boot… Thank you!

Ответить
@istuart0
@istuart0 - 29.06.2023 12:59

I wish I'd have seen this when I first started with .Net MAUI back in February. I have a few screens in my app and about a dozen VMs. I'd written my own workaround for trying to handle injecting dependencies but it was a bit messy and I felt there must be a better way. And there is.

Ответить
@fernandogoncalves1891
@fernandogoncalves1891 - 13.06.2023 12:45

Addtransient is not working. When I return to that page it doesn't create a new one

Ответить
@kopilkaiser8991
@kopilkaiser8991 - 10.06.2023 02:19

Love you brother.❤ I'm always growing my code skills and knowledge all thanks to you. You really explain in a simple way which is very easy to understand.

Ответить
@leosouza1520
@leosouza1520 - 06.05.2023 20:25

Great video! Do you think we still need a third-party dependency package, like DryIoc, in MAUI? We are having this discussion with the team, and your opinion would help.

Ответить
@user-jc2jw2xx1q
@user-jc2jw2xx1q - 29.04.2023 08:27

Great video! You wonderful programmer 🤘

Ответить
@user-wv1dw2ku9e
@user-wv1dw2ku9e - 18.04.2023 08:01

Hi James Montemagno, love your videos! 재미있게 잘 봤어요. 한국에서는 이런 강의를 못보는데 감사합니다.

Ответить
@michaelday6987
@michaelday6987 - 12.04.2023 05:32

This is a strange order for your playlist.

Ответить
@NotTheStinkyCheese
@NotTheStinkyCheese - 02.04.2023 20:33

so constructor injection is 'new' for dotnet Maui ?
I did like how you explained the concept from scratch as DI can be a bit magic if you don't understand what is happening.
btw I saw 'scoped' too, but that's just a transient with more staying power as it were which can be useful if you don't want something to be a singleton

Ответить
@davidroqueq.4271
@davidroqueq.4271 - 03.03.2023 21:23

I was doing it without Shell, it didn't occur to me that that's why I didn't get out, thank you

Ответить
@alexpinkerton7459
@alexpinkerton7459 - 26.02.2023 16:12

Thanks again James for the absolutely excellent video!

Ответить
@doogiebones
@doogiebones - 24.02.2023 23:08

This is so damn good, your content is incredible.

Ответить
@stkuo612
@stkuo612 - 19.02.2023 14:47

Hi James, I really enjoy your videos, can you make a video to talk about how to handle the login in .Net MAUI? Thanks

Ответить
@priyavenkat1936
@priyavenkat1936 - 16.02.2023 16:20

Hi James, Can you please give some advantage and disadvantage of using dependency injection in viewmodel

Ответить
@EvarDion
@EvarDion - 30.11.2022 07:49

This is a good explanation but having been a developer for 25 years and having used Dependency Injection in several web service projects,
I still remain thoroughly unconvinced as to why its such a brilliant idea to use everywhere as claimed by many of it's advocates.

For example: what is the point of registering and then passing the a static IConnectivity object to each view model when you could simply just access it directly in code via Connectivity.Current inside the constructor because it is already static?

And if you want more control over the Connectivity property then why not just add a static App.Connectivity property to your App Class?

Also.. if the use of static properties is such a huge "No No" then why does Microsoft use it everywhere in their own frameworks? App.Current, System.Threading.Thread.Current etc

To me, all dependency injection does is add extra bloat to your compiled code with the bonus of making it harder to debug and step through while also adding to the nightmare that is nuget hell.

I am all for making code simpler to read and write but not at the expensive of making it harder to support and maintain.

Ответить
@stephenhauck
@stephenhauck - 28.11.2022 15:06

I find I need an AppShell view model but when I try to register it and use constructor injection it does not work and complains about a parameterless constructor in the MainPage = new AppShell() .. is this because the "goodness" that makes constructor injection work happens after AppShell is created ?

Any way to get around this or just create it manually as I do now ?

Ответить
@TheAdamwest29
@TheAdamwest29 - 24.11.2022 03:33

Thanks James. I took the leap to net maui. Any pointers about managing state? Do I need to cook up a service back by storage?

Ответить
@davidpugh9947
@davidpugh9947 - 22.11.2022 19:06

I didn't know about ObservableProperty/ObservableObject before this video. Thank you SO much, it's going to save me a lot of time!!!

Ответить
@orangecake4427
@orangecake4427 - 09.11.2022 21:53

Thanks man! Why is this not part of the microsoft docs? 😅 awesome!

Ответить
@hamedhajiloo1235
@hamedhajiloo1235 - 23.10.2022 02:15

Hi. Please record a video about Intent-filter in Blazor MAUI

Ответить
@cdrbvgewvplxsghjuytunurqwfgxvc
@cdrbvgewvplxsghjuytunurqwfgxvc - 16.10.2022 09:43

[ICommand] -> [RelayCommand] hope it saves someone a little time

Ответить
@SathishKumar-yn7fq
@SathishKumar-yn7fq - 13.10.2022 20:38

Hi James, Can you suggest me any Bluetooth LE framework to communicate with hardware in .NET MAUI. Like read and write characteristics with examples. Need to support both iOS and Android.

Ответить
@cptechno
@cptechno - 10.10.2022 12:40

GENERAL QUESTION: I use Dependency Injection on the constructor of MainPage. The DI data is in MainPage and my application boots without errors. I want to get access to this data from the Index Razor page. How can I do that.? I researched and took several video courses, but I still did not find the answer. It's not obvious! I suggest you make a separate video addressing this topic. I'm sure there are several programmers like me asking the same type of questions.

Ответить
@dino633
@dino633 - 04.10.2022 09:14

Hi James, great work, thank you a lot! I have a question. In this example of DI, the constructors of the classes being injected are properly called, to prove that one can set a breakpoint. But not the destructors! I create a destructor in a vm, set a breakpoint, and see that it is never called. I know, this is not a bug but a feature. But I don't understand why DI is implemented this way. Could you comment on this a little bit more, why the dtors are not called?

Ответить
@zephaneas
@zephaneas - 26.09.2022 05:51

James, one other thing I'm having trouble with... I have plugged in both an Android table & and Android phone. Both have Dev Mode and USB Debugging turned on, but they don't show up under Android Devices. Am I missing something else?

Ответить
@zephaneas
@zephaneas - 24.09.2022 01:13

James, thanks for the great vid. One thing you should be aware of... You installed CommunityToolkit.Mvvm 8.0.0-preview3. Preview3 is no longer available. When I installed 8.0.0 the ICommand interface is no longer there. You may want to upgrade this project.

Ответить
@john-blair
@john-blair - 18.09.2022 21:20

Xamarin is meant to be platform independent so it is disappointing to see conditional compilation in the ServiceHelper to get the injection service for the different platforms. The nuget package Microsoft.Extensions.DependencyInjection handles constructor injection with no conditional compilation required for ios and android platforms. Thanks.

Ответить
@davidjackson148
@davidjackson148 - 13.09.2022 11:08

I notice in Xamarin you can uses plain old CSS files (which I like the idea of). Can you do the same with MAUI and if so can you do a video (I couldn't find one). Awesome stuff keep it coming

Ответить
@davidjackson148
@davidjackson148 - 12.09.2022 23:49

Hi James. great videos. I'm trying DI and I get this error "No parameterless constructor defined for type 'zzz.zzz.MainPage'." Some googling suggests its not supported yet. What am I missing?

Ответить
@ksyha8376
@ksyha8376 - 11.09.2022 19:11

This video is missing recommendation for scoped vs transient vs singleton for pages and viewmodels

Ответить
@TVsBen
@TVsBen - 07.09.2022 05:25

The real wonder of all this really manifests itself when I want to change an implementation. Create a new class that implements IConnectivity and now you've got to go to every page and change the initialization. But registering the service once lets you change which implementation to use at registration/builder time instead of having to go find every freakin reference. So that's really great.

Ответить
@sundinmikael
@sundinmikael - 27.08.2022 15:27

Is this better to have a static global method compared to use dependency injection and use IServiceProvider to get the service?

I added a example to easier understand.
class page{
public page(IServiceProvider serviceProvider){
var myService = serviceProvider.GetService<MyService>();
}

}

Ответить
@terry_hutt
@terry_hutt - 26.08.2022 18:42

I like the way you explain what you can do and also WHY you should do it. I have a hard time explaining to my team members why I want things done a particular way and this really helps me explain the why to them.

Ответить
@FrancoisMongeau
@FrancoisMongeau - 09.08.2022 05:28

58,000 times is more than 2 years - without sleep. We can do without the internet hyperbole. Seriously: keep up the good work, the best explanation of DI I got so far.

Ответить
@d9724
@d9724 - 22.07.2022 23:02

The explaining of James is increible. Thanks so much for the content! if you make a course I'll take it yes or yes!

Ответить
@mubarekabdi
@mubarekabdi - 16.07.2022 18:27

You did well James. Well explained!

Ответить
@knockout4907
@knockout4907 - 07.07.2022 17:30

My takeaway was this process removes the DI inception that I've experienced before. DI a db connection into my controller then pass it down to my helper class, etc. It was awful and messy. This example looks to eliminate that mess by injecting the root page resolving any DI down the pipelines as well. Hopefully I understood that correctly lol

Ответить
@kellelein
@kellelein - 09.06.2022 08:46

In the MainPage codebehind, why is

public MainView(MainViewModel mainViewModel)
{
InitializeComponent();

BindingContext = mainViewModel;

}

better than

public MainView()
{
InitializeComponent();

BindingContext = ServiceHelper.GetService<MainViewModel>();

}

Here you use the IoC container..

In the first example you use constructor injection, but you still have a hard dependecy to MainViewModel... To use it correct, should you not inject a IMainViewModel instead, and let your IoC bind your interface to your implementation... But I find it hard to understand, how to do it correct, because some clases don't nessary have a interface.. like MainPage typical don't

Ответить
@tawcreative
@tawcreative - 29.05.2022 17:48

I enjoy your videos so much. Thank you for taking the time to do them and break stuff down. Also, I've watched so many of your videos that I've watched you go from short hair to long hair!

Ответить
@UzairAli001
@UzairAli001 - 22.05.2022 09:38

What if I want to pass some parameters along with dependencies in vm

Ответить
@kostyantynherasymenko4
@kostyantynherasymenko4 - 17.05.2022 09:45

Thank you James! I always look forward to new videos about MAUI from you.

Ответить