Tuesday, 15 July 2014

c# - Best practice Objects from SQL -



c# - Best practice Objects from SQL -

i tried searching couldn't find proper answer. creating application contains lot of different objects. info these objects saved in mssql database. best way info out?

for simplicity utilize 2 objects here: itemobject userobject

both of them has constructor info database:

public itemobject(int id) //same 1 userobject { //code info database particular item }

itemobject has property called createdby userobject.

now question best way create itemobject? have 2 possible solutions:

solution #1:

public itemobject(int id) { datatable dt = dal.getdatatable("select top 1 * items id = @id") this.createdby = new userobject((int)dt.rows[0]["userid"]) }

solution #2

public itemobject(int id) { datatable dt = dal.getdatatable("select top 1 * items inner bring together users on items.createdby = users.id items.id = @id") this.createdby = new userobject((int)dt.rows[0]["userid"], dt.rows[0]["username"].tostring()) } public userobject(int id, string username) { this.id = id; this.username = username; }

in solution #1 inquire info twice in solution #2 inquire info once. although solution #1 much "cleaner" , easier read.

edited after steves correction.

i go solution two. point of view solution 1 not acceptable, though "cleaner". , think there no best practice reading objects. much entity framework purpose.

c# sql

No comments:

Post a Comment