【发布时间】:2015-02-25 10:10:05
【问题描述】:
我正在我的项目中实现存储库装饰器模式:
[Auditable]
public class Product
{
public int Id {get; set;}
public string Name {get; set;}
}
我从以下链接中得到了这个想法。
https://efpatterns.codeplex.com/discussions/282699
但未能成功实施。然后我开始学习装饰器模式和 DataAnnotation,因为 Product 实体上的 Auditable 属性的方式在 DataAnnotation 和装饰器模式中有些相似。所以我的问题是它们是一样的吗?如果它们相同,那么我将如何在我的项目中实现可审核存储库模式(更多链接)。
【问题讨论】:
-
我的理解是“装饰器”(通常)添加功能,而属性(通常)旨在添加元数据,然后由任意功能解释/使用。明显有例外(比如MVC filterAttribute)
-
存储库并不是抽象 EF 的绝佳方式。建议看Unit of Work design pattern
-
但是 DataAnnotation 中的静态类 TypeDescriptor 提供了与装饰器模式类似的功能。
标签: asp.net-mvc entity-framework repository-pattern data-annotations system.componentmodel