C
C#12mo ago
Ice_

Dependency injection in WPF

I'm trying to do DI in my WPF app. I've read about it on different sites and followed youtubers but it doesn't seem to work (it runs but I can't reach the registered service, it'll always be null). I am basically trying to inject a DbContext as a Singleton because its scoped nature is causing some issues in the application (entity mismatching etc. = badly written code probably). Help/Tips please!
13 Replies
Ice_
Ice_OP12mo ago
I have to add that I am trying to reach the dbcontext from my repository I made the dbcontext static from MainWindow and that did the trick. I don't like static though
Sir Rufo
Sir Rufo12mo ago
GitHub
GitHub - SirRufo/so-77657479-PageNavigation: Example for stackoverflow
Example for stackoverflow. Contribute to SirRufo/so-77657479-PageNavigation development by creating an account on GitHub.
Sir Rufo
Sir Rufo12mo ago
IDbContextFactory Interface (System.Data.Entity.Infrastructure)
A factory for creating derived DbContext instances. Implement this interface to enable design-time services for context types that do not have a public default constructor. At design-time, derived DbContext instances can be created in order to enable specific design-time experiences such as model rendering, DDL generation etc. To enable design-t...
Mayor McCheese
Mayor McCheese12mo ago
GitHub
wpf/src/WpfWithAspNetCore at main · devdevdeveau/wpf
Contribute to devdevdeveau/wpf development by creating an account on GitHub.
Ice_
Ice_OP12mo ago
Thank you guys. The links you sent do use DI but I don't see any DbContext stuff or maybe I'm blind. I'm not really that experienced so I can only assume I am unaware. Also, I have just opened a few files (from links) and tried to understand them so I have not seen the full picture of things yet. It's getting late etc. I also don't want a contextfactory for my WPF application. That would cause problems as the code is right now. I want to use the very same dbcontext so it can track all the entities correctly.
Mayor McCheese
Mayor McCheese12mo ago
Oh, mine was a DI sample in WPF; there shouldn't be a special requirement for DbContext in wpf, can you show your $code?
MODiX
MODiX12mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Sir Rufo
Sir Rufo12mo ago
You have two examples for WPF with DI container - what keeps you away from using the DbContext? There are thousands of tutorials on how to register a DbContext on the DI container and how to use that
PixxelKick
PixxelKick12mo ago
To register your DbContext for DI, once you have the Microsoft.DependencyInjection libraries installed and working, as well as the Entity Framework Core libs installed, you just use
services.AddDbContext<ApplicationDbContext>(
options => options.UseYourTypeOfSeverFromItsPackage(YourConnectionString));
services.AddDbContext<ApplicationDbContext>(
options => options.UseYourTypeOfSeverFromItsPackage(YourConnectionString));
in your setup logic
Mayor McCheese
Mayor McCheese12mo ago
Problematic issue is the connection string somehow has to be well known to your wpf app.
PixxelKick
PixxelKick12mo ago
I just usually also install the Configuration packages from Microsoft and use IConfiguration stuff
Mayor McCheese
Mayor McCheese12mo ago
Sure; that's fine But still if you have to rotate connection string it can be hard
Sir Rufo
Sir Rufo12mo ago
If you have to rotate any configuration it can be hard - even if you connect to a rest api ;o) But you can keep the rest api config even when you change the underlying database
Want results from more Discord servers?
Add your server