New Posts New Posts RSS Feed: Verification on non-entity properties
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Verification on non-entity properties

 Post Reply Post Reply
Author
mikewishart View Drop Down
Groupie
Groupie
Avatar

Joined: 26-Feb-2010
Location: Reno, NV
Posts: 49
Post Options Post Options   Quote mikewishart Quote  Post ReplyReply Direct Link To This Post Topic: Verification on non-entity properties
    Posted: 11-Mar-2010 at 6:03pm
Is it possible to use DevForce verification on non-entity properties?

for example:

public class MyViewModel : INotifyPropertyChanged
{
    string _valtest;
    [StringLengthVerifier(MinValue = 5, MaxValue = 30, IsRequired = true)]
    public string ValTest
    {
      get { return _valtest;}
      set { _valtest = value; }
    }

     // OnShow method
   {
         IEntityManagerProvider _entityManagerProvider = ServiceLocator.Current.GetInstance<IEntityManagerProvider>();
        _entityManagerProvider.EntityManager.VerifierEngine.DiscoverVerifiers(typeof(MyViewModel ));
   }
}

Why isn't the trigger firing, or if it is, why no exception?  I've verified that the StringLengthVerifier is being placed in the VerifierEngine, so that much is working.

It works if I throw this code in the setter, but that's not very pretty. 
    set {
        IEntityManagerProvider _entityManagerProvider = ServiceLocator.Current.GetInstance<IEntityManagerProvider>();
        VerifierResultCollection results = _entityManagerProvider.EntityManager.VerifierEngine.Execute(this, "ValTest", value);
        foreach (VerifierResult aResult in results)
          if (aResult.IsError)
            throw new Exception(aResult.Description);
        _valtest = value;
    }

I've also tried putting the VerifierEngine.Execute() in a [BeforeSet] adorned method, but the method is never executed.

Issue #2 -
getting an exception:

"Attempt by method 'IdeaBlade.Validation.VerifierEngine.set_ErrorsResourceManager(System.Resources.ResourceManager)' to access field 'IdeaBlade.Validation.VerifierErrorsResource.resourceMan' failed."

on the 2nd line of code below:

DomainModelEntityManager mgr = new DomainModelEntityManager();
mgr.VerifierEngine.ErrorsResourceManager = VerifierErrorsResource.ResourceManager;

I know "engine" is valid since the previous line sets PropertyNameToDisplayNameTranslator with no problems. VerifierErrorsResource is the resx file found in the DevForce install along with the generated Designer.cs file with the class.

This works fine in WPF, but fails in Silverlight.  (running v5.2.6.0)

Thanks!



Edited by mikewishart - 11-Mar-2010 at 9:57pm
Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Posted: 16-Mar-2010 at 7:00pm
I posted incorrect information earlier.  This post has now been corrected and the thread has been cleaned up to avoid confusion.
 
1)  Verification will not automatically fire on a POCO object because the verification engine has no way to know when a property has been set in a generic object.  Thus, you will need to call it explicitly when you want it triggered (as you've done, although obviously the code could be refactored into a method).
 
However, if you want verification for a custom property (e.g. a non-database property) on a DevForce Entity, you can have it automcatically triggered by adding hooks to support DevForce's property interceptors.
 
To support DevForce's property interceptors (which are useful for many things besides verification), you should define your properties like below.  This will enable the VerifierEngine to be notified when the property changes.
 
    [Attributes removed for clarity]
    public String ShoeSize {
      get { return ShoeSizeEntityProperty.GetValue(this); }
      set { ShoeSizeEntityProperty.SetValue(this, value); }
    }
 
Then you will need to define a static variable for each property (in this case ShoeSizeEntityProperty) that returns a DataEntityProperty that describes the property.
 
    // Note the final false indicating that this is not a datastore-backed property!
    public static readonly DataEntityProperty<Employee, String> ShoeSizeEntityProperty =
      new DataEntityProperty<Employee, String>("ShoeSize", false, false, ConcurrencyStrategy.None, false,
                                                                            VerificationSetterOptions.Both, false);

 
You can see more examples of this in our generated code.  Note that the final parameter will differ from the generated code as it indicates that the property is not backed by the database.
 
After you invest the time to add the property interceptor hooks to your class, you'll be able to use the VerifierEngine and Property Interceptors on your custom properties the same way they work on the DevForce generated properties.
 
2)  On the designer screen for the resource, there is an 'Access Modifier' drop down which you can change from 'internal' to 'public'.  If that is not sufficient, you may need to go into the designer.cs file for the resource and change some of the properties to public as well.
 
 


Edited by ting - 24-Mar-2010 at 5:34pm
Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Posted: 24-Mar-2010 at 4:06pm

Hi Mike,

I screwed up and sincerely apologize.  The code I posted only works if you add a custom property to a DevForce Entity (like ShoeSize to an Employee).  It won't work for non-Entity objects, so for that you will need to trigger the verification engine explicitly.
 
I will try and clean up this thread so nobody else gets confused.  Once again, sorry for the bad info!
 
Update: This thread now contains correct information
 


Edited by ting - 24-Mar-2010 at 5:36pm
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down