views:

115

answers:

1

I used myGeneration in some projects; I noticed that BusinessEntity keeps the result of the query in a DataTable, But what if the developer wants to get the result as List of objects? To solve this, I made the following modification for my own use.

public  class BusinessEntity

{
    //....
    public DataTable TheDataTable
    {
        get
        {
            return _dataTable;
        }
        set
        {
            _dataTable = value;
        }
    }

    public void PutTheRow(DataRow pRow)
    {

        DataRow newRow = _dataTable.NewRow();
        newRow.ItemArray = pRow.ItemArray;
        _dataTable.Rows.Add(newRow);
        _dataRow = newRow;
        //Rewind();
        _dataTable.AcceptChanges();

    }

    .......


}

Now suppose we have a table "Employee" and we generate a class for him, also I make the following modification:

public abstract class Employee : _Employee
{
    public Employee()
    {

    }
    public List<Employee> AsList()
    {

        List<Employee> list = new List<Employee>();
        Employee businessEntity = null;
        foreach (System.Data.DataRow row in TheDataTable.Rows)
        {
            businessEntity = new Employee();
            businessEntity.TheDataTable = TheDataTable.Clone();
            businessEntity.PutTheRow(row);
            list.Add(businessEntity);


        }
        return list;
    }
}

Now I can Get a list of objects, from the result of the query, instead of one object and the result in the data table :

Employee employee = new Employee();
    employee.Where.ID.Operator = WhereParameter.Operand.NotIn;
    employee.Where.ID.Value = "1,2,3";
    employee.Query.Load();
    foreach (Employee employeeLoop in employee.AsList())
    {
        TreeNode node = new TreeNode(employeeLoop.s_ID);
        node.Tag = employeeLoop;
        mMainTree.Nodes.Add(node);
}

Then I can access the selected Employee as following:

Employee emp = (Employee) mMainTree.SelectedNode.Tag;
emp.Name = "WWWWW";
emp.Save();

thanks. Do You have a better tips, Ideas? for more discussion, please visit MyGeneration forum.

A: 

Hello,

Why would you create a method called AsList() that only ever returns one Item? You could just create a generic extension method like this (created from the top of my head..):

public static List AsList(this T item) { return new List() { item }; }

There is no point to loop through the Employee List of one to add it to a TreeNode.

Thanks -Blake Niemyjski (Author of the CodeSmith CSLA Templates)

Blake Niemyjski
thanks for you.AsList() returns list of objects, not one item.the idea which I want to say is that the modification which I made converts rows from DataTable into List of objects.
houssam11350