Assigning default values to properties

Assigning default values to properties

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


mayurimalgave posted on Friday, May 02, 2014

Hello..

I have created a Silverlight application with CSLA. I want to assign default values to some of the BusinessBase class's properties, which are not bound to the UserControls.

eg: 1] SysDate - The current datetime should be assigned to it.

2] UserId - The current user's id should be assigned to it.

These fields are not bound to UI.

So, where should I assign these values in BusinessBase class?

Or what is the best practice for handling such type of issue?

 

tiago replied on Saturday, May 03, 2014

Hi,

 

For CSLA 4.5 this is how I do it:

 

/// <summary>
/// Loads default values for the <see cref="Doc"/> object properties.
/// </summary>
[Csla.RunLocal]
protected override void DataPortal_Create()
{
   LoadProperty(DocDateProperty, new SmartDate(DateTime.Today));
   base.DataPortal_Create();
}

mayurimalgave replied on Monday, May 05, 2014

Thanks..I tried your suggestion & it worked..

JonnyBee replied on Sunday, May 04, 2014

Why do you need them in the BO?

If you only use it for tracking of last changed/changed by in a database you do not need these properties in your BO.

Use Csla.ApplicationContext.User.Name to get the userid and DateTime,.Now() to get the timestamp in your DataPortal_XYZ methods.

mayurimalgave replied on Monday, May 05, 2014

Thanks for reply..

There are some dependent properties which need to be validated on UserId & SysDate.

So I am trying to initialize in BO.

If any other way, please suggest.

JonnyBee replied on Monday, May 05, 2014

In this case you must initialize the values in DataPortal_Create and DataPortal_Fetch.

Copyright (c) Marimer LLC