mirror of
https://github.com/qmlnet/qmlnet.git
synced 2026-05-16 14:15:57 -06:00
[GH-ISSUE #63] Support for dependency injection in Models #36
Labels
No labels
bug
enhancement
help wanted
help wanted
pull-request
question
todo
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: github-starred/qmlnet#36
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @MaxMommersteeg on GitHub (Aug 20, 2018).
Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/63
Currently working on a simple todo application (https://github.com/MaxMommersteeg/Qml.Net.TodoApp) and trying to use a repository in my model. I tend to use the
Models(as seen in the examples) as my controller (like a controller in mvc). Creating a controller/model without a parameterless constructor throws aSystem.MissingMethodExceptionexception.I setup my dependency injection as follows:
Program.cs
TodoItemsController.cs
Exception
System.MissingMethodException: 'No parameterless constructor defined for this object.'You can clone and run the
TodoApp.FrontEndproject to reproduce the problem. Any ideas on how to handle dependency injection?@pauldotknopf commented on GitHub (Aug 21, 2018):
Set
Qml.Net.TypeCreator.Currentto your own implementation ofITypeCreator.@MaxMommersteeg commented on GitHub (Aug 21, 2018):
Thanks, is there an example showing the intended use (registering an implementation to an interface.)?
@pauldotknopf commented on GitHub (Aug 21, 2018):
@MaxMommersteeg commented on GitHub (Aug 22, 2018):
Sadly to no avail. The
TodoAppDbContextthrows anObjectDisposedException. Issue can be reproduced by starting the app and adding a todo item using the textfield and button.Not sure whether
TodoAppDbContextis setup correctly for dependency injection yet. TheITodoItemRepositoryis correctly injected into theTodoItemsControllerclass.Program.cs
@pauldotknopf commented on GitHub (Aug 22, 2018):
Try making sure all your types are registered as a singleton.
@MaxMommersteeg commented on GitHub (Aug 22, 2018):
I don't think making
TodoAppDbContexta singleton would be a good practice.. Not sure how to continue from here. For sake of the example app I could write to a simple json file on desk I suppose.@pauldotknopf commented on GitHub (Aug 22, 2018):
When running desktop apps, scope practices are a little different that the per-request model of ASP.NET. I've seen (and used) a singleton model because there isn't a good location to create scope in desktop apps.
@MaxMommersteeg commented on GitHub (Aug 22, 2018):
I agree with you. So the default
AddDbContext<TodoAppDbContext>()method sets the lifetime toScoped. I tried setting it tosingletonmanually:serviceCollection.AddDbContext<TodoAppDbContext>(options => options.UseSqlite(connectionString), ServiceLifetime.Singleton);. Sadly it is giving me the same exception. For some reasonTodoAppDbContextis still disposed.@MaxMommersteeg commented on GitHub (Sep 1, 2018):
Stupid issue, totally my mistake:
Above code was the problem. Setting
ServiceLifetime.Singletonwas correct, but retrieving theTodoAppDbContextin a using disposes (of course) the object after use. See currentProgram.csin my repository for the working code.