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!
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.