身份模型的Blazor最佳做法 [英] Blazor best practices with Identity model

查看:107
本文介绍了身份模型的Blazor最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Blazor 3.2 Preview 3开发Web应用程序.我正在使用WebAssembly版本和Identity. 我的应用程序包括3个部分:客户端,服务器和共享. (就像基本模板一样).

I am currently working on a web app using Blazor 3.2 preview 3. I am using the WebAssembly version and Identity. My app consists of 3 parts: Client, Server and Shared. (just like the basic template).

我的模型类存储在共享"部分中.但是,用户"的模型类存储在服务器"文件夹中.

My models classes are stored in the Shared section. However, the model class for Users is stored in the Server folder.

我想使用一种模型(存储在Shared项目中):

I would like to use in one of my model (stored in the Shared project):

 public virtual ApplicationUser User { get; set; }

这是不可能的,因为共享项目无权访问服务器项目.

This is not possible because the Shared project does not have access to the Server project.

我是否必须将所有模型类放入服务器,并在共享部分中使用DTO?

Do I have to put all my models classes in the Server and use DTOs in the Shared part?

是将我所有的类放到Shared文件夹中(包括ApplicationUsers)吗?

Instead put all of my classes in the Shared folder (including ApplicationUsers)?

最佳做法是什么?

推荐答案

这是不可能的,因为共享项目无权访问服务器项目.

This is not possible because the Shared project does not have access to the Server project.

这不是必需的:您可以将ApplicationUser移到Shared并将Microsoft.Extensions.Identity.Stores nuget包添加到Shared.那就是定义IdentityUser的地方.但是,这不是最佳实践.除其他问题外,这还会大大增加您的客户端应用程序的大小.

That is not necessary: you could move the ApplicationUser to Shared and add the Microsoft.Extensions.Identity.Stores nuget package to Shared. That is where IdentityUser is defined. However, that would not be a best practice. Amongst other problems it would increase the size of your Client app a lot.

我是否必须将所有模型类放入服务器,并在共享部分中使用DTO?

Do I have to put all my models classes in the Server and use DTOs in the Shared part?

这是首选模式,当然在更大的项目之前.对于一些小问题,模型类和DTO之间的区别有时会变得模糊.但是,由于IdentityUser带来了一些依赖关系,并且包含了客户端上不需要或不需要的字段,因此创建ApplicationUserDto总是很必要的.

That is the preferred pattern, certainly fore a larger project. For something small the distinction between Model classes and DTOs sometimes becomes blurred. But because IdentityUser brings in some dependencies, and contains fields you don't need or want on the client, it would always pay to create an ApplicationUserDto.

这篇关于身份模型的Blazor最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆