My C# application uses the Repository Pattern, and I have a terrible doubt as how to implement the "Update" part of CRUD operations. Specifically, I don't know how to "tell" the repository which object I want to replace (so that persistence can be carried out aftwerwards.
I have the following code in a console application (written just as example) that uses the libraries from the application:
class Program
{
static void Main(string[] args) {
var repo = new RepositorioPacientes();
var listapacientes = repo.GetAll();
// Choosing an element by index
// (should be done via clicking on a WPF ListView or DataGrid)
var editando = listapacientes[0];
editando.Nome = "Novo Helton Moraes";
repo.Update(editando);
}
}
Question is: How am I supposed to tell the repository which element it has to update? Should I traverse the whole repository using an equality comparer to find the element?
NOTE: this repository encapsulates data-access using XML serialization, one file per entity, and my entities (of type Paciente
in this example) have the [Serializable]
attribute. That said, the "Update" operation would end up replacing the XML file of the given entity with another with updated data, via Serialize
method.
I am not concerned with that, though. what I cannot figure out is how to implement repo.Update(entity)
so that the repo knows that this entity that is being passed back is the same that has been selected from listapacientes
, which is not the repository itself.
Thanks for reading!