this is my setup
first, the base classes
public class TableBase
{
}
public class TableRepositoryBase
{
private BindingSource _bindingSourceOnForm;
public TableRepositoryBase(BindingSource bindingSource, string connectionString)
{
BindingSourceOnForm = bindingSource;
ConnectionString = connectionString;
SetupParameters();
}
protected string ConnectionString { get; set; }
protected Type TableType { get; set; }
protected BindingSource BindingSourceOnForm
{
get { return _bindingSourceOnForm; }
set { _bindingSourceOnForm = value;}
protected string SQLSelect { get; set; }
protected virtual void SetupParameters()
{ }
public virtual void FetchAll()
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
BindingSourceOnForm.DataSource = connection.Query<TableType>(SQLSelect).ToList();
}
}
}
now my inherited classes
public class TableUser : TableBase
{
public int UserID { get; set; }
public string UserFirst { get; set; }
public string UserLastname { get; set; }
}
public class TableRepositoryUser : TableRepositoryBase
{
public TableRepositoryUser(BindingSource bindingSource, string connectionString) : base(bindingSource, connectionString)
{ }
protected override void SetupParameters()
{
base.SetupParameters();
TableType = typeof(TableUser);
SQLSelect = "select UserID, UserFirst, UserLastname from tblUser";
}
}
Now the problem
In the base class TableRepositoryBase
the following line of code in the method FetchAll()
will not compile:
BindingSourceOnForm.DataSource = connection.Query<TableType>(SQLSelect).ToList();
The error is
Error CS0246 The type or namespace name 'TableType' could not be found (are you missing a using directive or an assembly reference?)
This will be because the query<>
is looking for a type, and although TableType
is declared as a type, it will probably see it as a variable here.
What I want to achieve is that the method FetchAll
can be done entire by the baseclass.
But, I would like that the list that is put in BindingSourceOnForm.DataSource
is not of type DapperRow
but in this case of type TableUser
.
I could override this method in TableRepositoryUser
like this
public override void FetchAll()
{
//base.FetchAll();
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
BindingSourceOnForm.DataSource = connection.Query<TableUser>(SQLSelect).ToList();
}
}
That will work, now the list assigned to BindingSourceOnForm.DataSource
if of type TableUser
But I would like to do this code in the base class, not in the inherited class.
How could I achieve this ?
EDIT
I am trying to adapt the answers from the link I got in the comments from @Flydog57 but with no success.
I have this so far
MethodInfo method = typeof(SqlConnection).GetMethod("Query");
MethodInfo generic = method.MakeGenericMethod(TableType);
BindingSourceOnForm.DataSource = generic.(SQLSelect).ToList();
I get a compile error Identifier expected
I guess this code makes no sense at all, but I dont understand how to use MakeGenericMethod
to solve my problem, if it is even possible.
So some help here would be appreciated
EDIT 2
Since I can't find a solution, I am now using this inefficient method
public virtual void FetchAll()
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
if (TableType == typeof(TableUser))
BindingSourceOnForm.DataSource = connection.Query<TableUser>(SQLSelect).ToList();
else if (TableType == typeof(TableTask))
BindingSourceOnForm.DataSource = connection.Query<TableTask>(SQLSelect).ToList();
}
}
Still hoping for a better way because this means I have to come back here and alter the code everytime a new repository class is inherited from RepositoryBase