FPGA
FPGA

Reputation: 3855

Creating a simple readonly structure similar to DataTable

I am trying to create a simple readonly structure similar to datatable structure , i will be using it to generate sql commands and i am looking for maximum performance the following is what i came up with, the idea is to generate Rows[] from an IList<T>.

class Column
    {
        public string Name { get; private set; }
        public string TableName { get; private set; }
        public bool IsPrimaryKey { get; private set; }
        public SqlDbType SqlType { get; private set; }
        public string SqlTypeString { get { return SqlType.ToString(); } }
        //etc..
        public Column(PropertyInfo propertyInfo){//init everything}
    }



class Cell
    {
        public Column Column { get; private set; }
        public object Value { get; private set; }

        public Cell(object Value,Column Column)
        {
            this.Column = Column;
            this.Value = Value;
        }
    }

class Row
    {
        public object[] Cells { get; private set; }
        public Row(Cell[] Cells)
        {
            this.Cells = new Cell[Cells.Count()];
            for (int i = 0; i < Cells.Count(); i++)
            {
                this.Cells[i] = Cells[i]; 
            }
        }
    }

each cell contains a reference to its column object which is i am not sure if its the right thing, i was wondering if dropping the column reference from the cell and using the object order to get the column reference is a better option, in other words i want to micro optimize it since its a simple structure.

Upvotes: 1

Views: 81

Answers (1)

StriplingWarrior
StriplingWarrior

Reputation: 156524

i want to micro optimize it since its a simple structure.

That's not a valid reason to micro-optimize this structure.

Your class structure, as it stands, isn't even correct yet. First focus on correctness, then you can work on optimization.

For example, by returning the same object[] that you use internally, this is not the "readonly structure" you're claiming it to be. Someone can modify the elements of this array and mess things up royally.

With what you've said you're trying to do with this framework (analyzing expression trees to produce SQL queries, etc.), chances are that any optimization you do at this level wouldn't even be noticeable. But if you do want to begin optimizing, the right place to begin is by removing the calls to .Count() on your arrays. Use .Length instead.

But, as I mentioned in the comments on your question, I'd strongly suggest trying out existing frameworks first. Smarter people than you and I have put a lot of time and effort into them, and they're likely to do a better job than we can. :-)

Upvotes: 3

Related Questions