c# - What is the best practise for handling complex objects as class members? -


okay, basic example have application expenses , each expense in sql database has id, employee id , amount. should this:

public class expense {     public int id { get; set; }     public employee employee { get; set; }     public decimal amount { get; set; } } 

or this:

public class expense {     public int id { get; set; }     public int employeeid { get; set; }     public decimal amount { get; set; } } 

you see first , when fetch expense database set employee field calling constructor like: employee = new employee(int id) make trip db complete members in employee object. handy because can access employees members/functions via expense.. i.e. if bound objectdatasource display eval("employee.name") , show more friendly number stored in db.

however current project i'm working on may run 10's of thousands of rows , number of db requests going sky rocket if i'm fetching first expense details , employee details each object. (and in fact current project has table 6-7 foreign keys).

is there way of having cake , eating too?

maybe having interface class id fields , not full object fields? feel i've never understood interfaces i'm not sure if make difference.

thanks has read far if don't have answer me.

it depends: if using feature-rich orm (nhibernate) can handle things relations cleanly 1st code more convenient; or if these view model objects can delegate task di engine (like ninject).

if design of app demands more control on how objects come , go or if use expense heavily (not employee) should use 2nd design (of course i.e. nhibernate has sophisticated caching toolbox can employed implement this).


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -