losing value of the base object's ID

losing value of the base object's ID

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


Parvathy posted on Friday, April 16, 2010

I have a class IndividualApplication which is derived from Application Class which is a businessbase object. Tha Application class has an ApplicationId property which is readonly. A protected method SetIdValue is used to set this ApplicationID property which resides in the Application class.

 

Application class:

[Serializable()]

    public class Application : BusinessBase<Application>

    {

      private int mApplicationID = 0;

      [System.ComponentModel.DataObjectField(true, true)]

        public int ApplicationID

        {

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]

            get

            {

                CanReadProperty(true);

                return mApplicationID;

            }

        }

 

      protected void SetIdValue(int pApplicationID)

        {

            mApplicationID = pApplicationID;

        }

 

      public override Application Save()

        {        

            if (IsDeleted && !CanDeleteObject())

                throw new System.Security.SecurityException("Not authorized to remove an Application.");

            if (IsNew && !CanAddObject())

                throw new System.Security.SecurityException("Not authorized to add an Application.");

            if (!CanEditObject())

                throw new System.Security.SecurityException("Not authorized to update an Application.");

 

            return base.Save();

        }

[..remaining code…]

}

 

IndividualApplication Class:

 

[Serializable()]

    public class IndividualApplication : Application

    {

 

      public override Application Save()

        {

            if (IsDeleted && !CanDeleteObject())

                throw new System.Security.SecurityException("Not authorized to remove an Application.");

            if (IsNew && !CanAddObject())

                throw new System.Security.SecurityException("Not authorized to add an Application.");

            if (!CanEditObject())

                throw new System.Security.SecurityException("Not authorized to update an Application.");

 

            return (IndividualApplication)base.Save();

        }

 

      [Transactional(TransactionalTypes.TransactionScope)]

        protected override void DataPortal_Insert()

        {

            DoInsertUpdate();

           

        }

 

        [Transactional(TransactionalTypes.TransactionScope)]

        protected override void DataPortal_Update()

        {

            DoInsertUpdate();

        }

 

      private void DoInsertUpdate()

        {

       

            // I invoke a proc that returns me a new application ID. I set the application ID using the SetIDValue method of the base class.

            SetIdValue(applicationID);

 

      }

 

My Code behind:

     

App is a variable of type IndividualApplicaiton.

      App.Save();

      Session(“ApplicationID”) = App.ApplicationID;

 

 The ApplicationID property seems to 0. When I tried to debug the same, I found that the proc seems to return the new ID and the moment I step out of the IndividualApplication class I lose the value of the applicationID property. This was a working code in the ASP.net 2.0 version. I am currently trying to migrate the code to .net 3.5.

 

In the ASP.net 2.0 version of the project, CSLA 2.0 was being used.  Currently my 3.5 version is refrencing the CSLA 3.8.2 version. I tried making my 3.5 version project refrence to the CSLA 2.0 and the very same code works like a charm.

 

Is there something I am missing out on? Is there something extra that needs to be done for the same code to work with CSLA 3.8.2 version?

 

Please advice.

 

Thanks

Parvathy

RockfordLhotka replied on Friday, April 16, 2010

Yes, go here (http://www.lhotka.net/search.aspx) and search for the term AutoCloneOnUpdate.

You are not using the results of the Save() method - which is the object with the updated values. The original object is obsolete after Save().

You can revert to the old behavior by setting the AutoCloneOnUpdate config option - and for a web app that may be an acceptable thing to do. For most apps it is not desirable, and the auto-clone behavior addressed a set of common bugs/issues people often faced.

The option was added in version 3, where the default was false. In 3.5 (I think) the default switched to true, because that's the correct setting - but you can still set it to false if you want the old (technically broken) behavior.

JoeFallon1 replied on Saturday, April 17, 2010

The bug is in the line:

App.Save();

It should be:

App = App.Save()

You always have to update your local reference with the return value of the save.

Joe

 

 

Copyright (c) Marimer LLC