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!

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

Yeahhhhh. This is exactly what I was trying to accomplish. You just helped me sooo much, thanks a mil!
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)) ?
I would say that this is quick fix.
Great trival DDD example (including Data Mapper, b/c inquiring minds want to know;) stackoverflow.com/questions/8844105/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.