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;
    }
}
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.