Ticket Q482084
Visible to All Users
Duplicate

We have closed this ticket because another page addresses its subject:

XpoDataServiceV3 - Provide a virtual method to create a custom Session

ObjectSpace is null when using oData/XpoDataServiceV3

created 12 years ago

Hello

I'm consuming a oData service which inherits your XpoDataServiceV3, for Domain Components.
For retrieving data, it is doing good. But for updates, I noticed that the objectSpace parameter is null within the OnSaving() event.
And I need the objectSpace to enforce some logic on the entities.

This is how I initialize the service:
----------------------------------
static IDataLayer CreateDataLayer()
{
XpoTypesInfoHelper.ForceInitialize();
var typesInfo = XpoTypesInfoHelper.GetTypesInfo();
var xpoTypeInfoSource = XpoTypesInfoHelper.GetXpoTypeInfoSource();
Type type = null;
type = typeof(Survey); typesInfo.RegisterEntity(type.Name, type);
typesInfo.GenerateEntities();

var connectionString = "Pooling=true;Data Source=.;User Id=***;Password=*****;Initial Catalog=****";
var dataStoreProvider = new ConnectionStringDataStoreProvider(connectionString);
var objectSpaceProvider = new XPObjectSpaceProvider(dataStoreProvider, typesInfo, xpoTypeInfoSource);
var dict = objectSpaceProvider.XPDictionary;
var store = XpoDefault.GetConnectionProvider(connectionString, DevExpress.Xpo.DB.AutoCreateOption.SchemaAlreadyExists);

var dataLayer = new ThreadSafeDataLayer(dict, store);
XpoDefault.DataLayer = dataLayer;
return dataLayer;
}
----------------------------------

Now, the domain component's event:
----------------------------------
public void OnSaving(Survey instance, IObjectSpace objectSpace)
{
var space = objectSpace; //<—objectSpace is null
//…
//…
//…
}
----------------------------------

I've tried to use XpoDefault.Session in the OnSaving() event, but it brings an exception saying that the object belongs to another session.
I've even tried "DevExpress.ExpressApp.Xpo.XPObjectSpace.FindObjectSpaceByObject(instance)", but it also returns null.

Finally, is it possible to retrieve the objectSpace (inside the Domain Component logic), from an object that started in a XpoDataServiceV3?

Show previous comments (1)

    Hello Dennis.
    Here is the sample.
    The project is a miscelania of Xaf + Dxtreme oData + Dxtreme Multichannel, in this order.
    Use the Xaf Winforms to compile the database, as I have tested here.
    If you need more info, please advise me.

      Just to document the steps:
      After all is compiled, and the DataService1 is started and the multichannel application is pointing to the DataService1 url, try to add a new Survey object by the DxtremeApplication1.
      When you click the button "Save", you will receive the message "HERE! ObjectSpace is null"
      Look at "XafObjectSpaceNull.Module.BusinessObjects.Survey" class to see the code.

      Dennis Garavsky (DevExpress) 12 years ago

        Hello Sidney,
        Thanks for the sample. I will research it and let you know my results. It will require more time, because as I wrote above this scenario is not currently supported.

        Answers approved by DevExpress Support

        created 12 years ago (modified 12 years ago)

        Hello Sidney,
        The IObjectSpace parameter will not be null in this method only if the object is created by means of the ObjectSpace.
        For instance, you can use the objectSpaceProvider.CreateObjectSpace method to create a valid ObjectSpace object and then use its CreateObject method to create the actual record.
        I hope you find this information helpful.
        UPDATE:
        Attached is a complete sample project (it requires 12.2.8) that shows how to accomplish this. Refer to my blog for more details on this solution: http://dennisgaravsky.blogspot.com/2013/04/exposing-domain-components-dc-via-odata.html

          Comments (3)

            Hi Dennis.
            But the DataService handle it automatically. How can I accomplish that when the DataService just receive and save the data by itself, without creating the object space? Is there any override I can use for forcing it?

            Dennis Garavsky (DevExpress) 12 years ago

              Sidney,
              I am afraid there is no virtual method you can use at the moment.
              Unfortunately, the GetSession method of the XpoDataServiceV3 class is internal:

              C#
              internal UnitOfWork GetSession() { UnitOfWork uow; if (!SessionCache.TryGetValue(this, out uow)) { uow = new UnitOfWork(Context.ObjectLayer); SessionCache.Add(this, uow); } return uow; }

              If it was not internal and non-overridable, then you could override it to return the Session got from the XAF's ((XPObjectSpace)ObjectSpaceProvider.CreateObjectSpace()).Session property.
              There is however the virtual GetService method, but overriding it requires more work:

              C#
              public virtual object GetService(Type serviceType) { if(serviceType == typeof(IDataServiceMetadataProvider)) return Metadata; if(serviceType == typeof(IDataServiceQueryProvider)) return new XpoQueryProvider(this); if(serviceType == typeof(IDataServiceUpdateProvider2)) return new XpoUpdateProvider(GetSession(), this); if(serviceType == typeof(IDataServiceStreamProvider2)) return new XpoStreamProvider(this.CurrentDataSource); if(serviceType == typeof(IDataServiceActionProvider)) { if(typeWithActions == null) typeWithActions = this; return new XpoActionProvider(GetSession(), this, typeWithActions); } return null; }

              I asked our XPO guys to provide a good solution for this: XpoDataServiceV3 - Provide a virtual method to create a custom Session. You will be automatically notified of any changes in this regard.

              Dennis Garavsky (DevExpress) 12 years ago

                Sidney,
                I have some good news for you. After the XpoDataServiceV3 - Provide a virtual method to create a custom Session request was kindly implemented by the XPO team, I managed to get your service working with DC components and DXTREME. I will post an updated project as soon as I can.

                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.