【发布时间】:2019-07-24 02:23:18
【问题描述】:
我是 mongo 的新手,我正在寻找有关处理引用关系并在 .net 核心中对其进行建模时的最佳实践的一些指导。
是的,这是通常的“在 mongodb 中加入??”问题。但是我还没有找到一个好的答案。
为了简单起见,假设我有一个简单的 API,我用控制器构建了一个用于管理用户和帐户的 API。
在 mongo 中有两个集合,Accounts 和 Users。用户属于其父帐户。在这种情况下,我不想采用嵌入式文档路线,因此每个用户文档中都会有一个 AccountID,以将用户链接回其父帐户。
我目前在 .net 中的实体是:
public class User
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("firstName")]
public string FirstName { get; set; }
[BsonElement("lastName")]
public string LastName { get; set; }
[BsonElement("email")]
public string Email { get; set; }
[BsonElement("status")]
public string Status { get; set; }
[BsonElement("type")]
public string Type { get; set; }
[BsonElement("createdDateTime")]
public DateTime CreatedDateTime { get; set; }
[BsonElement("modifiedDateTime")]
public DateTime ModifiedDateTime { get; set; }
[BsonRepresentation(BsonType.ObjectId)]
[BsonElement("accountId")]
public string AccountId { get; set; }
}
public class Account
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("name")]
public string Name { get; set; }
[BsonElement("status")]
public string Status { get; set; }
[BsonElement("type")]
public string Type { get; set; }
[BsonElement("createdDateTime")]
public DateTime CreatedDateTime { get; set; }
[BsonElement("modifiedDateTime")]
public DateTime ModifiedDateTime { get; set; }
}
然后使用控制器中的 AutoMapper 将它们映射到模型
public class UserModel
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string Email { get; set; }
[Required]
public string Status { get; set; }
[Required]
public string Type { get; set; }
[Required]
public DateTime CreatedDateTime { get; set; }
[Required]
public DateTime ModifiedDateTime { get; set; }
[Required]
public string AccountId { get; set; }
}
public class AccountModel
{
[Required]
public string Name { get; set; }
[Required]
public string Status { get; set; }
[Required]
public string Type { get; set; }
[Required]
public DateTime CreatedDateTime { get; set; }
[Required]
public DateTime ModifiedDateTime { get; set; }
}
以及使用映射器的控制器方法示例:
[HttpGet]
public async Task<ActionResult<List<AccountModel>>> Get()
{
try
{
var results = await _repository.Get();
return _mapper.Map < List < AccountModel >>(results);
}
catch (Exception ex)
{
return this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure");
}
}
一切正常。我可以调用控制器方法,获取数据,然后将其从实体映射到模型,然后从控制器方法返回。
问题是这样的:我想返回包含帐户信息的用户数据(例如:帐户名称)。所以只是一个简单的连接。
我想我已经掌握了如何使用此answer 中概述的方法之一进行连接本身。但我的问题是,是否有关于如何设置我的实体和模型以使存储尽可能干净的最佳实践?
我正在考虑向 User 实体添加一个属性来存储相关帐户。使用 [BsonIgnore] 属性进行标记,使其远离数据库。
[BsonIgnore]
public Account Account { get; set; }
属性
[BsonRepresentation(BsonType.ObjectId)]
[BsonElement("accountId")]
public string AccountId { get; set; }
仍会保留在用户实体中,因此会保留引用。
然后,User 模型可以具有类似
的属性 public string AccountName { get; set; }
它们使用映射器填充。
当您想引用相关对象而不是嵌入它们时,这是最好的设置方法吗?这里有什么我遗漏的问题吗?
【问题讨论】:
标签: c# mongodb asp.net-core-mvc