Search This Blog

Friday, March 15, 2013

Beware of Session.DataLayer in middle-tier scenarios

I would like to pay your attention to a couple of Support Center tickets on the subject: one and two.
It is important to remember that when you use DataServerObjectSpaceProvider with a dedicated application server (as per Middle Tier Security - .NET Remoting Service), then you cannot write the code like this:

        Session session = ((XPObjectSpace)View.ObjectSpace).Session;
        UnitOfWork uow = new UnitOfWork(session.DataLayer);


I recommend you use the View.ObjectSpace object and its methods to query and manipulate your data, if possible. This is because it will always work correctly regardless of whether you are using a middle-tier server or not.


If, for some reason, you cannot follow this recommended approach, use the Session.ObjectLayer property instead of the Session.DataLayer one.

BTW, you can test the Session.DataLayer property to execute your business logic on the server or on the client only:

protected override void OnSaving() {
if(Session.DataLayer != null && !(Session.ObjectLayer is DevExpress.ExpressApp.Security.ClientServer.SecuredSessionObjectLayer)
) {
        // Server
}
else { 
        // Client
}
base.OnSaving();
}

Here are also screenshots that show values for these properties when the OnSaving is called on the server or on the client sides:



Since we touched OnSaving here, I would also remind you that it can be executed several times (3 times in the screenshot above for the middle-tier scenario) in a general case (as per XPO Best Practices), so it is probably not the best place for your logic at all, unless you are ready to introduce additional checks here.


It is technically possible to run business logic on the application server side via the IApplicationDataService.Commiting event. I am afraid we currently do not have much documentation on this feature as we are improving it to support more scenarios (audit, validation, etc.).



See Also:

3 comments:

  1. How about some code like

    Sub Override OnSaving()
    dim nuow as UnitOfWork = New UnitOfWork(Session.DataLayer)

    End Sub

    because inside onsaving of XPO class, I cannot access to View Object.

    ReplyDelete
  2. It is absolutely the same: you cannot use the Session.DataLayer property in a middle-tier scenario. Use the Session.ObjectLayer instead.
    Finally, it is not a good idea to start one more transaction while the main one is being committed.

    ReplyDelete
  3. http://www.devexpress.com/Support/Center/Question/Details/Q517680

    ReplyDelete