title: Entity Framework Repository Pattern in DAL, how to implement Update functions?
body: <p>or one of our projects (a C# Windows Service, mainly for processing data from external resources and read/write this data to a SQL Server database) we have implemented the Repository Pattern in the DAL, and we created a repository class for each POCO object (which represents a table row). The use of these Repository classes is managed by a Unit of Work pattern, so all Repositories share the same DbContext within a single unit of work, but maybe that's irrelevant to the quesion.</p>
  
  <p>The repository class for, let's say a Customer entity, is implemented as following:</p>
  
  <pre><code>public class CustomerRepository
  {
      private readonly MyContext _context;
      public CustomerRepository(MyContext context)
      {
          _context = context;
      }
  
      public Customer GetCustomer(string id)
      {
          return _context.Customers.Find(new object[] {id});
      }
  
      public IEnumerable&lt;Customer&gt; GetAllCustomers(Func&lt;Customer,bool&gt; predicate )
      {
          return _context.Customers.Where(predicate);
      }
      public IEnumerable&lt;Customer&gt; GetCustomerByCity(string city)
      {
          return _context.Customers.Where(c=&gt;s.City.Equals(city));
      }
  
  
      public void SetAge(string id, byte age)
      {
           _context.Customers.Find(new object[] { id }).Age = age;
      }
  
      public void Update(Customer customer)
      {
          _context.Entry(customer).State = EntityState.Modified;
      }
  
  }
  </code></pre>
  
  <p>Now i have a few questions about what is best practice for the update-methods:</p>
  
  <ul>
  <li>Is it recomended to have separate methods for updating specific fields in de database, like the SetAge() method?</li>
  <li>Or Is it recommended to get the Customer object to our BLL (with GetCustomer(id)), and just set the properties in the BLL like
  customer.Age = 35;, and then save this changes?</li>
  </ul>
  
  <p>The update operations are mostly limited to a single field in a single row.</p>
  
  <p>Maybe anyone would like to share some real-world thoughts with me?</p>