开发者

Advise on object-oriented design

开发者 https://www.devze.com 2022-12-15 17:42 出处:网络
I would like some help with a OOD query. Say I have the following Customer class: public class Customer

I would like some help with a OOD query.

Say I have the following Customer class:

public class Customer
{
    public int Id { ge开发者_JS百科t; 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.


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.


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消