Replies: 1 comment
-
If you are interested in maximum correctness, then this is the correct way to do it. Your DTO should be separate from your code that uses it (EG your database logic). That way you can easily create abstractions around it and follow single responsibility principle. This applies to pretty much everything, including ASP Core. That said, I would suggest using a better name than
Where the
This is the "correct" pattern as a view model should wrap a model. But since I am lazy I generally just add a constructor to the view model that uses the model as a backing value for the fields/sets the initial values based on it and exposes a method to get the model back out of it.
Avalonia wont create scopes for you. You can manually create them yourself though. If it's "correct" or not depends on if the services need to be long lived. Generally for a website you don't want to leave a database connection open as you might get a connection exhaustion/memory leaks. The advantage of scoped services is they also get disposed of when the scope is disposed.
As the person who wrote original guide the Avalonia DI guide is based on (and also helped write the Avalonia DI guide), I can say that the guide is just meant to tell you how to bootstrap DI. Not how to actually structure your code. The guide makes a general assumption that you already know how to use DI and just want to set it up. |
Beta Was this translation helpful? Give feedback.
-
Hi and thanks for taking the time to read this. I am new to Avalonia but do have some experience with ASP NET core web MVC. Anyway, the title says its all. I have redone the ToDo list sample from your documentation, added in Reactive toolkit, and tried my best to implement MVVM with DI and Repository pattern + UnitOfWork with a MYSQL Database (Using Dapper as well). Just looking for guidance on if my implementation is correct. So here goes. I will post enough code below and then ask the few questions that i have. I will leave out all my Repository interfaces and UnitOfWork class because I can ask my questions without them. But i can host the repo on GitHub if anyone wants the full code?
ServiceCollectionExtensions.cs
App.axaml.cs
In Models folder i have ToDoITem.cs and ToDoItemMethods.cs
ToDoItemViewModel.cs
And MainWindowViewModel.cs -- (My DI is working so guess i set that up correctly)
Ok now the few Questions i have
Not sure this follows the correct pattern but it seemed to work best to have ToDoItem.cs as a class that only models the data (just the columns that map to the database) and have the class ToDoItemMethods.cs handle all the CRUD methods. Reason being is I needed a way to pass _unitOfWork to the class that handles CRUD methods but also still needed a convenient way to pass the ToDoItem objects from the ViewModel to the Model for data processing and having UnitOfWork in the ToDoItem class constructor really complicated that. Is this an ok approach or am i missing something obvious?
In my GetAllToDo method in the MainWindowViewModel class I needed to map the ToDoItem's that get returned from the database back into ToDoItemViewModel's and add them to the ToDoItems ObservableCollection. I could not find a way to cast a List of ToDoItem's to ObservableCollection of ToDoItemViewModel's. Is there a better way? I guess in general is seems like there is a lot of mapping between ToDoItem objects and the ToDoItemViewModel objects. Guessing that is really the only option?
is Singleton correct lifetime for UnitOfWork. I know in web apps we use Scoped but i would assume to use Singleton for desktop? And what lifetime should be used for the AddMySqlDataSource ? (in code above connection Lifetime defaults to Transient and dataSourceLifetime defaults to Singleton) Maybe Singleton for both connection and datasource? Any knowledge here is much appreciated.
It would be great if we could have an official Avalonia MVVM guide on adding in a real database with DI and Repository + UnitOfWork pattern. Seems like a question that gets asked a lot and I haven't found too much information on the best way to do it. If the code i have is written well enough I wouldn't mind sharing the full repo. or maybe even helping write the tutorial if i have time.
Hope this all makes sense and thank you in advanced for taking a look. I know the post is a bit long..
Beta Was this translation helpful? Give feedback.
All reactions