Print Page | Close Window

Can't seem to stop the verification engine.

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2298
Printed Date: 25-Jun-2026 at 5:26am


Topic: Can't seem to stop the verification engine.
Posted By: BringerOD
Subject: Can't seem to stop the verification engine.
Date Posted: 11-Nov-2010 at 11:11am
Can't seem to stop the verification engine.  I am doing a batch procedure that needs to ignore validation.
 
Any idea where I am going wrong

var leadIdToTest = new Guid("a7579944-fd68-4f87-875d-01cc15ba6662");

var entities = PrimeSystem.CreatePrimeEntities();

entities.VerifierEngine.Enabled = false;

var lead = entities.Leads.Where(x => x.LeadId == leadIdToTest).SingleOrDefault();

lead.EntityAspect.VerifierEngine.Enabled = false;

lead.LastName += "1";

entities.SaveChanges();

entities.VerifierEngine.Enabled = true;




Replies:
Posted By: kimj
Date Posted: 12-Nov-2010 at 12:00pm
Your support form request provided the important clue - where you said that the SaveChanges() call throws the error.
 
DevForce will by default do "server-side" validation.   This validation uses a separate VerifierEngine that the one used client side, even in a 2-tier application.  So, while you've turned off client-side validation, server-side validation is still running.
 
To turn off validation on the server you'll need to implement a custom EntityServerSaveInterceptor and either override the ValidateSave method or disable the VerifierEngine available on the interceptor.  If you disable the engine here you'll be disabling all server-side validation in your application since this VerififerEngine is shared across all client requests; so be careful, in an n-tier application this is a security vulnerability.  The best approach is to turn off validation in the ValidateSave on a more granular level, for example for specific entity instances or entity types.


Posted By: BringerOD
Date Posted: 12-Nov-2010 at 2:05pm

Thanks for the reply. That makes sense.

 

Unfortunately, this does not fit what I need to do.

 

I only need to stop validation for this function only.  Meaning I want server and client side validation for this entity and all entities except during this BATCH operation.

 

This is because there could be some legacy data that is not valid in the current database.  When I save the batch procedure it will try and validate the invalid data, where I don’t want to at this time.

 

Hope this makes sense.

 


Posted By: kimj
Date Posted: 12-Nov-2010 at 4:43pm
I was afraid you were going to need this for a specific operation within the wider application.  There's no built-in way to pass some sort of tag along with the save to identify a specific "batch", unlike a query which does have a Tag property.  You can simulate this, however, by adding a Tag (or whatever you want to call it) to the entity definition(s) for any entities you might work with in the batch.  If there are quite a few entities, then maybe add the property to a base class.  As long as the property has a public getter and setter and is marked as a DataMember, it will be serialized with the rest of the entity properties when the save is requested.  This is unfortunately not secure though, since it allows the client application to turn off server validation, so you'd need to take care that only authorized users can perform saves.
 
Here's a simple example -
 
public partial class Lead {
   System.Runtime.Serialization.DataMember]
   public bool BypassServerValidation { get; set; }
}
 
In the save interceptor -
  protected override bool ValidateSave() {
    var changes = EntityManager.FindEntities(EntityState.AnyAddedModifiedOrDeleted).OfType<Lead>();
    if (changes.Any(e => e.BypassServerValidation)) {
      return true;
    } else {
      return base.ValidateSave();
    }
  }
Another option, if you can use a separate EntityManager for the batch, is to use a different CompostionContext for the batch, and define a custom save interceptor which bypasses validation for this CompositionContext only.  This is likely not as complicated as it sounds, and if you're interested I'll get a quick sample together for you.


Posted By: BringerOD
Date Posted: 13-Nov-2010 at 9:27am
Thanks!

This really helped.

If it is not too much trouble, it would be great to see the CompositionContext only sample.




Posted By: kimj
Date Posted: 15-Nov-2010 at 3:20pm
1) First define a "composition context resolver" on both client and server:
  public class MyCompositionContextResolver : BaseCompositionContextResolver {

   public static CompositionContext CustomContext = new CompositionContext("NonValidatingBatch"
      falsenew Type[] { typeof(NonValidatingSaveInterceptor) });
   }
2) Define a custom save interceptor on the server:
  [PartNotDiscoverable]
  public class NonValidatingSaveInterceptor : EntityServerSaveInterceptor {
    protected override bool ValidateSave() {
      return true;
    }
  }
 
3) In the client code, provide the "composition context name":

var entities = PrimeSystem.CreatePrimeEntities(compositionContextName: MyCompositionContextResolver.CustomContext.Name

);




Print Page | Close Window