Abstract base or helper class

I just found out a little about Composition over inheritance . I was wondering if I should apply the concept to what I wrote recently.

We previously had two classes that were almost the same, except for a couple of small differences. They contained some basic database access functions, but worked on different (but related) types of objects. So we previously had a class that was structured like this:

class BallMillDBHandler{

    public BallMillDBHandler(){ ... }

    public void InsertTool(BallMill tool) { ... }
    public BallMill QueryTool(string toolID) { ... }
    public void UpdateTool(BallMill tool) { ... }
    public void DeleteTool(string toolID) { ... }
}

class DiamondToolDBHandler{

    public DiamondToolDBHandler(){ ... }

    public void InsertTool(DiamondTool tool) { ... }
    public DiamondTool QueryTool(string toolID) { ... }
    public void UpdateTool(DiamondTool tool) { ... }
    public void DeleteTool(string toolID) { ... }
}

I took most of the almost duplicated methods and reorganized them into a class BaseToolDBHandler()and inherited it from the other two, providing several abstract methods and properties to handle differences in access to the database parameters themselves.

BaseToolDBHandler , , /? ?

+5
4

, , /.

class DBHandler<TTool> where TTool : ToolBase // or ITool
{
    public DBHandler(){ ... }

    public void InsertTool(TTool tool) { ... }
    public TTool QueryTool(string toolID) { ... }
    public void UpdateTool(TTool tool) { ... }
    public void DeleteTool(string toolID) { ... }
}

, ( ) , , , .

< > :

var handler = new DBHandler<BallMill>();
BallMill value = handler.QueryTool("xyz");
value.SomeProperty = "New Value";
handler.UpdateTool(value);
+3

, , , . : " ". , , . "", ( ) .

, , , "" , , , . , DB. , . , (, - ), , , , .

+3

, ... . - , , (, #)

Composition implies that your Composite class creates classes where they are usually inherited. In this case, you provide a contract for implementation through interfaces.

To give you an example of how inheritance can be applied to your code, consider this: (this is not used composition)

public interface IHandler
{
    void InsertTool(ITool tool);
    void UpdateTool(ITool tool);
    void DeleteTool(string toolID);
    //void DeleteTool(ITool tool) - seems more consistent if possible!?

    ITool QueryTool(string toolID);
}

public interface ITool
{

}

class BallMill : ITool
{
}

class DiamondTool : ITool
{
}

class BallMillDBHandler : IHandler
{

    public BallMillDBHandler(){ ... }

    public void InsertTool(ITool tool) { ... }
    public BallMill QueryTool(string toolID) { ... }
    public void UpdateTool(ITool tool) { ... }
    public void DeleteTool(string toolID) { ... }
}

class DiamondToolDBHandler : IHandler
{

    public DiamondToolDBHandler(){ ... }

    public void InsertTool(ITool tool) { ... }
    public DiamondTool QueryTool(string toolID) { ... }
    public void UpdateTool(ITool tool) { ... }
    public void DeleteTool(string toolID) { ... }
}
0
source

Use generics instead of inheritance. You want to perform the same operation on different types of objects, which exactly matches the generics.

0
source

All Articles