【发布时间】:2019-08-07 15:40:48
【问题描述】:
我们正在审查通用存储库模式中的两种不同方法。 目前,想要将主键映射到 Id。这样做的目的是映射到使用 Id 的通用存储库接口。下面提供了两种解决方案。
.FindPrimaryKey().Properties 对性能有何影响。在尝试查找主键时是否会导致数据库表的模式锁定?它会导致任何应用程序缓慢吗?
与部分类方法解决方案 2 相比,它的性能如何? 哪个选项在性能方面更好?
注意:架构师要求在工作场所使用存储库模式,因此实施它。知道围绕这个问题存在争议,但不是我的呼吁。
脚手架模型示例:
namespace Datatest
{
public partial class Property
{
public int Property { get; set; }
public int DocumentId { get; set; }
public string Address { get; set; }
}
}
所有表的示例通用基础存储库:
public T Get(int id)
{
return Table.Find(id);
}
public async Task<T> GetAsync(int id)
{
return await Table.FindAsync(id);
}
public T Single(Expression<Func<T, bool>> predicate)
{
return All.Single(predicate);
}
public async Task<T> SingleAsync(Expression<Func<T, bool>> predicate)
{
return await All.SingleAsync(predicate);
}
public T FirstOrDefault(int id)
{
return All.FirstOrDefault(CreateEqualityExpressionForId(id));
}
解决方案 1:FindPrimaryKey()
Generic Repository in C# Using Entity Framework
使用 EF FindPrimaryKey()
var idName = _context.Model.FindEntityType(typeof(TEntity))
.FindPrimaryKey().Properties.Single().Name;
解决方案 2:部分类映射
Net Core: Create Generic Repository Interface Id Mapping for All Tables Auto Code Generation
public partial class Property: IEntity
{
[NotMapped]
public int Id { get => PropertyId; set => PropertyId = value; }
}
【问题讨论】:
-
停止在 EF Core 中使用存储库模式。请!你杀了它!
-
嗨@Artur 注意:架构师要求在工作场所使用存储库模式,因此实施它。知道围绕这个问题存在争议,但不是我的呼吁,试图充分利用它
-
尝试向你的老时尚建筑师展示你如何加入 2 个有和没有存储库模式的巨大表。在第一种情况下,整个数据将被物化,并且连接将在内存中执行,这会破坏您的性能以及多年的数据库开发和优化工作。
-
无论如何这是另一个话题,但感谢您的意见,:) 我们已经在工作场所就这个问题进行了辩论,他们告诉我们继续前进
-
如果您有外键和导航属性会有所帮助,但在某些随机连接的情况下则不然。无论如何,所有这些变通方法只会增加代码的复杂性,并使新团队成员花费大量时间来学习他已经熟悉的包装器框架。
标签: c# entity-framework asp.net-core .net-core entity-framework-core