When to make a method part of an interface or a concrete implementation? [duplicate]

I create an interface for database access to a MySQL database. Currently, this is the only way to access day, but to make it easier to test and in the event that the data access might change, I used an interface.

However, now I am faced with the dilemma of which methods I should put on the interface and which methods I should put on the concrete implementation” directly. Is there a rule of thumb for this?

I will give an example below where I have some CRUD operations for a ICar interface, but then some other utility functions that are just on the concrete implementation. Should these be on the interface as well? I wouldn’t want a user to have to implement every method if they didn’t need to?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>interface ICar {
Car GetCar(int id);
void DeleteCar(int id);
void InsertCar(Car car)
Car UpdateCar(Car car);
}
class Car : ICar {
Car GetCar(int id) {}
void DeleteCar(int id) {}
void InsertCar(Car car) {}
Car UpdateCar(Car car) {}
// methods not on interface, but should they be
public string GetCarVIN(Car car) {}
public string GetCarColor(Car car) {}
public string GetCarMake(Car car) {}
public string GetCarModel(Car car) {}
public string GetCarOwner(Car car) {}
}
</code>
<code>interface ICar { Car GetCar(int id); void DeleteCar(int id); void InsertCar(Car car) Car UpdateCar(Car car); } class Car : ICar { Car GetCar(int id) {} void DeleteCar(int id) {} void InsertCar(Car car) {} Car UpdateCar(Car car) {} // methods not on interface, but should they be public string GetCarVIN(Car car) {} public string GetCarColor(Car car) {} public string GetCarMake(Car car) {} public string GetCarModel(Car car) {} public string GetCarOwner(Car car) {} } </code>
interface ICar {
   Car GetCar(int id);
   void DeleteCar(int id);
   void InsertCar(Car car)
   Car UpdateCar(Car car);
}

class Car : ICar {

  Car GetCar(int id) {}

  void DeleteCar(int id) {}

  void InsertCar(Car car) {}

  Car UpdateCar(Car car) {}

  // methods not on interface, but should they be

  public string GetCarVIN(Car car) {}

  public string GetCarColor(Car car) {}

  public string GetCarMake(Car car) {}

  public string GetCarModel(Car car) {}

  public string GetCarOwner(Car car) {}
}

4

OP is having this confusion because of poor naming.


The reason OP asks this question is because OP has a confusion between the Repository Pattern and general object-oriented design.

The repository object sits between the OOP design – say, a Car class, and the database.


The “repository-object” relation shall not be confused with the “interface-implementation” relation or the “class-object” / “static-instance” relations.


A Car object provides access to the properties that a car (in real life) will usually have (named VIN, Color, Make, Model and Owner in the code example).

On the contrary, the repository object looks like a CarCollection in the abstract sense. The repository object is where CRUD operation belongs to.

To fix this issue, CRUD methods should exist on a CarRepository class (and optionally implementing an ICarRepository interface), and then Car-related methods should exist on a Car object and/or a ICar interface.


In C#, you can both keep the interfaces clean and still give the convenience to your users by using Extension methods.

Keep in mind that if the conceptual mix-up of repository object with data object is enough to give OP headaches, users of such conceptual mix-up is going to get the same kind of headaches, unless the users are being forewarned about it.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public interface ICarRepository
{
ICar GetCar(int carId);
// ... other CRUD and query methods,
// ... but NO car-properties here.
}
public interface ICar
{
string Owner { get; }
string VIN { get; }
// ... other car-properties here.
}
namespace ExtensionMethods
{
public static class CarExtensions
{
public static string GetCarOwnerFromId(this ICarRepository carRepo, int carId)
{
return carRepo.GetCar(carId).Owner;
}
public static string GetCarVINFromId(this ICarRepository carRepo, int carId)
{
return carRepo.GetCar(carId).VIN;
}
}
}
</code>
<code>public interface ICarRepository { ICar GetCar(int carId); // ... other CRUD and query methods, // ... but NO car-properties here. } public interface ICar { string Owner { get; } string VIN { get; } // ... other car-properties here. } namespace ExtensionMethods { public static class CarExtensions { public static string GetCarOwnerFromId(this ICarRepository carRepo, int carId) { return carRepo.GetCar(carId).Owner; } public static string GetCarVINFromId(this ICarRepository carRepo, int carId) { return carRepo.GetCar(carId).VIN; } } } </code>
public interface ICarRepository
{
    ICar GetCar(int carId);

    // ... other CRUD and query methods,
    // ... but NO car-properties here.
}

public interface ICar
{
    string Owner { get; }
    string VIN { get; }

    // ... other car-properties here.
}

namespace ExtensionMethods
{
    public static class CarExtensions
    {
        public static string GetCarOwnerFromId(this ICarRepository carRepo, int carId)
        {
            return carRepo.GetCar(carId).Owner;
        }

        public static string GetCarVINFromId(this ICarRepository carRepo, int carId)
        {
            return carRepo.GetCar(carId).VIN;
        }
    }
}

1

The interface should be as generic as possible. So, while this is an interface of Cars, if we assume that any car will be capable of having an owner, then the method for getting the owner should be inside the interface.

However, any non-generic method, e.g. the method for taking the model of the Car’s TV should not be inside the interface, since not all cars will have a TV.

Try to include the methods that any car can have inside the interface. All the other methods that only some cars will possess will be left outside the interface.

However, now I am faced with the dilemma of which methods I should put on the interface and which methods I should put on the concrete implementation” directly. Is there a rule of thumb for this?

Split the difference and create an abstract class.

2

If you intend to use the interface for easier testing, then it should expose all the logic you wish to test on the Car business object. That means every operation that relates to the Car and not the technical implementation.

In your example, I would include all those methods in the ICar.

I would not leave out some methods even if only some cars have them. You should put those in a separate interface, or use inheritance, depending on the situation.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật