Ticket Q415894
Visible to All Users

How to create XPO for Different scenarios

created 13 years ago

In XPO:
Person p = new Person (Session);
p.Name = "Sam";

p.Save() method is not necessity ?

In Controller:
IObjectSpace objSpace = Application.CreateObjectSpace();
Object obj = new Person(session);

and Person's constructor need pass session.
How to get session in controller?

And the operation above for Different scenarios is right ?

Answers

created 10 years ago

Hi Dennis,

You said:

C#
If you use UnitOfWork (it is a descendant of Session), then you should call its CommitChanges or CommitTransaction methods.

My problems:
When we use CommitChanges or CommitTransaction?please, tell me know. What's difference?

Thanks

    Comments (3)
    Dennis Garavsky (DevExpress) 10 years ago

      Hi Huy,

      There is not much difference, as the UnitOfWork.CommitChanges method uses the base Session.CommitTransaction method internally:

      C#
      public void CommitChanges() { WaitForAsyncOperationEnd(); if(!InTransaction) return; CommitTransaction(); }

        Hi Dennis,
        Thanks for reply,
        After many years, my project's so big, But maybe, it's always had some errors. I must be spent many time to do
        Maybe, I guest I'm not clearly somethings between Them.
        You mean:
        CommitChanges() vs CommitTransaction() difference:
        CommitChanges() have WaitForAsyncOperationEnd() ?
        if all processes are Done, It's will call CommitTransaction(); Then CommitTransaction() will update to DB?
        if WaitForAsyncOperationEnd(); had error, It's will RollbackTransaction() right?
        Because, I think we still have some mislead with WaitForAsyncOperationEnd();
        Do you have any small example to explain clearly more about it? I think It's very helpful for me and my projects.

        Dennis Garavsky (DevExpress) 10 years ago

          Huy,

          As I mentioned above, there is no significant difference between these two methods for most scenarios and you can use either of them. The CommitTransaction method is a base method of the Session class, which can be overridden in descendants, e.g. ExplicitUnitOfWork. The CommitChanges method is a method in the UnitOfWork, which adds additional behavior as can be seen from the code snipped I gave above (there is also InTransaction check).
          I am not sure which additional examples you need for this and what real problems you have with these methods. If you have problems with these methods, create a separate ticket and attach your problematic sample project along with a use-case scenario description so we can help you further.

          created 13 years ago (modified 13 years ago)

          Hello,
          In XPO, if you use Session and want to save your changes, you should either call the Save method of your persistent object or use the CommitTransaction method of the Session class.
          If you use UnitOfWork (it is a descendant of Session), then you should call its CommitChanges or CommitTransaction methods. You can also use the Save method to mark your persistent object as modified.
          In XAF, there is a similar entity called ObjectSpace. It uses UnitOfWork internally. To save data in an ObjectSpace, use its CommitChanges method.
          To create a new object, use the CreateObject method of the ObjectSpace class. Of course, you can access the underlying XPO session via the ((XPObjectSpace)theObjectSpace).Session.
          So, these are the same scenarios, just implemented differently.
          I hope this clarifies things for you.

            Comments (1)
            CR CR
            Chris Royle (LOBS) 13 years ago

              Also, one can create an extension method and then express this as theObjectSpace.Session() - which reads better and this method can also check that the typecast returns non null. Also useful for migrating from older versions of XAF.
              Something like:

              C#
              public static DevExpress.Xpo.Session Session(this IObjectSpace objectSpace) { Guard.ArgumentNotNull(objectSpace, "objectSpace"); DevExpress.ExpressApp.ObjectSpace objectSpaceInstance= objectSpace as DevExpress.ExpressApp.ObjectSpace; if (objectSpaceInstance== null) throw new Exception("Unable to cast IObjectSpace to DevExpress.ExpressApp.ObjectSpace"); return objectSpaceInstance.Session; }

              UPDATE:
              This code is for 11.2 only. Use DevExpress.ExpressApp.Xpo.XPObjectSpace in version 12.1

              Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

              Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.