0

I am little bit confused about whether I can put all below functions to one class or if I should split into more classes. I think GetCustomerByID and AddCustomerData got different responsibilites but on the other hand I think it's the same responsibility. Please help me by clearing the concept what is SRP in following code.

public class CustomerHelper
{
    NorthwindEntities db = new NorthwindEntities();

    public List<Customer> GetAllCustomers()
    {
        return db.Customers.ToList();
    }

    public Customer GetCustomerByID(string customerid)
    {
        return db.Customers.Find(customerid);
    }

    public int AddCustomerData(Customer customer)
    {  
        db.Customers.InsertOnSubmit(customer);
        db.SubmitChanges();
        return customer.CustID;
    }
}
Tauqeer
  • 1
  • 4
  • You can think of all of those methods having the "Manage Customer" responsibility. – T. Sar Aug 15 '17 at 11:27
  • 1
    Possible duplicate of [How to determine if a class meets the single responsibility principle?](https://softwareengineering.stackexchange.com/questions/154723/how-to-determine-if-a-class-meets-the-single-responsibility-principle) – gnat Sep 09 '17 at 07:02

1 Answers1

5

Yes, the GetAllCustomers, GetCustomerByID and AddCustomerData functions can co-exist in one class without violating the Single Responsibility Principle.

Each of those functions does something that is related to the persistent storage of Customer objects, either retrieving such objects or storing them.
The single responsibility of the CustomerHelper class is "the persistent storage of Customer objects".

As a side note, a more conventional name for CustomerHelper would have been CustomerRepository.

Bart van Ingen Schenau
  • 71,712
  • 20
  • 110
  • 179