Thursday, August 7, 2008

Design

Something that I just realized that's going to be important. I need to be able to get result sets by a Property. For example: user.BySex("male");

With the way I've been going I'm not sure how this will fit into the model, but it's a feature that must exist.

Maybe something like Factory<user>("Sex = ?1", "male");
I guess that works, maybe I'm thinking there is something missing where there isn't.

Tuesday, August 5, 2008

Generics Issues

I've never really used Generics before so I'm still struggling with it.
What I think I want to do, is have a method that returns List;. I can return the list ok, and I can use Activator to create new T objects. The only problem is that my test requires some boxing, which is what I want to avoid. I'm wondering if the boxing will disappear in a real world example.

test code



using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace GenericsTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Test 1");
Factory fact = new Factory();
List<obint>; test1 = fact.GetList<obint>();
foreach (ObInt item in test1)
{
Console.WriteLine(item.Data);
}

Console.WriteLine("\nTest Two");
List<int> test2 = new List<int>();
test2.Add(1);
test2.Add(2);
test2.Add(3);
foreach (int item in test2)
{
Console.WriteLine(item);
}

Console.WriteLine("Done, hit anykey");
Console.Read();

}
}

//The Goal is to see if I can return a Generic list
public class Factory
{
public List<t> GetList<t>() where T : IObject
{
List<t> list = new List<t>();

T tmp = Activator.CreateInstance<t>();
tmp.Add(5);

list.Add(tmp);

return list;
}
}

public interface IObject
{
void Add(object item);
}

public class ObInt : IObject
{
protected int _data;
public int Data
{
get { return _data; }
set { _data = value; }
}

public ObInt(int data)
{
_data = data;
}
public ObInt() { }

public void Add(object item)
{
_data = (int)item;
}

}

public class ObString: IObject
{
protected string _data;
public string Data
{
get { return _data; }
set { _data = value; }
}

public ObString(string data)
{
_data = data;
}
public ObString() { }

public void Add(object item)
{
_data = item.ToString();
}
}
}

Monday, August 4, 2008

Problem, Expected

I ran into a problem when trying to get the object to return a list. This has given me time to kinda rethink some of the choices I've made.
I've never liked the internal object, but didn't know of a way I would like better. I'm wondering if it would be better to have Methods to get the internal information instead of Properties. So I could call something like self.Column("Name"); and it would return a column name that was associated with the Propriety Name. Table could still be a Property, I see no reason why it shouldn't be.
While looking at other implementations of database object I've seen that they normally have a kind of master DB object. So you can do things like users = db.Select( "SELECT * FROM user"); (I'm looking towards Google's app engine here.) I don't have any core object in my design. I'm not sure if that is a good thing or not.
A problem I ran into so having to have an object already to call it's select. A Factory object would help with this. Something like DBFactory.Select( User, "Name = ?1", "Chris" ) would be nice. As long as I'm thinking about this route, List myusers = DBFactory.Select( typeof(User), "Age >= ?1 AND employed = ?2", 21, true ); Might be Idea. This is inspired by google's model. The only down side is that I can't build in custom filters on the list. So Instead of getting one list of all users over 21 and being able to get just the employed and then just the unemployed without ever making another DB call. The user has to either do the filter themselves, or (more likely) make the DB call twice (eww).
On the otherhand, if the DBFactory was global, it could keep a cache of the results it's returned and do the filter itself?