Ignoring individual properties when determining whether an object is savable

Ignoring individual properties when determining whether an object is savable

Old forum URL: forums.lhotka.net/forums/t/9249.aspx


chris.stafford posted on Wednesday, July 21, 2010

Hello, all.  I am constructing business objects for a project at work, and I have run into a bit of a snag.  I need to display an Employee's Name on a data grid, and the base object only contains the Id, which then joins to the Employee Table.  That's easy to handle; I simply use the getter to load it.  Like so:

 public static PropertyInfo<String> EmployeeNameProperty = RegisterProperty<String>(c => c.EmployeeName);

public String EmployeeName

{

get

{

#if SILVERLIGHT

Employee.GetEmployee((o,e) =>

{

if (e.Object != null)

{

SetProperty(EmployeeNameProperty, e.Object.LastName +

", " + e.Object.FirstName);}

},EmployeeId);

#endif

}

return GetProperty(EmployeeNameProperty);}


set { SetProperty(EmployeeNameProperty, value); }

 

This code works perfectly for retrieving the Employee's Name from the database.  Unfortunately, though, when the property gets called on the result handler, the collection is marked as savable.

Is there any way to prevent this property from triggering the save?  I've tried the following options, with no luck:

1.  Marking the property with [NonSerializable]

2.  Wrapping the SetProperty method with using(BypassPropretyChecks)

Anything else I can try?

 

Marjon1 replied on Wednesday, July 21, 2010

Instead of using SetProperty, use LoadProperty which will not mark the field as dirty.

BypassPropertyChecks only removes the calls to check authorization rules and doesn't call any validation rules, it has no affect on the dirty state as you've discovered.

Marking the property as NonSerializable would just mean that it is not sent across to the server and back, essentially resetting it every time it was sent to the sever.

I don't know what your objects are actually being used for, but with the code that you have there if there are 1,000 records in the grid then you will be hitting the database 1,000 times (possibly creating that many connections as well). If possible to get the information at the time of fetching the list would be much more efficient.

 

rsbaker0 replied on Thursday, July 22, 2010

Marjon is right on all counts. There are ways to avoid marking the collection savable, but the right way to do this is to left join the table containing the ID with the one containing the name and retrieve the collection in a single round-trip to the server.

We found out the hard way that round-trips for per row property retrieval will absolutely ruin performance.  It's one thing in a test environment with low network and database response latency, but if your latency gets up to, say 200 ms total for example, you'll make the user wait 20 seconds to see just 100 rows, and minutes to see 1000.

chris.stafford replied on Thursday, July 22, 2010

How would I go about joining the objects so that they all load at once?  I am still new to CSLA, so while I can handle single objects pretty well, associations of objects and collections are something I have no working knowledge of.

If it helps, here's the LINQ statement for my fetch selection:

 

retval = (

from item in context.ProjectRosterEmployees

join projects in context.Projects

on item.ProjectId

join employees in context.Employees

on item.EmployeeId

equals employees.Id

where projects.Id == ProjectId

orderby employees.LastName, employees.FirstName

select item).ToList();

 

As you can see, the join is already there.  I wanted to make sure the Roster list came back ordered properly.  I'm less clear on how to make sure the Employee object is attached to the ProjectRosterEmployee object, though.  Any help you guys can offer to help me improve the effeciency of this code would be very much appreciated.

 

RockfordLhotka replied on Thursday, July 22, 2010

If you haven't, you really should read Expert 2008 Business Objects chapters 1-5 and 17-18 - those chapters explain exactly what you are asking, and a lot more important material as well.

Another source of (more up to date) information is the Core 3.8 video series, which also covers the concepts you are asking about.

Copyright (c) Marimer LLC