Ticket T222736
Visible to All Users
Duplicate

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

How to reuse XAF Views and other standard module functionality in non-XAF apps, forms or controls

Rule validation and non-XAF application

created 10 years ago

Hi,
I've been trying to get XAF validation rules to work in a non-XAF application for a while but still don't know if it's possible or not. I've been reading a lot along the support tickets but still can't get a clear idea of it. Can you help me please?

My scenario is this:
- I have a big application with lots of validation rules for the entities
- I have recently created a non-XAF application to perform background and massive operations to tha data in the application
- that validation rules are not working in that new application, I mean no exception is thrown when a rule is not correct for any object
- UnitsOfWork are being used in that application, is it correct?

Here's how UnitOfWorks are created in the non-XAF application:

C#
string lConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; System.Reflection.Assembly[] assemblies = new System.Reflection.Assembly[] { typeof(Adreca).Assembly, //DivisionsTerritorials typeof(frsdMessage).Assembly, //ExpressApp.Messaging typeof(CuentaBancaria).Assembly }; //PersistentBase foreach (System.Reflection.Assembly lAssembly in assemblies) { foreach (Type type in lAssembly.GetExportedTypes()) { XafTypesInfo.Instance.RegisterEntity(type); Validator.RuleSet.RegisterRules(XafTypesInfo.Instance.FindTypeInfo(type)); } } foreach (Type type in typeof(Module.Empresas.Empresa).Assembly.GetExportedTypes()) { XafTypesInfo.Instance.RegisterEntity(type); Validator.RuleSet.RegisterRules(XafTypesInfo.Instance.FindTypeInfo(type)); } XafTypesInfo.Instance.GenerateEntities(); DevExpress.Xpo.Metadata.XPDictionary lDict = new DevExpress.Xpo.Metadata.ReflectionDictionary(); lDict.GetDataStoreSchema(typeof(Module.Empresas.Empresa).Assembly); DevExpress.Xpo.XpoDefault.Session = null; IDataStore lStore = DevExpress.Xpo.XpoDefault.GetConnectionProvider(lConnectionString, DevExpress.Xpo.DB.AutoCreateOption.SchemaAlreadyExists); IDataLayer dl = new DevExpress.Xpo.ThreadSafeDataLayer(lDict, lStore); lUnit = new UnitOfWork(dl); XPObjectSpace objectSpace = new XPObjectSpace( XafTypesInfo.Instance, XpoTypesInfoHelper.GetXpoTypeInfoSource(), new CreateUnitOfWorkHandler(() => { return lUnit; }));

But when a CommitChanges() is called validation rules are not applied. Is there something missing?

Otherwise if I try to force rules validation using this code inside an OnSaving method

C#
IObjectSpace lObjSpace = DevExpress.ExpressApp.Xpo.XPObjectSpace.FindObjectSpaceByObject(this); if (lObjSpace != null) Validator.RuleSet.Validate(lObjSpace, this, DefaultContexts.Save);

An exeption is thrown saying that the entity trying to be validate "is not registered".

Due to that we are now having inconsistent objects in the database, can you help us please?

Thanks,

Answers approved by DevExpress Support

created 10 years ago

Hello Sebastia.

Validation rules are not meant to be automatically evaluated when you commit changes. In XAF, this is done by the PersistenceValidationController that subscribes to object space events. In a non-XAF application, you should call RuleSet methods manually before committing changes and process the result.

The error you are getting when calling the RuleSet.Validate method is likely caused by the fact that you use different dictionaries in the XPO data layer and in the object space. Please always use the dictionary obtained from the XpoTypesInfoHelper.GetXpoTypeInfoSource().XPDictionary property, rather than creating a new ReflectionDictionary.

    Show previous comments (1)
    DevExpress Support Team 10 years ago

      >>>is there any way to call fore example all Rules of the "Save" context before performing a commit? Sometimes it can be difficult to know exactly which entities have been modified and will be saved.
      You can get objects that will be updated and deleted on commit using the GetObjectsToSave and GetObjectsToDelete methods of the session. Then call the RuleSet.ValidateAll or ValidateAllTargets method and pass the collection of objects and the context identifier in arguments.

      >>>Is there any documentation about the correct way of creating a UnitOfWork in a non-XAF application? Regarding what you said about the dictionaries I think the code we have above is not correct.
      This functionality is not documented. You can find sample code in the T108751 ticket for example.

      >>>Finally, I suppose that although in a XAF application, if a process is made with UnitOfWork or ObjectSpaces not related to a view rules will not be validated? Is that correct?
      When you save changes in an XAF view, all modified objects are validated by default. This functionality is provided the aforementioned controller activated for the view. If you create an object space not owned by a view, there are no controllers which could validate objects in it.

      S S
      Sebastia Prat i Pons 10 years ago

        Hi again Michael,
        thanks for the accurate response. Everything is clear to me now.
        Best regards,

        DevExpress Support Team 10 years ago

          You are welcome, Sebastià. Should you have additional questions, please feel free to contact us again.

          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.