dimanche 18 avril 2021

How to find a way to instantiate a class based on user selection of enum?

I have service interfaces on Business Logic Layer as following;

public interface IEnglishTurkishService
    {
        string Add(EnglishTurkish entity, string tableName);
        string Update(EnglishTurkish englishTurkish, string tableName);
        string Delete(EnglishTurkish englishTurkish, string tableName);
        List<EnglishTurkish> Search(EnglishTurkish englishTurkish, string tableName);
        List<EnglishTurkish> GetAll(string tableName, Func<EnglishTurkish, bool> filter = null);
    }

public interface ITurkishEnglishService
{
    string Add(TurkishEnglish entity, string tableName);
    string Update(TurkishEnglish englishTurkish, string tableName);
    string Delete(TurkishEnglish englishTurkish, string tableName);
    List<TurkishEnglish> Search(TurkishEnglish englishTurkish, string tableName);
    List<TurkishEnglish> GetAll(string tableName, Func<TurkishEnglish, bool> filter = null);
}

My manager classes implement the above interfaces as following;

public class EnglishTurkishManager : IEnglishTurkishService
public class TurkishEnglishManager : ITurkishEnglishService

In UI Layer, the user selects the source and destination language to determine language pair to perform a translation operation. I can only work with one type of class as it is tightly coupled in UI Layer as following;

IEntity word = TableInstanceFactory.GenerateTableInstance(CreateTableName());
word.Word = inputText.Text.ToLower();
word.Translation = translatedText.Text.ToLower();

EnglishTurkishManager englishTurkishManager = new EnglishTurkishManager(new SlEnglishTurkishDal());
string outputMessage = englishTurkishManager.Add(word, CreateTableName());
MessageBox.Show(outputMessage);

If the user selects 'English' as the source and 'Turkish' as the target language, then the above code works fine, 'CreateTableName' method takes combobox selected items and generates a table name from them. But if the user selects 'English' and 'Spanish' then it doesn't work, because in this way of implementation it only works with'EnglishTurkish' class object. How to refactor this code in such a way that it works based on user selection and instantiates Manager class based on selection?

Aucun commentaire:

Enregistrer un commentaire