Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best c# generics class for replacing DataTable as collection?

I'm trying to bring a legacy C# .NET 1.1 application into the modern era. We use DataTables for our collections of what could have been business objects.

Given that most of the code thinks it is talking to the interface of a DataRow, what generic collection would make for the least painful transition?

like image 750
MatthewMartin Avatar asked Dec 08 '22 08:12

MatthewMartin


1 Answers

if im reading your question rightly, you are asking for which container will just store a list of your Business objects and then allow you to just enumerate through the collection, or select via an index.

well I would consider looking into the List<>

where you methods would accept either IList<> (to access the index) or IEnumerable<> (to use a foreach loop on the collection)

for example

private void PrintAll<T>(IEnumerable<T> items)
{
    foreach(T item in items)
        Console.WriteLine(item.ToString());
}

now i can pass in any container which uses the IEnumerable<> interface, including List<> and the normal array

example

List<Person> people = new List<Person>();
//add some people to the list
PrintAll<Person>(people);

a sample n-tier app with Buiness objects

HTH

bones

like image 190
dbones Avatar answered Dec 09 '22 21:12

dbones