0

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!

samus
  • 6,102
  • 6
  • 31
  • 69

1 Answers1

1

You need mapping in any way. So its just a matter of code organization whete this code will reside, in strong typed repository class , or Entity class.

class foo : IDataReader publit void Construct(Object[] row)

Then your repository would be IRepository where T: IDataReader

Alex Burtsev
  • 12,418
  • 8
  • 60
  • 87
  • Yeahhhhh. This is exactly what I was trying to accomplish. You just helped me sooo much, thanks a mil! – samus Jul 27 '12 at 15:45
  • Alex: So this is an implementation of the Data Mapper pattern (or is there more to it, and this just a quick fix (which I'm doubting, seems solid)) ? – samus Jul 27 '12 at 15:48
  • I would say that this is quick fix. – Alex Burtsev Jul 27 '12 at 16:23
  • Great trival DDD example (including Data Mapper, b/c inquiring minds want to know;) http://stackoverflow.com/questions/8844105/repository-and-data-mapper-pattern – samus Jul 27 '12 at 17:38