存储库模式:编辑/删除方法的方法签名

发布于 2024-10-15 06:33:00 字数 820 浏览 0 评论 0原文

我正在尝试自学存储库模式,并且我有一个最佳实践问题。

想象一下我有一个实体(这是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝眼睛不忧郁 2024-10-22 06:33:01

您的删除方法应该如下所示。

    void Delete(Person person); 

如果您需要更通用的模式方法,请查看此博客文章:实体框架存储库和工作单元T4模板

Your Delete method should look like this.

    void Delete(Person person); 

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

逆光飞翔i 2024-10-22 06:33:00

我通常将它们(实体和 Id)用于删除:

void Delete(Person person);
void DeleteById(int personId);

并在实体上保存一个用于保存:

void Save(Person person);

您还可以考虑为标准 CRUD 操作创建一个通用基础存储库:

public interface IBaseRepository<T>
{
    T GetById(Guid id);
    IList<T> GetAll();
    void Delete(T entity);
    void DeleteById(Guid id);
    void Save(T entity);
}

如果您只需要一个 Save(T 实体)Insert(TEntity)Update(Tentity) 取决于您的架构。

I generaly have them both (entity and Id) for delete:

void Delete(Person person);
void DeleteById(int personId);

and one with on the entity for save:

void Save(Person person);

You might also consider to make a generic base repository for the standard CRUD actions:

public interface IBaseRepository<T>
{
    T GetById(Guid id);
    IList<T> GetAll();
    void Delete(T entity);
    void DeleteById(Guid id);
    void Save(T entity);
}

If you just need a Save(T entity) or a Insert(T entity) and Update(T entity) depends a little bit on your architecture.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文