You are welcome. Just don't get used to it every week. I don't have the time to produce three videos a week (two is a stretch). I do try to throw a bonus in if a lesson is short or if I have to separate out a piece though. I'm glad you enjoyed it.
Just when I thought I was intermediate in coding, I watch Tim's Video and Boom, it reminds me I am still learning lmaoo. Thanks Tim!, I really appreciate this.
We are all still learning. DI is really an advanced topic so I don't think it is a threat to your intermediate understanding of C#. Keep up the good work.
This course has been absolutely incredible! I watch the majority of these videos before I had even decided to follow along. I have learned more in just the first 5 videos than any other training sessions I have completed thus far. I finally understand how to properly use a constructor. Keep up the great work. I am really enjoying learning from you.
Thank you Tim. This is way over my head but that's why I'm watching. I'm coding along with you so I can have an awesome example for other projects. I really appreciate the videos.
@@IAmTimCorey I find coding along better than copy and paste or downloading source, so a 30 min vid is a little longer due to pausing as I find it easier to type and then listen to the explanation :)
Something new i learnt. thankyou Tim. somewhat hard to understand, but in future sessions, i may get understand. i did simply what u did. thats all. Thank you.
At about 20:00 i tried to start my one but i keep getting an exception of "System.MissingMethodException: 'Cannot create an instance of an interface.'" from the return statement at? protected override object GetInstance(Type service, string key) { return _container.GetInstance(service, key); }
I'm wondering if this isn't actually your issue. It might be that your issue is where you are requesting an instance of an interface that you did not properly set up in the container. Look at your container configuration and make sure you mapped your classes and instances correctly.
Hi Tim, A query which I wanted to ask. How do you identify the appropriate Nuget packages and third party libraries to use ? For example, in this video, how do we identify that Caliburn Micro is the best library for our purpose. Does it come with experience, or do we need to refer various forums or do we need to try out various third party libraries before hitting upon the one which suits our needs
Practice. When you learn about a library, try it out. See if it works well, then see what needs it fills. Then, write that down. When you r next project comes around, and you find that you have a need, see if you have found a package that fulfills that need. Just be careful that you don't have solutions in search of a problem. Only after you know you have a need should you look for a solution. Otherwise, you will be taking on dependencies without enough benefit.
Think I'm starting to follow along, with the shellviewmodel I'm so use to passing a parameter "container" to it etc. was a little confused when this worked and I didn't pass in any container into the class constructor. Guess that's the "Caliburn Micro" doing it's thing. - Is Caliburn Micro just for WPF - if so, what is the alternative for WinForms - is that AutoFac, Ninject - not really looked into these. Again thanks for another great episode!
The container is the tool that actually creates the instances. We don't need to pass it around unless we want to manually dip into the container. As for Caliburn Micro, it is specific to WPF. Normally when I use a DI system, I use AutoFac (I have a couple videos on it on this channel). We will use AutoFac in the WebAPI layer.
@@IAmTimCorey Thanks for the response Tim, much appreciated. I think I've seen an Autofac video of yours a while ago but don't really do much "new" projects to use it and learn it. I'll have to find it and watch it again.
Hey Tim! It's possible I'm jumping the gun here, but at 9:30 you are talking about the Configure method and end up declaring things like the WindowManager and EventAggregator... What would be the case in which you would use something different? Or are you by in large just going to be using the Caliburn Micro instances? I assume if for some reason we really wanted to, we could use our own or another library that has a WindowManager/EventAggregator that implements IWindowManager/IEventAggregator?
5:40 agree if you are curious or just want to know what particular piece of code does F12 it and find the definition, if nuget is hosted on github go to the source code and try figure it out yourself. Human is the creature of habit and it will gravitate to their preferred nuget packages but before you dive in make sure you undesrstood concepts well. #1.5 speed club
I'm more recommending that you understand the code in your application, not necessarily that you understand how the NuGet package code works, but if your curious, it is nice that you can explore that too.
Dear Tim, You are really a best teacher ever seen. thank you so much for these valuable content . I have a question always comes in my mind. May be a stupid question . how do you know this . these question always comes to my mind when I saw any code . How you know this, I need to add this code here . I need to use this instance here. etc.. I learned most basic things in c# . including OPPs. but when I see these new libraries . really wonder how you made that. yes you might say documentation.. but most time for me its almost no connection with what I saw. and I really loose my focus. It would be really helpful if you could guide on this.
I learn through reading the documentation, reading articles about the topic, and watching TH-cam videos. Then, I practice everything I think I just learned so I know for sure that I understand it.
@@IAmTimCorey thank you Tim, I m from different backgrounds (electrical eng) and want to have a transition in my career as a developer.. Soon I ll get to that point .. With the help of Tim like personalities.. 👍👍
Hey Tim, at 29:50 when you run the solution, how exactly does the ShellViewModel with the custom constructor (with ICalculations) get accessed? Wouldnt the undefined default constructor be called to create an instance of the ShellViewModel instead? I'm do not understand the link from OnStartUp to constructing ShellViewModel(ICalculations).
Actually, once you define one constructor, there no is no longer a default constructor. If you wanted an empty constructor (like the default is), you would need to define that specifically as a separate, overloaded constructor. In this case, though, dependency injection is looking at the constructors and identifying which one(s) it can fully satisfy. It uses the one with the most items it can satisfy. It finds our only constructor, identifies that it needs an ICalculations object, knows it understands how to instantiate one of those, does so, and passes in that object to the correct constructor.
Great series. I am lucky to be your follower. I have a question. In this series did you managed to talk about "logging" ? which is one of the key in any enterprise application development? I hope you have covered in upcoming videos in this series.
Hello Tim, Thanks for all your great videos, I am enjoying a lot it while learning C#. while typing after you in this video the compiler yale about: Where(type => type.IsClass) giving me an error : Error CS1061 'Type' does not contain a definition for 'Where' and no accessible extension method 'Where' accepting a first argument of type 'Type' could be found (are you missing a using directive or an assembly reference?) I have the using statement : using System; and even added using System.Reflection; but it still through the error, I reviewed it many times. I can't compile appreciating your help.
I did the same mistake too, there should be Types with "S" instead of Type just before Where(type => type.IsClass), or take this full code: GetType().Assembly.GetTypes() .Where(type => type.IsClass) .Where(type => type.Name.EndsWith("ViewModel")) .ToList() .ForEach(viewModelType => _container.RegisterPerRequest( viewModelType, viewModelType.ToString(), viewModelType));
@@leeroy1986 i already did that for sure still nothing and i added using system.linq but i figured out my mistake with Artavazd comment , i missed the s :). thanks a lot for replying.
@@Artavazd.kirakosyan Yes this was the mistake i wrote GetType instead of GetType while i take a screenshot from the video to compare it with what i wrote and i missed it too :) Thanks a lot man.
Hi Tim, I have to thank you for your tutorial sets, they are great. I'm struggling with this video though, to be honest as we progress from video to video, I understand and can follow less and less, ending in simply typing what you type as I have no clue what is happening. Do you have other simpler tutorials for a beginner like myself?
Check out the Tournament Tracker application: th-cam.com/play/PLLWMQd6PeGY3t63w-8MMIjIyYS7MsFcCi.html or my Foundation in C# course series: www.iamtimcorey.com/blog/49797/how-to-learn-csharp-well
Thanks Tim for a very good tutorial. Could you please make some video about how to use passwordbox properly or how to create a very secure login for desktop applications. Thank you.
Tim thank you for sharing your knowledge. I have some issues that i cant resolve. It started with WPF Setup. When i installed Caliburn.Micro, and deleted Main page and changed ShellView as a main page, when i start the app i get message: Cannot find view for TRMDesktopUI.ViewModels.ShellViewModel. after continuing to write code in this video till 20:00 minute,i have tried to start app and i got Message: System.NullReferenceException: 'Object reference not set to an instance of an object.' under DisplayRootViewFor(); Hope that you can help me. Thanks for lessons, you are amazing.
great video. Question 1: what if we want to call an instance of ShellView from another View? We will have to create an instance of ShellViewModel, right ? For example: var viewModel = new ShellViewModel(/*here is my ploblem /*); (new WindowsManager()).ShowDialog(viewModel); Question 2: what if we want to pass also another data (e.g: int fooValue) to ShellViewModel using constructor ? should we change the constructor definition to public ShellViewModel(ICalculations calculations, int foo) ?
Hi Tim, excellent tutorial as usual. However i am having an issue with caliburn micro simplecontainer DI. Its singleton implementation does not allow me to change from HttpClient to HttpClientFactory implementaion. I am assuming there is a way to implement it in the simplecontainer DI, but with all my research i cannot how to. Secondly, in my frustrations, i decided to change from simplecontainer and use netcore's DI system using the IServiceCollection . But once again another roadblock, i dont know how configure the DI for it to work in Bootstrapper. Please advise how i can implement any of the above solutions so that i can use HttpClientFactory in application. Thanks in advance.
Hi Tim, When using WPF Core, do you know of any method to integrate the IHTTPClientFactory into this simple container or otherwise use the .NET core Dependency injection alongside Caliburn micro? Keep it up with all your priceless video's ! Grtz
Stick with this series. We eventually upgrade all of the projects to .NET Core. Then you will see how to integrate simple container with IHTTPClientFactory.
@@IAmTimCorey I really wished you had started this with .NET CORE cause I am actually working a .NET CORE PROJECT. But I am following you and trying to get as much as possible from this. I am currently working with autofac for dependency injection and I don't quite see the difference with Caliburn Micro, can they work together and stuff?
Here is a video I did on Dependency Injection and how it works (using a slightly different system but the principle is exactly the same): th-cam.com/video/mCUNrRtVVWY/w-d-xo.html and here is a video I did on the Dependency Inversion Principle (not Dependency Injection), which is the principle that Dependency Injection is based upon: th-cam.com/video/NnZZMkwI6KI/w-d-xo.html
this is so funny because you sound just like Louis CK. So I imagine Louie is teaching me how to program :) You have the same sound, pauses, mannerism, etc. so cool.
I would like to utilize built in Microsoft dependency injection in a class library as part of my asp.net mvc application. I know how to inject into controllers and views but I don't know how to instantiate an instance in class library. I would appreciate it if you point me to right direction. Great videos Tim.
Just add those classes to your DI system in your UI project. We are actually doing that with this project. We have a class library and we integrate it into our WPF DI system. We do the same with our API project. I go into a LOT more detail in this course: www.iamtimcorey.com/p/dotnet-core-dependency-injection-in-depth (.NET Core Dependency Injection In Depth).
Hi Tim, This is a great tutorial for WPF & MVVM. I have got one question for you. How do I keep my Login view out of shell window? I want to load the shell window once the login is succeeded. What changes I need to do ? Please advice.
You would have to use the Window Manager to open a separate window if that is what you really wanted to do. It will be trickier, though, since C# wants to close the application when the Shell View closes. That can only happen if the Shell View opens when the application opens. Why don't you just put the login box inside the Shell View, like I do in this series?
Hi Tim! I'm building an application of my own for learning purposes after following through your TournamentTracker series and the WPF add-on course for that. I've come to a point where I want to start adding unit tests. To make that easier I learned about DI. Now I have a fairly similar case to what you showed in this video where I have a Calculator class, but mine is placed in the Class Library. Whenever I try to use DI for my Calculator class in the WPFUserInteface project or for the ViewModel classes in my WPFUserInterfaceTests project, I'm getting the error "No suitable constructor was found." I'm guessing this is because all the DI-related methods that are configured in Bootstrapper are protected? I noticed that in your Autofac video you are setting up DI so that classes from your class library are involved as well. Is SimpleContainer only usable in the WPF project and if so how should DI be setup so that it can be used across the solution in Unit Tests for example?
Hello Tim, I'm so grateful for your tutorials - they are awesome! There's one question I have. Supposing, I want to use another DI container (Castle.Windsor for example). So, the code I have to write to "configure" my WPF App is pretty much the same, isn't it? (It's all about syntax?)
When I tried to launch it threw an exception 'Object reference not set to an instance of an object'. Then below it says This exception was originally thrown at this call stack: [External Code]. And I get a Break Mode page saying this application is in break mode. Any ideas?
The first video in this series laid out the basic roadmap. The plan is to do all .NET Framework at first and then transition the app over to .NET Core. Doing a .NET Core from scratch video is simple (and I'll be doing one that way soon) but it doesn't really simulate where the marketplace is today. New apps may be built in .NET Core but there are a LOT of existing apps that need to be moved over. We are going to tackle this second issue with this course in order to better simulate the real world. That way this channel will have both fresh .NET Core videos and transitioning to .NET Core videos.
Hey Tim, nice tutorials, I am following along and leaving "likes" to them. I just have a question: How would you do if we had two implementations from the same interface?I mean, how the ShellViewModel class would know which to use it? Thanks and keep up with your videos.
I would recommend against it unless you really need to. It will be tricky, but there will be some changes that are more difficult than just a simple replacement.
I was able to get WPF working as detailed in video. Now I am trying to implement same pattern in Web MVC but having trouble using the access token, the api helper is null when I try to log in. Any advice?
At the end you created a simple Calculations class and an interface for it, and then you registered that in your Configure method in the Bootstrapper. What if you have an ICalculations interface and then a bunch of _different_ classes which implement that interface? Do you also generate a new interface for each of the classes which implement ICalculations, and then you register these via their dedicated interfaces in the bootstrapper? And if yes, then by the end of your project you will have a whole bunch of interfaces whose purpose is for nothing more than registering them in the Bootstrapper. This is the intended way to do this? And I guess the same applies to abstract classes? You'd have a new interface for each class which inherits from the abstract base class and you'd register these? Or am I completely misunderstanding what you did?
It depends on your scenario. Do you anticipate running all of those classes when you want to interact with ICalculation? If so, you can get a list of them back. If you want to have one back, which one? If only one, you put that one in DI. If different ones at different times, you need to have some way of telling the system that. Individual interfaces is an easy option. Another is to have metadata (a bit more advanced). Interfaces are free so use them the way that makes sense for your project. You may have interfaces that are specific to each class. That's ok. Remember that the purpose is to allow us to replace the implementation later, either when we upgrade a class or when we are creating unit tests. Take the ILogger for example. We have more than one implementation, usually. I tend to use Serilog, but Microsoft also has a built-in logger. So, my DI only specifies one logger, even though multiple are available. In my unit tests, I don't want to write log entries when I run tests so I replace the Serilog implementation with an in-memory one.
Hi Tim, thank you so much for the brilliant tutorials, but unfortunately, I am getting some errors after deleting Calculations.cs and ICalculations.cs since these two are being used by Bootstrapper.cs. Thanks once again!
Yep, it takes a bit of cleanup. Just follow along with what I did. Once you have them in the class library and once you have referenced the class library in your UI project, you can add the correct using statement in the Bootstrapper file to fix the errors. I did that nearer to the end.
Hi Tim, how about instantiating Class that is asking for model as argument in its constructor?Is it also excepted for DI? You said in one of your video 'DI with Autofac' that Model is excepted in DI because it only contains data, meaning Model should be instantiated using 'new' statement,correct me if i misunderstood this? Another one is the built-in classes in .net framework can i use DI on all of them or there are some also excepted. Last one, I believe it is possible to use multiple different classes that implement one interface, am i right to register it as-self?
Adding info through the constructor in DI is a bit more advanced. I'll be doing a video on it at some point. As for models not typically being included in DI, that is correct. The use of built-in classes depends on the situation but in general, we add them to DI. Finally, if you have multiple classes that implement one interface, you can register one of them to the interface, apply logic to which one gets returned, return all of them and let the client decide, or you can self-register.
Hi Tim, Dont know if this was asked before, but how do you use ActivateItem on a ViewModel that has DI ? For example, when changing Screens (User Control) after login screen i do this: _mainViewModel.ActivateItem(IoC.Get()) or _mainViewModel.ActivateItem(new DashboardViewModel()) Both arent working, It asks for the parameter in the constructor of DashboardViewModel... But my DashboardViewModel has a DI, so it doesn't work.. . Any suggestions ? Thanks. hope you will replay..
Could you explain what exactly does service mean in programming? To me, I always linked it with windows service. But what does it mean for a software developer?
Tim, any idea when I have added all the injection code override function mine OnStartup crashed when I launch the app? It says the ShellViewModel is null. If I comment all the override methods of the dependency injection, it starts working again. Cant find the relation or the root cause. Cheers!
I received this error on the ShellViewModel constructor 'ICalculations' is less accessible than method 'ShellViewModel.ShellViewModel(ICalculations)' TRMDesktopUI C:\Users\Rafae\Desktop\C#\TimCoRetailManager\TRMDesktopUI\ViewModels\ShellViewModel.cs Please advise.
I know it's been a long time, but in case anyone else has this issue. I found that I was getting the error becasue the interface in ICalculations.cs wasn't public. Changed it to public interface ICalculations and worked
My guess is that you upgraded the Caliburn Micro NuGet package to the 4.x version, which has breaking changes. Specifically, there are a few methods that now only have an async version. I believe GetInstance is one of them. Downgrade to the 3.x version and you should be fine.
For some reason this particular video is not buffering for me? I'm able to watch the episode "WPF with MVVM" before it and "Planning the register" which comes after. But this one will not buffer no matter what i try :O Anyone else with this issue? :)
To be honest, I did not understand everything that was shown in the video. Just following along doing the same things as shown, hoping I will understand them in future. I know what is Dependency Injection in basic. But how and why everything that was done here is too much to grasp in start. A little more on DI would have been nice or a link to a blog post/video to understand it better.
hello IAmTimCorey i ve followed this video line by line but iam getting this error mssage whenever i run the application. An exception of type 'System.NullReferenceException' occurred in Caliburn.Micro.Platform.dll but was not handled in user code what could be wrong?
@@IAmTimCorey if i comment out this line, it works well protected override object GetInstance(Type service, string key) { return _container.GetInstance(service, key); }
If anyone experience the same issue as commented. Make sure line 31 ###Where(type => type.Name.EndsWith("ViewModel"))### has an upper case V for "ViewModel". Having a lower case v "viewModel" will result in the "System.NullReferenceException" error
Thanks Tim. The code in Bootstrapper.cs/Configure reminds me a bit of the Factory you created when explaining the dependency inversion principle (th-cam.com/video/NnZZMkwI6KI/w-d-xo.html). With respect to the override for the Configure method, does Caliburn Micro create Singletons for the WindowManager and EventAggregator if you do not override the method?
Thanks for sharing Tim, but you lost me at WPF. I guess I just cant sit through another language that I will throw away after the tutorial. I really need MVC + Web API + SQL Server. I will be back again when you pick up ASP.net MVC again. Thanks again!
I understand that you don't intend to learn WPF, but just to be clear, WPF is just C#. There isn't a different language (except XAML, which is only used to display items on a form, not do any of the actual work). Whether you want to use WPF or not, MVVM is actually just a good use of OOP in C#. Most of what we will do will be in C# classes. The key to remember about C# is that user interfaces are not the language. They are just the visual part of an application. The language is what powers everything, from Console apps through web apps.
I have to say, I absolutely do not agree with the statement you make about Singletons. Well at least not in this context, where you are using DI and thus IoC. Here you can perfectly mock singletons when writing Unit Tests. Also; the real problem is storing data in those Singletons, not the fact that you register a class as a singleton. And even that is sometimes okay. Another problem some seem to have with Singletons is to do with muti-threading, but again that is not a problem of singletons, but a problem with the implementation of you class. After all even a transient instance could be called from multiple threads at the same time. In fact a class can never tell if it will be called concurrently.
You just went through a list of reasons not to use a singleton (or at least to be very cautious about it). That was what I was trying to communicate. Err on the side of instantiation.
Two a week! AWESOME! Thank you Tim!
You are welcome. Just don't get used to it every week. I don't have the time to produce three videos a week (two is a stretch). I do try to throw a bonus in if a lesson is short or if I have to separate out a piece though. I'm glad you enjoyed it.
Just when I thought I was intermediate in coding, I watch Tim's Video and Boom, it reminds me I am still learning lmaoo. Thanks Tim!, I really appreciate this.
We are all still learning. DI is really an advanced topic so I don't think it is a threat to your intermediate understanding of C#. Keep up the good work.
@@IAmTimCorey Thanks Tim!
Same thing here. And most often than I care to admit, I realize I was doing it all wrong.
This course has been absolutely incredible! I watch the majority of these videos before I had even decided to follow along. I have learned more in just the first 5 videos than any other training sessions I have completed thus far. I finally understand how to properly use a constructor. Keep up the great work. I am really enjoying learning from you.
Awesome!
Really nice video. The world needs more people like you!! Thank you for making that series, I'm learning a lot.
Thank you!
Thank you Tim. This is way over my head but that's why I'm watching. I'm coding along with you so I can have an awesome example for other projects. I really appreciate the videos.
Sometimes just coding along helps things click. It definitely pushes your knowledge forward faster than just watching. Keep up the good work.
@@IAmTimCorey I find coding along better than copy and paste or downloading source, so a 30 min vid is a little longer due to pausing as I find it easier to type and then listen to the explanation :)
Definitely the first time I have found a valuable treasure clarifying DI!
Thanks again!
Glad it was helpful!
Thank you Tim for this nice video, I would appreciate it if at the end of the video tell us what's coming next so we can be prepared.
I'm working on getting this in place. Thanks for the suggestion.
Thanks Tim! Soo much to learn still, it's hard to actually really start going.
Love this series.
I am glad you are enjoying it.
Something new i learnt. thankyou Tim. somewhat hard to understand, but in future sessions, i may get understand. i did simply what u did. thats all.
Thank you.
That's a good start. Do it a couple times and use it a bit and you will get the hang of it.
Another Great Video Thx Tim. This are my first steps (babysteps at first ;) ) in DI
You are most welcome. Thanks for watching.
At about 20:00 i tried to start my one but i keep getting an exception of "System.MissingMethodException: 'Cannot create an instance of an interface.'" from the return statement at?
protected override object GetInstance(Type service, string key)
{
return _container.GetInstance(service, key);
}
If i comment out that method the window displays
I'm wondering if this isn't actually your issue. It might be that your issue is where you are requesting an instance of an interface that you did not properly set up in the container. Look at your container configuration and make sure you mapped your classes and instances correctly.
@@IAmTimCorey Thanks a typo - I had IWindowManager instead of WindowManager in .Singleton()
works now thank you
Had this issue too. Ended up making the same mistake.
Dang, I had the same issue, these comments save me from pulling my hair out - that is if I had any... hahaha
Excellent Job
I really appreciate all your tutorials, Thank you very much
You are welcome.
Thank you very much Tim, you are a great tutor.
You are welcome.
Hi Tim,
A query which I wanted to ask. How do you identify the appropriate Nuget packages and third party libraries to use ? For example, in this video, how do we identify that Caliburn Micro is the best library for our purpose. Does it come with experience, or do we need to refer various forums or do we need to try out various third party libraries before hitting upon the one which suits our needs
Practice. When you learn about a library, try it out. See if it works well, then see what needs it fills. Then, write that down. When you r next project comes around, and you find that you have a need, see if you have found a package that fulfills that need. Just be careful that you don't have solutions in search of a problem. Only after you know you have a need should you look for a solution. Otherwise, you will be taking on dependencies without enough benefit.
Think I'm starting to follow along, with the shellviewmodel I'm so use to passing a parameter "container" to it etc. was a little confused when this worked and I didn't pass in any container into the class constructor. Guess that's the "Caliburn Micro" doing it's thing. - Is Caliburn Micro just for WPF - if so, what is the alternative for WinForms - is that AutoFac, Ninject - not really looked into these. Again thanks for another great episode!
The container is the tool that actually creates the instances. We don't need to pass it around unless we want to manually dip into the container. As for Caliburn Micro, it is specific to WPF. Normally when I use a DI system, I use AutoFac (I have a couple videos on it on this channel). We will use AutoFac in the WebAPI layer.
@@IAmTimCorey Thanks for the response Tim, much appreciated. I think I've seen an Autofac video of yours a while ago but don't really do much "new" projects to use it and learn it. I'll have to find it and watch it again.
Very nice tutorials
Thank you very much
You are welcome.
Hey Tim! It's possible I'm jumping the gun here, but at 9:30 you are talking about the Configure method and end up declaring things like the WindowManager and EventAggregator... What would be the case in which you would use something different? Or are you by in large just going to be using the Caliburn Micro instances? I assume if for some reason we really wanted to, we could use our own or another library that has a WindowManager/EventAggregator that implements IWindowManager/IEventAggregator?
5:40 agree if you are curious or just want to know what particular piece of code does F12 it and find the definition, if nuget is hosted on github go to the source code and try figure it out yourself. Human is the creature of habit and it will gravitate to their preferred nuget packages but before you dive in make sure you undesrstood concepts well.
#1.5 speed club
I'm more recommending that you understand the code in your application, not necessarily that you understand how the NuGet package code works, but if your curious, it is nice that you can explore that too.
Dear Tim,
You are really a best teacher ever seen. thank you so much for these valuable content .
I have a question always comes in my mind. May be a stupid question . how do you know this . these question always comes to my mind when I saw any code .
How you know this, I need to add this code here . I need to use this instance here. etc.. I learned most basic things in c# . including OPPs. but when I see these new libraries . really wonder how you made that. yes you might say documentation.. but most time for me its almost no connection with what I saw. and I really loose my focus. It would be really helpful if you could guide on this.
I learn through reading the documentation, reading articles about the topic, and watching TH-cam videos. Then, I practice everything I think I just learned so I know for sure that I understand it.
@@IAmTimCorey thank you Tim, I m from different backgrounds (electrical eng) and want to have a transition in my career as a developer.. Soon I ll get to that point .. With the help of Tim like personalities.. 👍👍
Hey Tim, at 29:50 when you run the solution, how exactly does the ShellViewModel with the custom constructor (with ICalculations) get accessed? Wouldnt the undefined default constructor be called to create an instance of the ShellViewModel instead? I'm do not understand the link from OnStartUp to constructing ShellViewModel(ICalculations).
Actually, once you define one constructor, there no is no longer a default constructor. If you wanted an empty constructor (like the default is), you would need to define that specifically as a separate, overloaded constructor. In this case, though, dependency injection is looking at the constructors and identifying which one(s) it can fully satisfy. It uses the one with the most items it can satisfy. It finds our only constructor, identifies that it needs an ICalculations object, knows it understands how to instantiate one of those, does so, and passes in that object to the correct constructor.
Great series. I am lucky to be your follower. I have a question. In this series did you managed to talk about "logging" ? which is one of the key in any enterprise application development? I hope you have covered in upcoming videos in this series.
I don't remember if I added logging or not, but here is a video on how to implement logging in .NET: th-cam.com/video/_iryZxv8Rxw/w-d-xo.html
Thank you again, Tim! You help me a lot!
You are welcome.
A link to the playlist would help a lot, sometimes one skip a video and it would be very handy if we can have that in the description. Thanks!!
Here's the link to the playlist. I'll see what I can do about adding it to videos: th-cam.com/play/PLLWMQd6PeGY0bEMxObA6dtYXuJOGfxSPx.html
Hello Tim,
Thanks for all your great videos, I am enjoying a lot it while learning C#.
while typing after you in this video the compiler yale about: Where(type => type.IsClass) giving me an error :
Error CS1061 'Type' does not contain a definition for 'Where' and no accessible extension method 'Where' accepting a first argument of type 'Type' could be found (are you missing a using directive or an assembly reference?) I have the using statement : using System; and even added using System.Reflection; but it still through the error, I reviewed it many times. I can't compile appreciating your help.
I did the same mistake too, there should be Types with "S" instead of Type just before Where(type => type.IsClass), or take this full code: GetType().Assembly.GetTypes()
.Where(type => type.IsClass)
.Where(type => type.Name.EndsWith("ViewModel"))
.ToList()
.ForEach(viewModelType => _container.RegisterPerRequest(
viewModelType, viewModelType.ToString(), viewModelType));
Lee is correct and yes, watch out for what Artavazd pointed out as well.
@@leeroy1986 i already did that for sure still nothing and i added using system.linq but i figured out my mistake with Artavazd comment , i missed the s :). thanks a lot for replying.
@@Artavazd.kirakosyan Yes this was the mistake i wrote GetType instead of GetType while i take a screenshot from the video to compare it with what i wrote and i missed it too :) Thanks a lot man.
@@IAmTimCorey Got it thanks a lot Tim, keep the great work, I am a fan.
Hi Tim, I have to thank you for your tutorial sets, they are great. I'm struggling with this video though, to be honest as we progress from video to video, I understand and can follow less and less, ending in simply typing what you type as I have no clue what is happening. Do you have other simpler tutorials for a beginner like myself?
Check out the Tournament Tracker application: th-cam.com/play/PLLWMQd6PeGY3t63w-8MMIjIyYS7MsFcCi.html or my Foundation in C# course series: www.iamtimcorey.com/blog/49797/how-to-learn-csharp-well
Thanks Tim for a very good tutorial. Could you please make some video about how to use passwordbox properly or how to create a very secure login for desktop applications. Thank you.
Well, we will be logging into the API from this WPF application, so you should see all of that.
Tim thank you for sharing your knowledge. I have some issues that i cant resolve. It started with WPF Setup. When i installed Caliburn.Micro, and deleted Main page and changed ShellView as a main page, when i start the app i get message: Cannot find view for TRMDesktopUI.ViewModels.ShellViewModel.
after continuing to write code in this video till 20:00 minute,i have tried to start app and i got Message: System.NullReferenceException: 'Object reference not set to an instance of an object.' under DisplayRootViewFor();
Hope that you can help me.
Thanks for lessons, you are amazing.
It sounds like you missed a step somewhere. You might not have added the ShellView or you might be missing a using statement.
@@IAmTimCorey i started project again, now works fine. Thanks again :)
Brilliant stuff once again!!!
Thank you!
great video.
Question 1: what if we want to call an instance of ShellView from another View? We will have to create an instance of ShellViewModel, right ? For example:
var viewModel = new ShellViewModel(/*here is my ploblem /*);
(new WindowsManager()).ShowDialog(viewModel);
Question 2: what if we want to pass also another data (e.g: int fooValue) to ShellViewModel using constructor ? should we change the constructor definition to public ShellViewModel(ICalculations calculations, int foo) ?
are we going to go over branching and merging?
Probably not in this course, since it is based upon the premise that it is just me doing the work.
Hi Tim, excellent tutorial as usual. However i am having an issue with caliburn micro simplecontainer DI. Its singleton implementation does not allow me to change from HttpClient to HttpClientFactory implementaion. I am assuming there is a way to implement it in the simplecontainer DI, but with all my research i cannot how to. Secondly, in my frustrations, i decided to change from simplecontainer and use netcore's DI system using the IServiceCollection . But once again another roadblock, i dont know how configure the DI for it to work in Bootstrapper. Please advise how i can implement any of the above solutions so that i can use HttpClientFactory in application. Thanks in advance.
Hi Tim,
When using WPF Core, do you know of any method to integrate the IHTTPClientFactory into this simple container or otherwise use the .NET core Dependency injection alongside Caliburn micro?
Keep it up with all your priceless video's !
Grtz
Stick with this series. We eventually upgrade all of the projects to .NET Core. Then you will see how to integrate simple container with IHTTPClientFactory.
@@IAmTimCorey I really wished you had started this with .NET CORE cause I am actually working a .NET CORE PROJECT. But I am following you and trying to get as much as possible from this. I am currently working with autofac for dependency injection and I don't quite see the difference with Caliburn Micro, can they work together and stuff?
Hey Guys, Is there anywhere I can learn about this Dependency Injection System, why we are doing this, how will it help and all?
Here is a video I did on Dependency Injection and how it works (using a slightly different system but the principle is exactly the same): th-cam.com/video/mCUNrRtVVWY/w-d-xo.html and here is a video I did on the Dependency Inversion Principle (not Dependency Injection), which is the principle that Dependency Injection is based upon: th-cam.com/video/NnZZMkwI6KI/w-d-xo.html
this is so funny because you sound just like Louis CK. So I imagine Louie is teaching me how to program :) You have the same sound, pauses, mannerism, etc. so cool.
Interesting.
@@JohannaMueller57 We can all perceive things differently, the beauty of human nature
I would like to utilize built in Microsoft dependency injection in a class library as part of my asp.net mvc application. I know how to inject into controllers and views but I don't know how to instantiate an instance in class library. I would appreciate it if you point me to right direction. Great videos Tim.
Just add those classes to your DI system in your UI project. We are actually doing that with this project. We have a class library and we integrate it into our WPF DI system. We do the same with our API project. I go into a LOT more detail in this course: www.iamtimcorey.com/p/dotnet-core-dependency-injection-in-depth (.NET Core Dependency Injection In Depth).
Hi Tim, This is a great tutorial for WPF & MVVM. I have got one question for you. How do I keep my Login view out of shell window? I want to load the shell window once the login is succeeded. What changes I need to do ? Please advice.
You would have to use the Window Manager to open a separate window if that is what you really wanted to do. It will be trickier, though, since C# wants to close the application when the Shell View closes. That can only happen if the Shell View opens when the application opens. Why don't you just put the login box inside the Shell View, like I do in this series?
@@IAmTimCorey Thanks for your swift reply. Will try that as explained in your series.
Hi Tim! I'm building an application of my own for learning purposes after following through your TournamentTracker series and the WPF add-on course for that. I've come to a point where I want to start adding unit tests. To make that easier I learned about DI. Now I have a fairly similar case to what you showed in this video where I have a Calculator class, but mine is placed in the Class Library. Whenever I try to use DI for my Calculator class in the WPFUserInteface project or for the ViewModel classes in my WPFUserInterfaceTests project, I'm getting the error "No suitable constructor was found." I'm guessing this is because all the DI-related methods that are configured in Bootstrapper are protected? I noticed that in your Autofac video you are setting up DI so that classes from your class library are involved as well. Is SimpleContainer only usable in the WPF project and if so how should DI be setup so that it can be used across the solution in Unit Tests for example?
I set up SimpleContainer to pull from the class library here as well. I think it is just a configuration issue that you are experiencing.
Hello Tim, I'm so grateful for your tutorials - they are awesome! There's one question I have.
Supposing, I want to use another DI container (Castle.Windsor for example). So, the code I have to write to "configure" my WPF App is pretty much the same, isn't it? (It's all about syntax?)
Correct. You will probably just need to change the setup code and that's it.
@@IAmTimCorey, thanks s lot.
When I tried to launch it threw an exception 'Object reference not set to an instance of an object'. Then below it says This exception was originally thrown at this call stack: [External Code]. And I get a Break Mode page saying this application is in break mode. Any ideas?
Were you able to get a solution? I got the same error. This is the line throwing the error. DisplayRootViewFor();
Any help is appreciated.
I got similair error and I had typed "viewModel" instead of "ViewModel" in EndsWith("ViewModel")
Thanks @@Frankieboy2008 . I also had typed "WindowManager" instead of "IWindowManager" in the Configure() method.
Thanks 👍
Gonna have to watch this one twice. I don't really understand what all is going on.
I offer other videos on Dependency Injection, if that might help - th-cam.com/users/IAmTimCoreysearch?query=injection
Nice vids and all but, can you publish some about .Net Core ??
The first video in this series laid out the basic roadmap. The plan is to do all .NET Framework at first and then transition the app over to .NET Core. Doing a .NET Core from scratch video is simple (and I'll be doing one that way soon) but it doesn't really simulate where the marketplace is today. New apps may be built in .NET Core but there are a LOT of existing apps that need to be moved over. We are going to tackle this second issue with this course in order to better simulate the real world. That way this channel will have both fresh .NET Core videos and transitioning to .NET Core videos.
Hey Tim, nice tutorials, I am following along and leaving "likes" to them. I just have a question:
How would you do if we had two implementations from the same interface?I mean, how the ShellViewModel class would know which to use it?
Thanks and keep up with your videos.
We would need to either differentiate them or we could load multiple views (a list). Ideally, though, you have one for each type.
Hi Tim! How hard is it to replace CaliburnMicro with MVVMCross in an already running application, let's say TRM for instance? Thank you!
I would recommend against it unless you really need to. It will be tricky, but there will be some changes that are more difficult than just a simple replacement.
I was able to get WPF working as detailed in video. Now I am trying to implement same pattern in Web MVC but having trouble using the access token, the api helper is null when I try to log in. Any advice?
At the end you created a simple Calculations class and an interface for it, and then you registered that in your Configure method in the Bootstrapper.
What if you have an ICalculations interface and then a bunch of _different_ classes which implement that interface? Do you also generate a new interface for each of the classes which implement ICalculations, and then you register these via their dedicated interfaces in the bootstrapper? And if yes, then by the end of your project you will have a whole bunch of interfaces whose purpose is for nothing more than registering them in the Bootstrapper. This is the intended way to do this?
And I guess the same applies to abstract classes? You'd have a new interface for each class which inherits from the abstract base class and you'd register these?
Or am I completely misunderstanding what you did?
It depends on your scenario. Do you anticipate running all of those classes when you want to interact with ICalculation? If so, you can get a list of them back. If you want to have one back, which one? If only one, you put that one in DI. If different ones at different times, you need to have some way of telling the system that. Individual interfaces is an easy option. Another is to have metadata (a bit more advanced).
Interfaces are free so use them the way that makes sense for your project. You may have interfaces that are specific to each class. That's ok. Remember that the purpose is to allow us to replace the implementation later, either when we upgrade a class or when we are creating unit tests.
Take the ILogger for example. We have more than one implementation, usually. I tend to use Serilog, but Microsoft also has a built-in logger. So, my DI only specifies one logger, even though multiple are available. In my unit tests, I don't want to write log entries when I run tests so I replace the Serilog implementation with an in-memory one.
Hi Tim!
I am passing a connection string in my implementation, thus, have to do something like:
_container.PerRequest();
How do we do that?
You would do that in the constructor rather than inside the angle brackets. Otherwise, you are right on track.
Thank you ^_^
Hi Tim, thank you so much for the brilliant tutorials, but unfortunately, I am getting some errors after deleting Calculations.cs and ICalculations.cs since these two are being used by Bootstrapper.cs. Thanks once again!
Yep, it takes a bit of cleanup. Just follow along with what I did. Once you have them in the class library and once you have referenced the class library in your UI project, you can add the correct using statement in the Bootstrapper file to fix the errors. I did that nearer to the end.
Hi Tim, how about instantiating Class that is asking for model as argument in its constructor?Is it also excepted for DI? You said in one of your video 'DI with Autofac' that Model is excepted in DI because it only contains data, meaning Model should be instantiated using 'new' statement,correct me if i misunderstood this? Another one is the built-in classes in .net framework can i use DI on all of them or there are some also excepted. Last one, I believe it is possible to use multiple different classes that implement one interface, am i right to register it as-self?
Adding info through the constructor in DI is a bit more advanced. I'll be doing a video on it at some point. As for models not typically being included in DI, that is correct. The use of built-in classes depends on the situation but in general, we add them to DI. Finally, if you have multiple classes that implement one interface, you can register one of them to the interface, apply logic to which one gets returned, return all of them and let the client decide, or you can self-register.
Thanks Tim.
Hi Tim,
Dont know if this was asked before, but how do you use ActivateItem on a ViewModel that has DI ? For example, when changing Screens (User Control) after login screen i do this:
_mainViewModel.ActivateItem(IoC.Get())
or
_mainViewModel.ActivateItem(new DashboardViewModel())
Both arent working, It asks for the parameter in the constructor of DashboardViewModel...
But my DashboardViewModel has a DI, so it doesn't work..
.
Any suggestions ?
Thanks. hope you will replay..
Could you explain what exactly does service mean in programming? To me, I always linked it with windows service. But what does it mean for a software developer?
It depends on the context but basically it is just a system that does work and sometimes returns values.
Tim, any idea when I have added all the injection code override function mine OnStartup crashed when I launch the app? It says the ShellViewModel is null. If I comment all the override methods of the dependency injection, it starts working again. Cant find the relation or the root cause. Cheers!
Not sure what is going on.
I received this error on the ShellViewModel constructor 'ICalculations' is less accessible than method 'ShellViewModel.ShellViewModel(ICalculations)' TRMDesktopUI C:\Users\Rafae\Desktop\C#\TimCoRetailManager\TRMDesktopUI\ViewModels\ShellViewModel.cs
Please advise.
You are missing the "public" modifier on something (probably ICalculations).
I know it's been a long time, but in case anyone else has this issue. I found that I was getting the error becasue the interface in ICalculations.cs wasn't public. Changed it to public interface ICalculations and worked
My ShellView doesnt Start anymore when i have the GetInstance method in my Bootstrapper. Do you have any idea why this could be?
My guess is that you upgraded the Caliburn Micro NuGet package to the 4.x version, which has breaking changes. Specifically, there are a few methods that now only have an async version. I believe GetInstance is one of them. Downgrade to the 3.x version and you should be fine.
@@IAmTimCorey Thank you very much for that fast Reply! Do you maybe know what the async variation is by hard?
@@IAmTimCorey This seems to be fixed now in 4.0.212
For some reason this particular video is not buffering for me? I'm able to watch the episode "WPF with MVVM" before it and "Planning the register" which comes after. But this one will not buffer no matter what i try :O
Anyone else with this issue? :)
To be honest, I did not understand everything that was shown in the video. Just following along doing the same things as shown, hoping I will understand them in future.
I know what is Dependency Injection in basic. But how and why everything that was done here is too much to grasp in start. A little more on DI would have been nice or a link to a blog post/video to understand it better.
This video might help: th-cam.com/video/mCUNrRtVVWY/w-d-xo.html
hello IAmTimCorey i ve followed this video line by line but iam getting this error mssage whenever i run the application.
An exception of type 'System.NullReferenceException' occurred in Caliburn.Micro.Platform.dll but was not handled in user code
what could be wrong?
It sounds like you probably have a naming issue.
@@IAmTimCorey if i comment out this line, it works well
protected override object GetInstance(Type service, string key)
{
return _container.GetInstance(service, key);
}
@@IAmTimCorey if
@IAmTimCorey if i comment out this line, it works well
protected override object GetInstance(Type service, string key)
{
return _container.GetInstance(service, key);
}
If anyone experience the same issue as commented. Make sure line 31 ###Where(type => type.Name.EndsWith("ViewModel"))### has an upper case V for "ViewModel". Having a lower case v "viewModel" will result in the "System.NullReferenceException" error
To the certain moment I was intact but then lost completely.
Go back and try it again. Keep at it. Pushing through will be really beneficial for you.
Sir, please entityframe core video
I will add it to the list. Thanks for the suggestion.
Thanks Tim. The code in Bootstrapper.cs/Configure reminds me a bit of the Factory you created when explaining the dependency inversion principle (th-cam.com/video/NnZZMkwI6KI/w-d-xo.html). With respect to the override for the Configure method, does Caliburn Micro create Singletons for the WindowManager and EventAggregator if you do not override the method?
Yes it does.
Thanks for sharing Tim, but you lost me at WPF. I guess I just cant sit through another language that I will throw away after the tutorial. I really need MVC + Web API + SQL Server. I will be back again when you pick up ASP.net MVC again. Thanks again!
I understand that you don't intend to learn WPF, but just to be clear, WPF is just C#. There isn't a different language (except XAML, which is only used to display items on a form, not do any of the actual work). Whether you want to use WPF or not, MVVM is actually just a good use of OOP in C#. Most of what we will do will be in C# classes.
The key to remember about C# is that user interfaces are not the language. They are just the visual part of an application. The language is what powers everything, from Console apps through web apps.
I have to say, I absolutely do not agree with the statement you make about Singletons. Well at least not in this context, where you are using DI and thus IoC. Here you can perfectly mock singletons when writing Unit Tests. Also; the real problem is storing data in those Singletons, not the fact that you register a class as a singleton. And even that is sometimes okay. Another problem some seem to have with Singletons is to do with muti-threading, but again that is not a problem of singletons, but a problem with the implementation of you class. After all even a transient instance could be called from multiple threads at the same time. In fact a class can never tell if it will be called concurrently.
You just went through a list of reasons not to use a singleton (or at least to be very cautious about it). That was what I was trying to communicate. Err on the side of instantiation.
Caliburn.Micro 4.0 (untested code fix):
protected override async void OnStartup(object sender, StartupEventArgs e)
{
await DisplayRootViewForAsync();
//DisplayRootViewFor();
}