Search code examples
c#base-class

C# Base Class forces to create a function in a dependent class but different parameters


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, [...])

Solution

  • Ok, I worked out my solution. I don't know if it's the best solution in the world but I like it and accept suggestion.

    First of all I defined my base class with all common function and the implementation of INotifyPropertyChanged

    Base class with T and INotifyPropertyChanged

    public abstract class BaseSyncClass<T> : INotifyPropertyChanged where T : SyncResultBase {
        private readonly ISyncData _syncData;
    
        public BaseSyncClass(ISyncData syncData) {
            _syncData = syncData;
        }
    
        public abstract string UpdateRecordFromTable(T sync);
        public abstract string UpdateRecordFromTableNoSyncId(List<T> sync);
    
        public void UpdateFromServer(List<T> result) {
            foreach (T sync in result) {
                UpdateRecordFromTable(sync);
            }
        }
    }
    

    Then when I create a new class base on the above, I can have all method to implement

    Example of implementation

    public class ListingConditionTableSync : BaseSyncClass<SyncResultCondition> {
        public ListingConditionTableSync(ISyncData syncData) : base(syncData) {
        }
    
        public override string UpdateRecordFromTable(SyncResultCondition sync) {
            throw new NotImplementedException();
        }
    
        public override string UpdateRecordFromTableNoSyncId(List<SyncResultCondition> sync) {
            throw new NotImplementedException();
        }
    }
    

    What do you think? Is it an elegant and useful solution?