Enrico Enrico - 1 year ago 81
C# Question

C# Baseclass foces to create a function in a dependent class but different params

maybe it's a stupid question but I'm creating a long sync process for an app and I don't want to forget something. For each table I have to follow the same procedure but different parameters.

It's a long and boring work. For this reason I should have a constraint in the base class that it forces me to implement some functions with different parameters.

For example

bool DeleteRecordFromTable(SyncResultTable1 sync, bool ExecuteScript = true)
bool InsertRecordFromTable(SyncResultTable1 sync)
bool UpdateRecordFromTable(SyncResultTable1 sync, string text)
bool DeleteRecordFromTable(SyncResultTable2 sync, int value1)
bool DeleteRecordFromTable(SyncResultTable(n) sync, bool IsDelete, int value1)
bool InsertRecordFromTable(SyncResultTable(n) sync, DateTime dtExecute)
bool UpdateRecordFromTable(SyncResultTable(n) sync, [...])

Answer Source

You could try to do it this way:

Define your SyncResult as interface implementation

interface ISyncResultTable

public class SyncResultTable1 : ISyncResultTable

public class SyncResultTable2 : ISyncResultTable


interface ITable<T> where T:ISyncResultTable
    bool DeleteRecordFromTable(T sync, bool ExecuteScript)
    bool InsertRecordFromTable(T sync, bool ExecuteScript)
    bool UpdateRecordFromTable(T sync, bool ExecuteScript)

than you could write your classes this way:

public class Table1 : ITable<SyncResultTable1>