Salutations. I'm using a generic repository to facilitate access to two distinct, and necessary datastores; a local SQLite DB and a remote REST WS (in C# for a mobile MonoDroid app).
interface IRepository<T>
{
List<T> Read(ICond cond);
}
class RepositorySQL<T>: IRepository where T : new()
{
List<T> Read(ICond cond)
{
sqlArgs = convertCond(cond); // convert cond to sql arguments
List<object[]> results = dbLayer.DBSelect(sqlARgs); // sqlite SELECT wrapper
List<T> returnList = new List<T>();
for(int i=0; i < results.Count; i++)
{
object[] oa = results[i];
T t = new T();
// cast/convert oa to T
///////////////////////////
t = (T)oa; // override cast operator ???
t = oa; // override = operator ???
t.Convert(oa); // use a method/delegate somehow ???
returnList.Add(t);
}
}
My problem is that I need to convert or cast each object[]
to T
before adding it to the return List<T>
, but am not sure what the best approach is. My DB Layer method DBSelect
builds and returns a List<object[]>
since that was the most direct and generic way (I thought) to obtain the query result from the SqliteDataReader
method GetValue(i)
(each object[]
is a row, each column a object
. I could also stored rows into a string[]
by calling GetString(i)
instead, but will still have the casting/converting issue).
I really want to use the generic repository here, even though a statically typed one would be a quick fix. I also am sticking with two repositories (I've read many posts about repositories with answers pointing out why two shouldn't be used b/c of data dup concerns, etc... ). Other than that, I'm open to any and all suggestions. Thanks for the help!