How much to encapsulate when objects being dealt with are pretty similar

As I have been answering questions related to object-oriented design and how to go about reducing the amount of classes to make sure that there is not a lot of “class clutter” and code repetition I have started doubting myself as to how much encapsulation is too much encapsulation and by the same token how much encapsulation is not enough encapsulation.

Here is a specific scenario. One of the most painful ones, which I have come across a few times already, is when one is trying to retrieve few pieces of similar data from the database and as a result the row objects and repositories look very similar as well.

This begs a question as to how much should one encapsulate. I guess for the entity objects one could create a base class, but what about the repositories?

Most of the mapping code is going to be similar but are you really going to put half of mapping in the base class and half of it in the subclasses? Not that you can’t but for me personally it is a code smell in a sense that your base class is supposed to facilitate the creation of its derived classes but not help them with their method implementation. It should allow them to override methods but not give them half of the mapping logic and then let them implement the rest.

Would you rather completely separate the repositories even if they were a bit similar in data mapping (the entity objects would be different for each repository) or would you try to base class some of it?

Basically my struggle is this:

Would you rather let each repository encapsulate all that it needs for itself even if some of it might be similar to other repositories?

Or

Would you rather base class it by getting rid of the duplication and as a result have that strange design where a base class provides half the logic and the subclasses take care of the rest?

Example:

public class RepositoryA
{
    private EntityObjectA load()
    {
         // load from db by calling a stored procedure
         // once loaded we do mapping
     }
 }

public class RepositoryB
{
    private EntityObjectB load()
    {
        // load from db by calling a stored procedure
        // once loaded we do mapping
    }
}

public class RepositoryC
{
    private EntityObjectC load()
    {
        // load from db by calling a stored procedure
        // once loaded we do mapping
    }
}

public class EntityObjectA
{
    public string field1 { get; set; }
    public string field2 { get; set; }
    public string field3 { get; set; }
}

public class EntityObjectB
{
    public string field1 { get; set; }
    public string field2 { get; set; }
    public string field3 { get; set; }
    public string field4 { get; set; }
    public string field5 { get; set; }
}

public class EntityObjectC
{
    public string field1 { get; set; }
    public string field2 { get; set; }
}

Now then would you make a baseclass as an EntityObjectBase?

That I can still understand but what about the repositories?

I mean you can pretty much see mapping is going to be pretty similar but does it worth trying to combine them?

How far do you go with encapsulation because in this case there will be some code repeat if we don’t, but if we do we get a weird type of design.

3

Not that you can’t but for me personally it is a code smell in a sense
that your base class is supposed to facilitate the creation of its
derived classes but not help them with their method implementation.

I’m not sure if this assertion is necessarily true.

I can think of many examples where it’s accepted for base classes to provide functions which are useful to the internal implementation of classes extending them. Perhaps a trivial example would be something like a toString() or equals() method which is often useful to the internal implementation of child classes. Also, the protected access modifier is explicitly intended for the scenario where a function should be accessible to a child internally but not externally.

In your case I think a combination of a template method pattern and generics could be used. The load function would belong to the abstract class, it would call a protected getStoredProc() function (not implemented in the abstract class), and return a generic type. Then the actual implementation only has to implement the getStoredProc() method and define the type.

1

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

How much to encapsulate when objects being dealt with are pretty similar

As I have been answering questions related to object-oriented design and how to go about reducing the amount of classes to make sure that there is not a lot of “class clutter” and code repetition I have started doubting myself as to how much encapsulation is too much encapsulation and by the same token how much encapsulation is not enough encapsulation.

Here is a specific scenario. One of the most painful ones, which I have come across a few times already, is when one is trying to retrieve few pieces of similar data from the database and as a result the row objects and repositories look very similar as well.

This begs a question as to how much should one encapsulate. I guess for the entity objects one could create a base class, but what about the repositories?

Most of the mapping code is going to be similar but are you really going to put half of mapping in the base class and half of it in the subclasses? Not that you can’t but for me personally it is a code smell in a sense that your base class is supposed to facilitate the creation of its derived classes but not help them with their method implementation. It should allow them to override methods but not give them half of the mapping logic and then let them implement the rest.

Would you rather completely separate the repositories even if they were a bit similar in data mapping (the entity objects would be different for each repository) or would you try to base class some of it?

Basically my struggle is this:

Would you rather let each repository encapsulate all that it needs for itself even if some of it might be similar to other repositories?

Or

Would you rather base class it by getting rid of the duplication and as a result have that strange design where a base class provides half the logic and the subclasses take care of the rest?

Example:

public class RepositoryA
{
    private EntityObjectA load()
    {
         // load from db by calling a stored procedure
         // once loaded we do mapping
     }
 }

public class RepositoryB
{
    private EntityObjectB load()
    {
        // load from db by calling a stored procedure
        // once loaded we do mapping
    }
}

public class RepositoryC
{
    private EntityObjectC load()
    {
        // load from db by calling a stored procedure
        // once loaded we do mapping
    }
}

public class EntityObjectA
{
    public string field1 { get; set; }
    public string field2 { get; set; }
    public string field3 { get; set; }
}

public class EntityObjectB
{
    public string field1 { get; set; }
    public string field2 { get; set; }
    public string field3 { get; set; }
    public string field4 { get; set; }
    public string field5 { get; set; }
}

public class EntityObjectC
{
    public string field1 { get; set; }
    public string field2 { get; set; }
}

Now then would you make a baseclass as an EntityObjectBase?

That I can still understand but what about the repositories?

I mean you can pretty much see mapping is going to be pretty similar but does it worth trying to combine them?

How far do you go with encapsulation because in this case there will be some code repeat if we don’t, but if we do we get a weird type of design.

3

Not that you can’t but for me personally it is a code smell in a sense
that your base class is supposed to facilitate the creation of its
derived classes but not help them with their method implementation.

I’m not sure if this assertion is necessarily true.

I can think of many examples where it’s accepted for base classes to provide functions which are useful to the internal implementation of classes extending them. Perhaps a trivial example would be something like a toString() or equals() method which is often useful to the internal implementation of child classes. Also, the protected access modifier is explicitly intended for the scenario where a function should be accessible to a child internally but not externally.

In your case I think a combination of a template method pattern and generics could be used. The load function would belong to the abstract class, it would call a protected getStoredProc() function (not implemented in the abstract class), and return a generic type. Then the actual implementation only has to implement the getStoredProc() method and define the type.

1

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