简体   繁体   English

Mvvm Light和Entity Framework核心最佳实践

[英]Mvvm Light and Entity Framework Core best practices

I'm creating a new UWP application using Mvvm Light and Entity Framework Core. 我正在使用Mvvm Light和Entity Framework Core创建一个新的UWP应用程序。 I'm new to these 2 technologies. 我是这两项技术的新手。

I create my model : the Article class is a simple ObservableObject with 3 properties : Id , Référence and Désignation . 我创建了我的模型: Article类是一个简单的ObservableObject,它有3个属性: IdRéférenceDésignation

My DbContext is the following : 我的DbContext如下:

public class UniversalTest1Context : DbContext
{
    public DbSet<Article> Articles { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Filename=UniversalTest1.db");
    }

}

I'm looking for the best way to manage the DbContext and my different views. 我正在寻找管理DbContext和我不同观点的最佳方法。

Is it better to create one DbContext for the whole application ? 为整个应用程序创建一个DbContext更好吗? I don't really like that idea . 我真的不喜欢这个主意

Do I have to create a DbContext in each ViewModel ? 我是否必须在每个ViewModel中创建一个DbContext? I like this more . 我更喜欢这个
When the user double tap an item in the article list view, I navigate to the article detail view and pass the article to the view model associated to the article detail view. 当用户双击文章列表视图中的项目时,我导航到文章详细信息视图并将文章传递给与文章详细信息视图关联的视图模型。 But this already existing article is not related to the DbContext of the Article detail view model. 但是这篇已经存在的文章与文章详细视图模型的DbContext无关。

Is it possible to instantiate the DbContext only when needed ? 是否可以仅在需要时实例化DbContext? My prefered option . 我喜欢的选择
For this, I pass the article from the list view model to the detail view model. 为此,我将文章从列表视图模型传递给详细视图模型。 And then, when the user clicks save I execute something like this : 然后,当用户单击“保存”时,我执行以下操作:

using (var db = new UniversalTest1Context())
{
    db.Articles.Add(article);
    await db.SaveChangesAsync();
}

Of course, this works for new articles (insert), but not for existing ones (update). 当然,这适用于新文章(插入),但不适用于现有文章(更新)。

I have difficulties making up my mind here. 我在这里下定决心很困难。

Many thanks in advance, Julien 非常感谢Julien

In my opinion, you should hide all storing operations by interface (like IArticleRepo or smth like that) and you should use IoC for accessing it (if some class wants to work with Store, it have to declare IArticleRepo in ctor parameters). 在我看来,你应该通过接口隐藏所有存储操作(比如IArticleRepo或类似的那样),你应该使用IoC来访问它(如果某个类想要使用Store,它必须在ctor参数中声明IArticleRepo)。 Also, inside this interface IArticleRepo you can delcare neecessarry operations with Articles, like IArticleRepo.AddArticle(Article a). 此外,在此界面IArticleRepo中,您可以使用文章来进行必要的操作,例如IArticleRepo.AddArticle(文章a)。 If you do so, you can choose later, whether you want to create dbcontext for each operation or not OR maybe you want to make IArticleRepo implementation as singletone using IoC container. 如果这样做,您可以稍后选择是否要为每个操作创建dbcontext,或者您可能希望使用IoC容器将IArticleRepo实现为单线程。 But such decisions should not affect on other code. 但是这样的决定不应该影响其他代码。 As very simle sample in my code (I showing only this file, whole project is ugly and I will change it) https://github.com/tym32167/arma3beclient/blob/master/src/Arma3BE.Client.Libs/Repositories/PlayerRepository.cs#L23 在我的代码中非常简单的样本(我只显示这个文件,整个项目很难看,我会更改它) https://github.com/tym32167/arma3beclient/blob/master/src/Arma3BE.Client.Libs/Repositories/ PlayerRepository.cs#L23

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM