存储库模式:编辑/删除方法的方法签名
我正在尝试自学存储库模式,并且我有一个最佳实践问题。
想象一下我有一个实体(这是一个 linq to sql 实体,但为了清楚起见,我已经删除了所有 linq to sql 代码和数据注释属性):
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }
public string Telephone { get; set; }
}
到目前为止,我的接口的抽象存储库是:
public interface IPersonRepository
{
IQueryable<Person> Person { get; }
void Add(Person person);
void SubmitChanges();
// I want an Edit method here
// I want a Delete method here
}
我的问题是:是编辑/删除方法的方法签名吗?这些的最佳实践是什么?例如,如果 Id 是 Person 的唯一“不可编辑”(即键)属性,您将如何实现它?
Edit 是否应该采用 Person 参数,然后编辑方法代码查找具有提供的 id 的实体并以这种方式进行编辑?
删除应该采用 Person 参数,还是只是一个 id?
我试图思考什么是最合乎逻辑、最清晰的方法,但我很困惑,所以我想问一下!
谢谢!
I'm trying to teach myself the repository pattern, and I have a best practices question.
Imagine I have the entity (this is a linq to sql entity but I've stripped all the linq to sql code and the data annotations attributes for clarity):
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }
public string Telephone { get; set; }
}
The abstract repo for my interface so far is:
public interface IPersonRepository
{
IQueryable<Person> Person { get; }
void Add(Person person);
void SubmitChanges();
// I want an Edit method here
// I want a Delete method here
}
My question is this: What would be the method signature for the edit / delete methods? What would be the best practices for these? If Id for example was the only "uneditable" (i.e. the key) property of a Person, how would you implement this?
Should Edit take a Person parameter, and then the edit method code lookup the entity with the supplied id and edit that way?
Should delete take a Person parameter, or simply an id?
I'm trying to think what would be the most logical, clear way to do it, but I'm getting all confused so thought I'd ask!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的删除方法应该如下所示。
如果您需要更通用的模式方法,请查看此博客文章:实体框架存储库和工作单元T4模板
Your Delete method should look like this.
If you need a more generic approach of the patterns, please take a look at this blog post: Entity Framework Repository & Unit Of Work T4 Template
我通常将它们(实体和 Id)用于删除:
并在实体上保存一个用于保存:
您还可以考虑为标准 CRUD 操作创建一个通用基础存储库:
如果您只需要一个
Save(T 实体)
或Insert(TEntity)
和Update(Tentity)
取决于您的架构。I generaly have them both (entity and Id) for delete:
and one with on the entity for save:
You might also consider to make a generic base repository for the standard CRUD actions:
If you just need a
Save(T entity)
or aInsert(T entity)
andUpdate(T entity)
depends a little bit on your architecture.