Hi Sean. Great video, as always. When implementing dependency injection, I've seen you use both the ServiceProvider, as well as the Host and HostBuilder. What's the difference between the two approaches, and how do you know when to choose one over the other? Thanks!
@SingletonSean
3 жыл бұрын
Good question Jason K! I usually always roll with the HostBuilder nowadays. The benefit of Host/HostBuilder is that it includes a ServiceProvider AND it includes helpful things like logging and configuration settings (appsettings.json).
@MsQlan
3 жыл бұрын
Hi! I'm currently changing my carrier path from IT support to full time developer and I just wanted to start off by thanking you for your amazing videos. They have been such a great help in breaking down difficult concepts into structured and easy to follow guides. Do you have a suggestion for how to manage a Navbar toggle to indicate in the Navbar what viewmodel is selected. As the entire layout gets recreated each time a navigation occurs it would need to be set as the Layout is created. My first thought was to use a Store, maybe modify the NavigationStore perhaps?
@SingletonSean
3 жыл бұрын
Thanks for the feedback MsQlan, and best of luck on your transition! That's a good question regarding the navigation bar indicator. I might have to add another video to show how to do that in a clean manner since that's a common requirement and I kinda hacked it up in my SimpleTrader app. In most cases, a Store would be the perfect solution, and NavigationStore would be the perfect place. However, I think we can stick to a simpler solution for now by just directly telling the NavigationBarViewModel about the active view when we create the view. If we need to know the active view from a bunch of different places in the application, then we'll move the logic into the NavigationStore. Sadly I don't have a video now, but I DO have a GitHub changeset with comments 💯 : github.com/SingletonSean/wpf-tutorials/commit/9270850b62b8124e2bd5eb70335b793c08f22d8d?branch=9270850b62b8124e2bd5eb70335b793c08f22d8d&diff=unified
@MsQlan
3 жыл бұрын
@@SingletonSean Thank you so much for the prompt response and I totally agree for now a simple solution is better. I reviewed the changeset on Github, applied the changes to my own code and it works perfectly. My navigation buttons are generated from an observable collection of MenuItemViewModels so I ended up adding the IsActive property there.
@SingletonSean
3 жыл бұрын
That's fantastic, and thanks for supporting the channel MsQlan!
@Malygon
2 жыл бұрын
I've seen a bunch of approaches when it comes to Dependency Injection architecture. One would be having a DI provider as a singleton that registers all services on startup and then can be called by every class that needs a service. The advantages would be not having to pass around the service provider and its services like hot potatoes. So in this example, every NavigationService would just call the DI provider directly to get their NavigationStore instead of it being passed via the constructor. Seems like pretty good architecture to me but I reckon there are some downsides to it, what do you think?
@DrSteve123456
2 жыл бұрын
Hi Sean, In a previous video you suggested to use an async factory method if the task is to create a view model and load some data into it from db. I wonder how this works with DI. DI can give back an instance but it will not take care of an async loading.
@bity-bite
3 жыл бұрын
Hi. Can you create a video about the inversion of control, your videos are amazing. Thank you!
@SingletonSean
3 жыл бұрын
Perhaps! I would love to demonstrate some SOLID principles. Thanks for the suggestion!
@TheNimbleCoder
2 жыл бұрын
Hey Sean. Thanks for the time you have taken to put all of this together. First class for sure. I'm new to WPF and your explanation of the MVVM architectural pattern is clear and concise. This series covers everything I need to get rolling on a prototype for a production system I'm going to be building. Thank you!!
@SingletonSean
2 жыл бұрын
That's fantastic to hear Mark! Glad this is helpful, and best of luck on your prototype and production system. Thank you for supporting the channel too!
@wvvwwwvvw
2 жыл бұрын
Hi Sean. SO what if I have multiple services? Say, AuthenticationService, RfidScannerService, EmailNotifierService.. the AuthService is only needed in the LoginViewModel, but the other two would be required in one of the next Views.. should I still carry around all the services I want to use through all the NavigationCommands and have them injected via the constructor of each ViewModel? Or is there a cleaner way of doing it?
@SingletonSean
2 жыл бұрын
Hey Aleksandr! First off, thanks for leaving that wonderful message on Memspace 😂😅 Based on your scenario, I'm assuming the LoginViewModel needs those services in order to instantiate whatever view model (ex: NextViewModel) for the next view. In that case, you could use some kind of NextViewModelFactory class. This factory class would take in the services required to instantiate the NextViewModel. From there, your LoginViewModel would just need a NextViewModelFactory to create the NextViewModel. The NextViewModelFactory would be responsible for managing all those services for the NextViewModel. The nice thing about this is that if your NextViewModel needs more services in the future, you won't have to drill those through the LoginViewModel or other places of your app. Would just need to update the factory. I hope this explanation helps! Be sure to let me know if you have any questions. I don't think I have a specific video on this, but my Factory Design Pattern video might be worth taking a look at.
@wvvwwwvvw
2 жыл бұрын
@@SingletonSean Thanks. I've watched the Factory design pattern video and also found an example of a factory in your SampleTrader sln. However, I assume that this would rqeuire the factory to instantiate those services as well (which means tightly couple them) or to have them passed in through the constructor (alternatively, through the method params). But then in the case of the latter, where would the instantiation of those services happen? It's not like the DI container is going to make it work just if I register those services? Or will it...? [secret revealing music playing]
@davidcampbell5644
3 жыл бұрын
Did I make an error, but after completing this when I log out the full menu remains visible. Did the Account page and logout options not disappear after logging out before. Great videos and the new mic has made a big difference. Thanks Yes rolled back to the end of the modal video and the account and logout are not visible. So something in the DI in App.xml.cs has caused the IsLoggedin not to be passed. I tried a breakpont which wasn't hit when I logged out mmmm....
@SingletonSean
3 жыл бұрын
Hey David, thanks for the support! Regarding your issue, did you register the NavigationBarViewModel as Transient? I register it as Singleton in this video, but actually experienced the same issue. I mention this a bit in the description. Honestly, I'm not entirely sure why it won't work as Singleton. It must be an issue with multiple view models/views referencing the same NavigationBarViewModel, but I was never able to pinpoint the issue. Luckily, I recommend registering all view models as Transient anyways in order to support disposing.
@davidcampbell5644
3 жыл бұрын
@@SingletonSean Thanks for the speedy reply, yes changing the NavigationBarViewModel to transient did resolve the issue, that was one of the few things that I hadn't tried, thanks. I am really enjoying your videos and learning loads - please keep up the good work.
@paulomaciel2895
2 жыл бұрын
Man, what a job is this! I'm just started studying WPF / MVVM and this series will be the basis for my first project. Thanks a lot!
@SingletonSean
2 жыл бұрын
Hooray, glad this is helpful Paulo! Best of luck to your first project.
@Jeremygrmn
3 жыл бұрын
Hey Sean. Two questions: 1. Is there any case where you would want to register commands used in a ViewModel in dependency injection? 2. Could the way DI is handled in this video be at all considered falling into the service locator pattern since you pass in the serviceprovider into factory methods - eg. to create the HomeNavigationService?
@SingletonSean
3 жыл бұрын
Good questions Jeremy! 1. I talk about this in this video: kzitem.info/news/bejne/w4-Gu2yOcZhjq4o Overall, I think it "could" be useful if you need the view model to handle multiple, distinct behaviors 2. It seems quirky passing the service provider around, but I think it's not the service locater pattern because everything is contained with the App.xaml.xs. Additionally, those methods (ex: CreateHomeNavigationService) are only for registering services in DI
@xPa7r1cKx
3 жыл бұрын
Thats quite a mess there, not gonna lie^^
@SingletonSean
3 жыл бұрын
Hey xPa7r1cKx, I appreciate the honesty! I can't disagree either. The functionality is here and it works as desired, but we definitely need a cleaner approach. I imagine it'd be nice to have an abstraction where we could do "AddViewModel" or something. I've had this in the back of my mind and I'll let you know if I take a deeper look!
@xPa7r1cKx
3 жыл бұрын
@@SingletonSean Thats true, it works and that's the most important part. Maybe it sounded harsh, if it did I apologize. As someone who's coming from asp.net world where doing DI is a piece of cake it's a bit of a shock now when trying to learn WPF and all these complex stuff when it comes to navigation and DI :') I've read about Prism and other Mvvm frameworks, where these things are supposted to be easier, is that true, do you have any plans perhaps on showing how to do such things there, with the existing frameworks?
@SingletonSean
3 жыл бұрын
All good, criticism leads to innovation :) I agree, everything feels so much easier in ASP.NET since the ecosystem is bigger. As for frameworks, I believe Prism has a built in navigation infrastructure that you might find interesting. I haven't decided on creating tutorials with a framework, but I appreciate the feedback!
@xPa7r1cKx
3 жыл бұрын
@@SingletonSean Understandable. Btw, haha I found your questions on wpf Lvcharts github about memory leaks, did you perhaps manage to find a solution? I had to remove the charts entirely from my app because they were causing so many leaks even when doing the most basic stuff xD
@nieminenj
3 жыл бұрын
@@SingletonSean I use Microsoft's MVVM Toolkit. It's pretty easy to call needed dependencies from it's ioc container. I have generic NavigateTo where I just use needed vm and call it from ioc container.
@dannaughton3921
2 жыл бұрын
Hey @SingletonSean - can you please explain how CreateHomeNavigationService and CreateNavigationBarViewModel in App.xaml.cs do not have a circular dependency? It seems that to resolve a NavigationBarViewModel, you need create a HomeNavigationService, which in turn requires a NavigationBarViewModel etc. I'm running into an infinite loop problem in my app. Thanks!
@Malygon
2 жыл бұрын
A bit late but: While the "CreateNavigationBarViewModel" method is directly calling "CreateHomeNavigationService" the service is not calling the create method. It is instead registering a callback function which is called once the navigation happens. So that's why there is no infinite loop here since the callback is not firing immediately.
@nicoch8858
3 жыл бұрын
Hi!!... one question... and with your experience, You Implemented "Navigation" in projects with many views, viewModels and menu user? Doesn't it get complex to handle?
@SingletonSean
3 жыл бұрын
Good question Nico! It's definitely a lot to handle. I typically split up my dependency injection registrations in to extension methods to try and organize it a bit. Moving complex registrations to methods is helpful as well. Honestly, I'm still hunting for a pattern/best practice regarding this!
@nicoch8858
3 жыл бұрын
@@SingletonSean ok, thanks for answering
@zerosandones7547
3 жыл бұрын
what is a factory function?
@SingletonSean
3 жыл бұрын
Good question Zeroes and Ones! A factory function is just a function that instantiates an object. It's based on the factory pattern, which I talk about here: kzitem.info/news/bejne/tZ15p4JjpXl2g6Q
@Speede04
2 жыл бұрын
Hey. Following your tutorial I made everything working really nice and even developed more from this, but at some point I came across problem I can't handle by myself. I needed interface IDialogService to be passed through view's code-behind constructor (xaml.cs) and after that I getting Null Exception in App.xaml.cs in line with "MainWindow.Show()". Of course I've registered everything. I can pass parameters through MainWindow.xaml.cs, but not through any other View
@SingletonSean
2 жыл бұрын
Hey Świętopeł! I recall facing a similar problem on an old project that attempted using dependency injection on views. I never found a work-around for this issue. It seems that any view with a constructor w/ parameters cannot be defined in XAML. Overall, I think your best option would be moving the IDialogService into the view model layer.
@Speede04
2 жыл бұрын
@@SingletonSean That what I did before I decided to put it in the view. I spend last two weeks on StackOverflow looking for the solution for opening dialog in proper way without violating MVVM pattern. Finally I came up with my own solution and posted it on SO for verification and then... I have been told that opening dialog by ViewModel violates pattern... Damn, MVVM is not that straight forward :D In my opinion if the way that me or You or anyone else is writing his code in MVVM doesn't make it unit untestable or something else I do not know yet - it is good enough. Also I think there is as many "correct ways", "theories" of MVVM as many people saying them ;)
Пікірлер: 38