tags:

views:

81

answers:

3

Hi there.

I would like some help with a OOD query.

Say I have the following Customer class:

public class Customer
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

It's a simple placeholder for customer related data. Next comes the CustomerFactory, which is used to fetch customers:

public static class CustomerFactory
{
    public static Customer GetCustomer(int id)
    {
        return null; // Pretend this goes off to get a customer.
    }
}

Now, if I wanted to write a routine named UpdateCustomer(Customer customer) can someone suggest where I could place this method?

Obviously I don't want to use the Customer class since that would be against SRP (Single Responsibility Principle), also I don't see it as a good idea to attach the method to the CustomerFactory class, since it's only role is to get customers from the database.

So it looks like I'm going to need another class, but I don't know what to name it.

Cheers. Jas.

+4  A: 

What you have called a Factory isn't a Factory at all. It's a Repository.

A Factory handles the instansiation of various classes sharing a common Interface or Class Hierarchy based on some set of parameters.

A Repository handles the retrieval and management of data.

The Repository would definitely have the UpdateCustomer(Customer customer) method in it as well as the GetCustomer(int id) method.

Justin Niessner
Ahh, great. This is where I've made things muddy for myself, by naming the class CustomerFactory, rather then CustomerRepository. That already seems better.
Jason Evans
A: 

Wouldn't your UpdateCustomer routine be placed in your DAL (Data Access Layer). You should define a class to handle inserts or updates to the database and then pass a customer object to it.

You could write the DAL class to handle all of this but I don't see any issue in storing it in your CustomerFactory class, although as mentioned it is not really a factory.

JonH
+4  A: 

You are more on less on your way to creating a Repository. Do something like this:

public interface ICustomerRepository
{
    Customer SelectCustomer(int id);

    void UpdateCustomer(Customer customer);

    void DeleteCustomer(int id);

    void CreateCustomer(Customer customer);
}

Then create concrete implementations of this interface (the interface is really just because it's good practice to program against interfaces - you could skip it, though, although I would recommend that you keep it).

Mark Seemann
awesome advice +1!
JonH