IsSavable is set to true when object is loaded how do I set it to false?

IsSavable is set to true when object is loaded how do I set it to false?

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


Harry posted on Sunday, February 05, 2012

Hi,

I'm using CSLA 4.1 and also MVVMLight. I have loaded a list of DynamicListBase objects.  When each object is created from my datasource the IsSavable property is set to true before I have edited the object. Once the object is saved is it set to false, but how do it set it to false during the inital load of data. I want to use it IsSavable property in a CanSave routine of the relayCommand.

I creat the list with in the ViewModel:

Users = U

 

 

In Library.Net the Userlist code is:

serList.GetUserList();

 

 

 

 

return DataPortal.Fetch<UserList>();}

 

 

private void DataPortal_Fetch()

{

 

 

 

using (var dalManager = DataAccess.DalFactory.GetManager()) var dal = dalManager.GetProvider<DataAccess.IUserDal>();

 

 

var data = dal.Fetch();

 

 

var rlce = RaiseListChangedEvents;

RaiseListChangedEvents = 

 

 

 
 
false;

 

 

foreach (var i in data) var u = DataPortal.Fetch<UserInfo>();

{

 

 

 

 

 

 

 

{

u.UserId = i.UserId;

u.UserName = i.UserName;

u.Email = i.Email;

u.UserLanguage = i.UserLanguage;

u.UserGroup = i.UserGroup;

u.LoggedOnFlag = i.LoggedOnFlag;

u.LoggedOnProduct = i.LoggedOnProduct;

u.LoggedOnFunction = i.LoggedOnFunction;

u.LastUpdate = i.LastUpdate;

Add(u);

}

RaiseListChangedEvents = rlce;

}

}

public static UserList GetUserList()

{

 

 

 

 

TIA.

Marjon1 replied on Sunday, February 05, 2012

Because you are using the properties directly to assign the values, the object is being marked as dirty.

You either need to wrap those statements around a U.BypassPropertyChecks() call or call MarkAsOld directly on the User object before adding it to list. This obviously is depends on what access you have to the object though. 

This will also mean that no validation rules are run on your object, you would need to call this manually if required.

Harry replied on Sunday, February 05, 2012

Marjon1 thanks for the reply. You say "because you are using the properties directly to assign the values" is there another way?

I have added a procedure to my object that copies the data from the datasource and creates the object then calls MarkOld() which seems to work OK but if there is a "better" way to do this I would like to know.

JonnyBee replied on Monday, February 06, 2012

I highly recommend Rockys Using CSLA 4 ebook series. Thes go into detail on this.You can also look at the ProjectTracker sample.

The recommended way is to delegate property set/load into the item class and use BypassPropertyChecks.

When you set your objects properties like that you will

And you do not want to do this in your DAL. DAL should use BypassPropertyChecks or LoadProperty to just load values into properties.

The code in you child item should be like this:

    private void Child_Fetch(ProjectTracker.Dal.AssignmentDto data)
    {
      using (BypassPropertyChecks)
      {
        ResourceId = data.ResourceId;
        Role = data.RoleId;
        LoadProperty(AssignedProperty, data.Assigned);
        TimeStamp = data.LastChanged;
      }
    }

Harry replied on Monday, February 06, 2012

Thanks Jonney,

I have the books and have read them, there is a lot of information there and a newbie can miss the important bits.  I'm sure its the fault of the reader not the writer Smile.

Thanks for the pointers I'll redo my code.  I understand the use of ByPassPropertyChecks better now.

Copyright (c) Marimer LLC