Validator examples, C# version of INotifyPropertyChanged

Share with us how you use PostSharp, or how you would like to use it.

Validator examples, C# version of INotifyPropertyChanged

Postby PeteWeissbrod on Sun Jan 20, 2008 8:55 pm

Hi I recently found your PostSharp project and I think it is very inspiring!

My name is Peter. I would like to share my code with you.
I have been working on property validators, and a C# implementation of INotifyPropertyChanged.

Examples of my code can be found on my blog at the following two links:
http://www.acceptedeclectic.com/2008/01 ... -dont.html
http://www.acceptedeclectic.com/2008/01 ... pport.html

My INotifyPropertyChanged code originated from the VB examples on your user samples project on google code. I removed the IEditable functionality because it did not integrate with the other enhancers that I have written.

I am very new with PostSharp, and I would like to verify that I am using your library correctly.

Would you like my code in your user samples project?
PeteWeissbrod
 
Posts: 7
Joined: Sun Jan 20, 2008 8:49 pm
Full Name: Peter Weissbrod

Re: Validator examples, C# version of INotifyPropertyChanged

Postby gfraiteur on Sun Jan 20, 2008 9:18 pm

Peter,

Of course you are welcome to contribute with your code samples! Just communicate me your Google Account ID and I will make you a member.

I have a question however -- maybe a stupid one. The VB sample you found is basically a translation in VB of a sample that is installed with PostSharp. What is your sample different in? I would like to avoid different samples of basically the same thing.

As for other samples, it's of course not a problem to publish it.

Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 608
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: Validator examples, C# version of INotifyPropertyChanged

Postby PeteWeissbrod on Sun Jan 20, 2008 11:17 pm

Thanks for the fast reply. I was using your VB example of "SupportDataBinding" from google code. I translated this into C#, then I used it in a business object like:
Code: Select all
[SupportDataBinding]
    public class SomeBusinessObject
    {
        #region members
        private string m_Name;
        #endregion

        #region properties
       
        [NotNullValidator]
        [Logger( LogOnEntry = true, LogOnException = true, LogOnSuccess = true)]
        [StringLengthValidator(MinLength = 1,MaxLength = 10)]
        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }
        #endregion

        #region methods
        public SomeBusinessObject(string name)
        {
            m_Name = name;
        }
        #endregion
    }


Your code example of SupportDataBindingAttribute works correctly, when it is alone. When I add my own enhancers such as the "Logger" or "NotNullValidator", I have some problems with the IEditableObject feature. The EditableObjectImplementation.FirePropertyChangedForAll() method can throw a SecurityException when I call CancelEdit().

I believe this is the problem line:
Code: Select all
myINotifyPropChangedImpl = DirectCast(mixIns.GetImplementation(Nothing), NotifyPropertyChangedImplementation)


My reflection knowledge is weak, but it seems to me that "InstanceCredentials.Null" are not good enough credentials to access the INotifyPropertyChanged functionality at runtime if I add my own enhancers.

Here is an example of one of my validators:
Code: Select all
[Serializable]
    public class NotNullValidator : OnMethodBoundaryAspect
    {
        public override void OnEntry(MethodExecutionEventArgs eventArgs)
        {
            //ignore calls that have no arguments, they come from reflection
            if (eventArgs.GetArguments() == null)
                return;
            //ignore anything that isnt a setter
            if (!eventArgs.Method.Name.StartsWith("set_"))
                return;

            if (eventArgs.GetArguments()[0] == null)
                throw new ArgumentNullException(eventArgs.Method.Name);
            base.OnEntry(eventArgs);
        }
    }

I would like to try this again with the "installed" version of [SupportDataBinding].
I cannot find it. Tell me, which namespace is this [SupportDataBinding] attribute defined?

Later this evening, I am going to test PostSharp's behavior with the DynamicProxy functionality of NHibernate, this is important to me because all of my business object persistence is managed by NHibernate which relies heavily on reflection. I will be able to give you some feedback on this very soon, if you are interested.
PeteWeissbrod
 
Posts: 7
Joined: Sun Jan 20, 2008 8:49 pm
Full Name: Peter Weissbrod

Re: Validator examples, C# version of INotifyPropertyChanged

Postby PeteWeissbrod on Wed Jan 23, 2008 11:18 pm

I have some compile-time validator attributes written. This is .NET 3.5, but the code should run fine in earlier versions.

There general usage is attributes for setters of various properties.
I am uncertain of one thing in my code:
Am I using Compile-Time validation correctly?

I dont know. This is why my test cases fail for the SqlDateTimeValidator. Hopefully, with a bit of your help we can add it to the user samples project.

My apologies about the broken references, the forum only allows upload files of size 256K :roll:
You do not have the required permissions to view the files attached to this post.
PeteWeissbrod
 
Posts: 7
Joined: Sun Jan 20, 2008 8:49 pm
Full Name: Peter Weissbrod

Re: Validator examples, C# version of INotifyPropertyChanged

Postby gfraiteur on Thu Jan 24, 2008 11:16 am

Hi Peter,

I was using your VB example of "SupportDataBinding" from google code.


It is not my code! I don't even verify it completely.

The best way to share and modify code is to use the project http://code.google.com/p/postsharp-user-samples/. If you tell me your google account id (gmail), I could make you a member and you could upload your samples!

Cheers,

Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 608
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: Validator examples, C# version of INotifyPropertyChanged

Postby PeteWeissbrod on Thu Jan 24, 2008 3:56 pm

Understood. my google account is talktopete.
PeteWeissbrod
 
Posts: 7
Joined: Sun Jan 20, 2008 8:49 pm
Full Name: Peter Weissbrod

Re: Validator examples, C# version of INotifyPropertyChanged

Postby gfraiteur on Thu Jan 24, 2008 3:58 pm

You are a project member :geek: .
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 608
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: Validator examples, C# version of INotifyPropertyChanged

Postby PeteWeissbrod on Thu Jan 24, 2008 5:51 pm

Thanks, everything is committed for now. Do let me know if theres anything else I can do to help.
Otherwise, I am working on a Authorization security-based Attribute in the near future, so I will see you again.
PeteWeissbrod
 
Posts: 7
Joined: Sun Jan 20, 2008 8:49 pm
Full Name: Peter Weissbrod

Re: Validator examples, C# version of INotifyPropertyChanged

Postby gfraiteur on Thu Jan 24, 2008 5:52 pm

Ok, thank you!
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 608
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org


Return to What To Do With PostSharp?