I have a model class as following which implements an interface;
public class EngTrModel : IWordModel
{
public string Word { get; set; }
public string Translation { get; set; }
public string WordPair
{
get
{
return $"{Word}->{Translation}";
}
}
}
I have a processor class which is responsible for connecting to a Sqlite database and gets all rows as following;
public class SqliteDataAccess : IDataAccess
{
public static string LoadConnectionString(string id = "Default")
{
return ConfigurationManager.ConnectionStrings[id].ConnectionString;
}
public static List<EngTrModel> LoadDatabase()
{
using (IDbConnection conn = new SQLiteConnection(LoadConnectionString()))
{
var output = conn.Query<EngTrModel>("Select * from Words", new DynamicParameters());
return output.ToList();
}
}
}
In my WPF form code, when I click the 'Load Records' button it works fine and loads the table rows into a listbox as following;
private void loadFromDatabase_Click(object sender, RoutedEventArgs e)
{
LoadFromDatabase(SqliteDataAccess.LoadDatabase());
}
private void LoadFromDatabase(List<EngTrModel> words)
{
WordsListBox.ItemsSource = null;
WordsListBox.ItemsSource = words;
WordsListBox.DisplayMemberPath = "WordPair";
}
In the future when I expand my application to support other languages also, for example when I need to add EngFrModel which implements IWordModel interface into my application, I want to be able to use 'LoadFromDatabase' method without changing anything(Open Closed Principle). I've made a few attempts but none of them working; For example, I want to use following method not with hard-coded values and give this method the functionality to work with different kind of models and database tables;
public static List<IWordModel> LoadDatabase(string table)
{
using (IDbConnection conn = new SQLiteConnection(LoadConnectionString()))
{
var output = conn.Query<IWordModel>($"Select * from {table}", new DynamicParameters());
return output.ToList();
}
}
Then I made following changes;
private void loadFromDatabase_Click(object sender, RoutedEventArgs e)
{
LoadFromDatabase(SqliteDataAccess.LoadDatabase("Words"));
}
private void LoadFromDatabase(List<IWordModel> words)
{
WordsListBox.ItemsSource = null;
//WordsListBox.ItemsSource = DataAccess.LoadDatabase();
WordsListBox.ItemsSource = words;
WordsListBox.DisplayMemberPath = "WordPair";
}
The code can be compiled without error but it gives System.ArgumentException: 'Invalid type owner for DynamicMethod.' error in the following line of code;
var output = conn.Query<IWordModel>($"Select * from {table}", new DynamicParameters());