开发者

need a more efficient way of working with object data source in .net

I am writting a website using .net in some of my pages I need to use datagrid for showing some datas and for binding my data to datagrid I use object datasource something like below :

//this is 
public class userData
{
    private long userid = 0;
    private string username = null;
    private long amounts = 0;
    private DateTime registerdate;

    public long Userid
    {
        get { return userid; }
        set { userid = value; }
    }

    public string Username
    {
        get { return username; }
        set { username = value; }
    }

    public long Amounts
    {
        get { return amounts; }
        set { amounts = value; }

    }

    public DateTime Registerdate
    {
        get { return registerdate; }
        set
        { registerdate = value;}

    }

    }

//my businesshelper class

public class BusinessHelper
{
    public BusinessHelper()
    {
        //
        // TODO: Add constructor logic here
        //
    }


    public ICollection GetusersAmount()
    {
        ArrayList list = new ArrayList();
        DataSet1TableAdapters.userTableAdapter usert = new DataSet1TableAdapters.userTableAdapter();
        DataSet1.userDataTable ud = usert.GetData();


        foreach(DataSet1.userRow R in ud)
        {
            userData user = new userData();
            user.Amounts = R.Amount;
            user.Registerdate = R.registerdate;
            user.Userid = R.userID;
            user.Username = R.username ;
            list.Add(user);

        }
        return list;
    }
}

and then in my apsx fi开发者_如何学编程le I put an objectdatasource and bind the select statement to the GetUsersAmount() and then bind obejctdatasource to my datagrid

but I think the way I've chosen is not efficient I just want to hear about your way of doing such these things.

regards.


You can start by using auto properties:

public class userData
{
  //  private long userid = 0;

    public long Userid    { get; set;   }

    ....
}

And, assuming you want to keep the TableAdapter:

// untested
DataSet1.userDataTable ud = usert.GetData();

var list = from r in ud.Rows.AsEnumerable()
           select new userData() { Userid = r.userID, Username = r.username }

return list; // maybe list.ToList()

Aa an aside, I would suggest taking a good look at your naming conventions, those lower case class names confused me quite a bit.


I don't think that you are doing anything wrong (at the most, as suggested by other poster, you can use DataReader instead of data set). The class userData is typically called DTO (data transfer object) or entity class. You have other choices of DTO as well as - for example, you can use typed data tables or generic data sets. Custom classes are the most flexible choice but of course needs most efforts - because you have to hand-code them and write logic to fill/save them. There are frameworks (OR Mapper) to reduce these mundane efforts and generate data access code automatically. Example of such tools would be Entity Framework (ADO.NET Entity Model), Linq To Sql, NHibernate etc.


I wouldn't use a dataset to populate your list of business objects, but would use a SqlDataReader or something along those lines. Datasets have much more overhead than DataReaders.

I also would use a Collection (or something based on IEnumerable) to return the list of business objects back. There is probably a cost involved in casting to and from an ArrayList.

You may also want to implement some paging support into your method, unless your datagrid displays every record at once.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜