سؤال

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!

هل كانت مفيدة؟

المحلول

Ultimately, this should come down to the time-space trade off. Your suggestion of implementing an equality comparer and iteration through the entire repository maximizes runtime but uses little space by using a List<T> as the data structure used by the repository. In the worst case, where you update the last element of the list, you will need to iterate through the entire thing and run the equality operation on each element until it matches the last one. This is feasible for smaller repositories.

Another very common solution would be to override the GetHashCode of your T types in the repository, and using a HashSet<T> or Dictionary<T, V> as the data structure in the repository. The latter would minimize time to O(1) but take more space for the data structure. This is probably a better solution for much larger repositories, especially so if each of the type T objects has one property, like a GUID or database identifier associated with it that is unique because then you have a very easy hash value.

There are other data structures you can consider for your repository based on the exact use-case of your repository. For example, if you are trying to maintain an ordering of elements in the repository where only the highest or lowest element is fetched at a time, a PriorityQueue or Heap might be for you. If you spend time thinking about the data structure that backs your repository, the rest of the implementation should solve itself.

نصائح أخرى

Don't load everything into memory. Try it something like this.

class Program
{
    static void Main(string[] args) {

    var repo = new RepositorioPacientes();

    var editando = repo.SingleOrDefault(p => p.Id == 1);

    editando.Nome = "Novo Helton Moraes";

    repo.Update(editando);
  }

}

you can use this link: http://www.codeproject.com/Articles/644605/CRUD-Operations-Using-the-Repository-Pattern-in-MV

And try this code

    public ActionResult Edit(int id)
        {
    Book book = _bookRepository.GetBookByID(id);
    return View(book);
    }  
     [HttpPost]
    public ActionResult Edit(Book book)
    {
      try
       {
         if (ModelState.IsValid)
          {
           _bookRepository.UpdateBook(book);
           _bookRepository.Save();
           return RedirectToAction("Index");
          } 
         }
          catch (DataException)
          {               
           ModelState.AddModelError("", "Unable to save changes. Try again, " + 
           "and if the problem persists see your system administrator.");
        }
            return View(book);
       }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top