-1

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, [...])
Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
Enrico
  • 3,592
  • 6
  • 45
  • 102

2 Answers2

1

You could try to do it this way:

Define your SyncResult as interface implementation

interface ISyncResultTable
{
}

public class SyncResultTable1 : ISyncResultTable
{
}

public class SyncResultTable2 : ISyncResultTable
{
}

etc..

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>
Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49
  • Thanks! Each function as different parameters. I took for granted it was clear my example before. mmm... but at the end I can use something like your suggestion... I put all details in T instead of having a lot of parameters... – Enrico Nov 24 '16 at 12:16
  • I'm using the same thing in the data project... Why have I started to think that? I don't know... – Enrico Nov 24 '16 at 12:19
0

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?

Enrico
  • 3,592
  • 6
  • 45
  • 102