Print Page | Close Window

Get/Set Interceptors

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2009
Forum Discription: For .NET 3.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1118
Printed Date: 15-Apr-2025 at 1:14pm


Topic: Get/Set Interceptors
Posted By: ken.nelson
Subject: Get/Set Interceptors
Date Posted: 05-Mar-2009 at 12:37pm
Hi, I'm using DevForceEF version 4.3.0 and am a little confused about Get/Set interceptors.  Mostly because I can't seem to find them anywhere.  In previous versions of DevForceEF, in the Object Mapper I was able to choose from Get/Set Interceptor modes, and it's still in the current documentation that this is still supported (around page 119 of the Developers Guide), but the options simply aren't there for me.  Any idea what I'm doing wrong?



Replies:
Posted By: kimj
Date Posted: 05-Mar-2009 at 1:59pm
Ken,
 
You're not doing anything wrong.  We didn't get the documentation completely synchronized with the current functionality, and as you've found still have some old information.  We'll fix this ASAP.
 
In version 4.3.0 the get/set interceptors are always available and not controlled via the Object Mapper.  See the new chapter on Property Interceptors in the Dev Guide for information on how to use them.


Posted By: ken.nelson
Date Posted: 10-Mar-2009 at 7:59am
Thanks for the info.
 
I have one follow on question regarding Property Interceptors.  It appears that any method defined as a Property Interceptor must have public scope.  When I tried private and internal the method never executes.  While it's not a huge problem to have these methods be public, it really dirties up Intellisense.
 
I've tried using the System.ComponentModel attribute: [EditorBrowsable(EditorBrowsableState.Never)] but it still does not hide the method from displaying in Intellisense.
 
The only other 'solution' I could find to the problem was to prefix all of my Property Interceptors with an underline "_", to force them to move out of the way in Intellisense, but this is more of a hack than a real solution, and they're still visible in Intellisense, they're just not mixed in with real properties and methods.
 
Do you have a workaround for this?
 
Thanks,
Ken


Posted By: kimj
Date Posted: 10-Mar-2009 at 9:48am

Property interceptors in a nested public class will still automatically be discovered and applied to properties in the parent class, so you can do something like the following to lessen Intellisense clutter:

public partial class Employee {
 
   public class NestedPropertyInterceptors {
       [BeforeGet(EntityPropertyNames.LastName)]
       public static void BeforeGetLastName(IPropertyInterceptorArgs args) {
          var emp = args.Instance as Employee;
          Console.Writeline("emp:BeforeGetLastName");
       }
    }
}
 
You can also define interception methods in a separate class, being sure to provide the TargetType of the interceptor, and then call the DiscoverInterceptorsFromAttributes method of the PropertyInterceptorManager to force discovery, like this:
 
PropertyInterceptorManager.CurrentInstance.DiscoverInterceptorsFromAttributes(
          typeof(EmployeeInterceptorExtensions));
 
    public class EmployeeInterceptorExtensions {
 
        [BeforeSet( Employee.EntityPropertyNames.Country, TargetType=typeof(Employee))]
        public static void LowercaseCountry(IPropertyInterceptorArgs<Employee, String> args) {
           var country = args.Value;
           if (!String.IsNullOrEmpty(country)) {
             args.Value = country.ToLower();
           }
        }
    }


Posted By: ken.nelson
Date Posted: 10-Mar-2009 at 10:36am
Excellent.  Thanks!



Print Page | Close Window