Object not updating

Object not updating

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


strangerd posted on Thursday, January 21, 2010

Hi

In the documentation somewhere, it talks about objects being "refreshed". After I create a new object and save it into the DB, is the object supposed to be brought back to be used immediately in the application?

For instance I create a new task with a new DB generated identity field in SQL Server. I want to use the ID in the application immediately afterwards. Is the ID property in the object supposed to have the new ID value from SQL after it has been saved?

Or is the solution to use SCOPE_IDENTITY() in the stored proc that creates the record.

Thank you


Calin replied on Thursday, January 21, 2010

Hi Strangert,

Here is how I achive what you requiere. In the ObjectFactory I have a method DoInsert that inserts the new object in the database. Here some usefull code from it.

private void DoInsert(BusinessObject bo)
{
... update the BO properties ...

using (BypassPropertyChecks(bo))
{
LoadProperty(bo, BusinessObject .IdProperty, boEntity.ID);
}

.... rest of the code like MarkAsOld(bo) ...
}

Now this is called from the Update method in the Object Factory:

[Transactional(TransactionalTypes.TransactionScope)]
private BusinessObject Update(BusinessObject bo)
{
.... update and delete code ...
if(bo.IsNew)
{
DoUpdate(bo);
}

return bo;
}

You might have noticed that I use a ORM mapper (nhibernate in this case) this explains the boEntity.ID my data entity get's automaticly updated when saving into the databse, and all I have to do is to load the new id in the business object.

Hope this answers your questions,
Regards.

strangerd replied on Thursday, January 21, 2010

Thanks

JonnyBee replied on Thursday, January 21, 2010

Hi,

When you save a Csla object a new object is returned that will contain f.ex the Id generated in the DB.

var savedObject = myBO.Save();

the new savedObject should then be databound to the UI and will containg the generated id.

How you get the Id generated in the DB into you BO would depend on the chosen Data Access technology. EntityFramework and ADO.NET are different breeds and I am not sure what you are using.

strangerd replied on Thursday, January 21, 2010

Thank you

strangerd replied on Friday, January 22, 2010

Hi JonnyBee

I am using ADO.NET

Thanks

Copyright (c) Marimer LLC