Save Command in Silverlight

Save Command in Silverlight

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


hanspret posted on Tuesday, April 09, 2013

I've got a situation where I need to save an object but there is other interactions with the database that need to happen with the save process. If one of the transactions fail all the transactions need to rollback.

To satisfy this requirement I have created a command object using CommandBase. The Save Command work as expected but in Silverlight I want to return the Root object so that the  Model can be in a state of "Saved" or not Dirty.

The problem is that when I assign the Model with the saved object I get the error "Edit level mismatch".

The command code is as follows:

[Serializable()]

public class SaveAssetTypeCommand : CommandBase<SaveAssetTypeCommand>

{

public SaveAssetTypeCommand()

{ }

public static PropertyInfo<CseAssetTypeRoot> AssetTypeObjProperty = RegisterProperty<CseAssetTypeRoot>(c => c.AssetTypeObj);

public CseAssetTypeRoot AssetTypeObj

{

get { return ReadProperty(AssetTypeObjProperty); }

set { LoadProperty(AssetTypeObjProperty, value); }

}

public static PropertyInfo<int> ResultDeletedProperty = RegisterProperty<int>(c => c.ResultDeleted);

public int ResultDeleted

{

get { return ReadProperty(ResultDeletedProperty); }

set { LoadProperty(ResultDeletedProperty, value); }

}

public static PropertyInfo<int> ResultInsertedProperty = RegisterProperty<int>(c => c.ResultInserted);

public int ResultInserted

{

get { return ReadProperty(ResultInsertedProperty); }

set { LoadProperty(ResultInsertedProperty, value); }

}

public static void Execute(CseAssetTypeRoot oAssetType, EventHandler<DataPortalResult<SaveAssetTypeCommand>> callback)

{

var dp = new DataPortal<SaveAssetTypeCommand>();

dp.ExecuteCompleted += callback;

dp.BeginExecute(new SaveAssetTypeCommand { AssetTypeObj = oAssetType });

}

#if !SILVERLIGHT

public static SaveAssetTypeCommand Execute(CseAssetTypeRoot oAssetType)

{

return DataPortal.Execute<SaveAssetTypeCommand>(new SaveAssetTypeCommand { AssetTypeObj = oAssetType });

}

[Transactional(TransactionalTypes.TransactionScope)]

protected override void DataPortal_Execute()

{

//First do the deletion

var DeletedList = AssetTypeObj.CseAssetTypeSpecLinks.GetDeletedItems();

foreach (int item in DeletedList)

{

using (var dalManager = DAL.Interface.DALFactory.GetManager())

{

var tableAccessor = dalManager.GetDataAccessor<DAL.Interface.IAscSpecValueDAL>();

ResultDeleted = tableAccessor.BulkDelete(item, AssetTypeObj.TypeCode);

 

}

}

//Now do Insertion

foreach (var item in AssetTypeObj.CseAssetTypeSpecLinks)

{

if (item.IsNew)

{

using (var dalManager = DAL.Interface.DALFactory.GetManager())

{

var tableAccessor = dalManager.GetDataAccessor<DAL.Interface.IAscSpecValueDAL>();

ResultInserted = tableAccessor.BulkInsert(item.SpecID, AssetTypeObj.TypeCode);

}

}

}

AssetTypeObj = AssetTypeObj.Save();

}

 

#endif

}

 

The Silverlight Save Code is as follows:

public override void Save(object sender, Csla.Xaml.ExecuteEventArgs e)

{

Shell.Instance.ShowStatus(new Status { Text = "Saving", IsBusy = true });

Business.SaveAssetTypeCommand.Execute(Model, (o, ee) =>

{

if (ee.Error == null)

{

 

Model = ee.Object.AssetTypeObj;

OnSaved();

}

else

OnError(ee.Error);

});

 

}

 

On the "Model = ee.Object.AssetTypeObj;" line I get the error.

What should I do to re-assign the saved object to the Model?

 

Copyright (c) Marimer LLC