❔ Question about setting up dependency injection (console app with no async stuff)
So I've been trying to set up a dependency injection in my program for the first time to no avail. From what I've learned, a dependency injection allows you to use methods from another class without having to explicitly instantiate a new instance of the class which allows for loosely coupled code and less dependencies.
I was told I should be injecting my DBContext instead of calling a new context every time I do a database action. I think the way I have been approaching this fix is wrong in that I'm creating an interface for my Helpers class when in reality I should be making an interface for my PhoneBookContext class. Am I on the right track? Any feedback is appreciated.
Pasted code:
https://paste.mod.gg/hhwsmajvkxnq/2
BlazeBin - hhwsmajvkxnq
A tool for sharing your source code with the world!
41 Replies
I was told I should be injecting my DBContext instead of calling a new context every time I do a database actionthat is true for ASP.net because it's the framework that resolves stuff from the container. If you're in a console app, you're going to have to resolve at least the root item manually yourself.
I should be making an interface for my PhoneBookContext classNo. doing
helpers = new()
and then new
-ing up a PhoneBookContext
in helpers is the opposite of dependency injection.
a dependency injection allows you to use methods from another class without having to explicitly instantiate a new instance of the class which allows for loosely coupled code and less dependencies.That doesn't sound correct. loose coupling comes from class design and proper use of interfaces or other de-coupling mechanisms.
i think i see, it creates more dependencies?
It's the same dependency. It's not injected. It's tightly coupled
But its the only db context you;re going to have. It's ok to be relatively tightly coupled there
But it's better to inject as it is reduced coupling
so instead of getting loosely coupled code from dependency injections, its reducing coupling instead
?
I dont know what youre referring to
this and how my definition of DI is wrong
di is using constructors
constructor injection
thats the kind of injection i'm trying to do here, right?
It's what would be better. I don't see any attempt at it in your code
yeah im confused on how to set it up in the first place
looking at the microsoft documentation, i'm assuming i need an interface
if doing it manually, the only place 'new' should be is in Main
No, interfaces not needed
oh
could you explain what you mean by Main?
people have said that before and i dont know if they mean like program.cs or something
i remember there used to be a public static void main thing for csharp
yes
is that what youre talking about?
but doesn't every class have that
the entry point of the program
oh nvm on my last statement
No.There is exactly one main in an app (zero in pure libs)
And to make sure, this is in my program.cs right?
or unless i define it in a different class
what is in program.cs?
and don't use program.cs for my main
literally just this
oh for top level statements.
i have dependencies everywhere
uhh yeah?
Yes, that is your main
got it, so all my "new" stuff should be in main
and then i inject from there?
is this what you mean by the root?
yes. So here your root is Menu
if you were using a container, you'd have something small like
var menu = container.GetService<Menu>()
and it would make everything else that needs to be injected into Menu's ctor
but manually you might need to do
oh i see
well i dont mind listing them all out in my main
it would only be like 3 or 4
but youre saying I dont need an interface, so after would I follow the documentation as normal?
i'm following this and it says after i need to register my service
Dependency injection - .NET
Learn how to use dependency injection within your .NET apps. Discover how to registration services, define service lifetimes, and express dependencies in C#.
but then im not using an interface with a concrete type so i'm confused on that
should i just learn how to do DI when I learn about asp.net core?
without an interface it's just
services.AddSingleton<Menu>()
as opposed to services.AddSingleton<IMenu, Menu>()
so something like this?
no, none of that is related
validate
not used. input
not used. helpers
not used. _context
not used
AddSingleton<Program>
looks pointlessi think im just gonna have to come back to this later
i have no idea what im doing
i used something like this when i tried Console Dependency Injection.
I dont know if its the perfect way to do DI in consoles, but i think its not that bad.
You could use this example:
I see that my repo is public, you can see it all at this address:
https://github.com/PhilemonPhilippin/IceCreamVendor-repo/tree/master
But i am junior dev and i made it at my even earlier stage, so take it with a grain of salt if i may say
GitHub
GitHub - PhilemonPhilippin/IceCreamVendor-repo: App to practice log...
App to practice loggings and DI. Contribute to PhilemonPhilippin/IceCreamVendor-repo development by creating an account on GitHub.
The best thing to do in this case imo : watch a tutorial (youtube or else) on the subject, try it on your own, research more deeply for every bit you don't understand, and when you write code, F12 keyboard shortcut on every Microsoft base library that you don't understand will be your friend! You got this 💪
thanks for the input! Im gonna try and understand your code tomorrow and see how it goes 💯
and definitely for sure will try again with DI
Using the host is different than using DI. Hosting in .NET lets you get other stuff, namely graceful shutdowns, built-in logging, etc., etc. But if you just want to learn DI then you can wire just that up like this.
wow thank you
it was such a headache trying to understand how to set it up
literally multiple days of no progress
will try and understand this tomorrow 💯
no prob. DI is confusing until it 'clicks' and then it becomes very simple
feel free to message me if you want further explanations
sounds good, will do
thank you again
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.