Tuesday, 15 January 2013

c# - How to make grid operations work with custom property in EF? -



c# - How to make grid operations work with custom property in EF? -

in asp.net mvc 5 web site have gridview using devexpress component binding using linq method.

ef generated partial class map table utilize display in gridview. in partial class generated ef have id_status property wich has corresponding description in other table. made partial class deal custom property , works ok, except when seek create 'sort' operation clicking on header of column.

the partial class generated ef.

[table("test")] public partial class test { [key] public long id_test { get; set; } public long id_teststatus { get; set; } //other properties }

my custom partial class:

public partial class test { private static readonly testrepository _testrepository; static testrepository() { _testrepository= new testrepository(); } public string statusdescription { { homecoming _testrepository.getstatusdescriptionbyid(id_teststatus); } } }

when seek sort using column works fine, when seek sort using custom property column grid cell values gets empty, without value.

any suggestion?

it's not thought have info access code within entity. 1 reason makes hard write unit test. reason is give rising n + 1 anti pattern. in case, does: 1 (1) query tests, each test (n) sends separate query database statusdescription.

the way implemented raises eyebrows, because

_testrepository static, meas there probable context instance living entire lifecycle of application - unless getstatusdescriptionbyid creates new context each call, wouldn't thought either. the getstatusdescriptionbyid phone call made each time property accessed. in web application may not big problem because objects newly created each time requested anyway, in other environments highly inefficient.

a improve approach fetch testss status included:

context.tests.include(t => t.teststatus)

and have unmapped property like

public string statusdescription { { homecoming teststatus== null ? string.empty : teststatus.description; } }

better still (in opinion) not show test objects directly, testdto objects

public class testdto { public string statusdescription { get; set; } //other properties match test's properties }

and utilize tool automapper map collection of tests testdtos. if test has property status , teststatus has property description, automapper able flatten statusdescription automatically.

both statusdescription property , dto appraoch set state of test(dto) object once. don't think grid component can mess that.

c# asp.net-mvc entity-framework gridview

No comments:

Post a Comment