Custom Identity with Validation...

Custom Identity with Validation...

Old forum URL: forums.lhotka.net/forums/t/2181.aspx


ballistic posted on Friday, January 19, 2007

Am working on a site where members will need to activate their account before they can log in.

I've created my own custom identity (based on PTIdentity) by adding a new property: DateActivate.

I know I can just add a 'where' clause to check if the member is active in the db, but I would like to inform them that their account is inactive.

From what I've read, I need to make my Identity class inherit from BusinessBase instead of ReadOnlyBase, and add a rule to check that DateActive is not empty. 

Since the private fields in my custom identity class would be populated in the fetch method, how would I trigger the error checking?

I'm hoping I'm going about it the right way.  Would appreciate someone's input.

Thanks,

- Andy

RockfordLhotka replied on Saturday, January 20, 2007

In your DP_Fetch() method, after you've loaded your fields you can just call ValidationRules.CheckRules() to check all the rules for the object.

ballistic replied on Sunday, January 28, 2007

Rocky,

Did like you suggested and my validation methods get called, but I do not get any exceptions raised even after my methods return false.

As fas as I know, I do not need to raise the exception myself, but I might be wrong.

Here is what I have:

    Public Class CommunityIdentity
        'Inherits ReadOnlyBase(Of CommunityIdentity)
        Inherits BusinessBase(Of CommunityIdentity)


        Protected Overrides Sub AddBusinessRules()
            ValidationRules.AddRule(Of CommunityIdentity)(AddressOf AccountNotDeleted, "DateDeleted", 10)
        End Sub

        Private Shared Function AccountNotDeleted(Of T As CommunityIdentity)(ByVal target As T, ByVal e As Validation.RuleArgs) As Boolean
            If Not (target.DateDeleted.IsEmpty) Then
                e.Description = My.Resources.MemberAccount_Deleted
                Return False
            Else
                Return True
            End If
        End Function

RockfordLhotka replied on Monday, January 29, 2007

Where are you expecting an exception? CheckRules() doesn't throw exceptions, it just puts the object into an invalid state if any rules are broken. You'd only get an exception when calling Save() on your root object. Save() throws a ValidationException if the object is in an invalid state.

Review Chapter 3 for more details on how this works. Or Chapters 9 and 10 to see how the validation rules are used in Windows and Web UIs.

ballistic replied on Monday, January 29, 2007

Rocky,

I will review chapter 3, but based on your explanation and examples I think I understand how it works.

What I would like to happen is that when I set the values from the db to my object if the member has been marked as deleted in the db, that I would get an exception, which I can then pass up to the principal class and then to the gui.  Would I need the Get method to check the object that was fetched and raise an exception if is invalid???

On a similiar note, there will be several times when I need to validate input information (the username) before I query a member.  I would like to call a Get factory method and get an exception raised if the username is not of valid format.  Would I need to raise my own exception since I would not be calling Save()?

RockfordLhotka replied on Monday, January 29, 2007

In both your examples you'll need to throw your own exceptions.
 
Rocky

ballistic replied on Monday, January 29, 2007

Thank you very much!!!

In the factory methods where I check the validity of input parameters I'll simply check their format and throw my own exception if invalid.

In the case of the Identity, where the values from the database tell me if the member is invalid, I will populate my object then call ValidationRules.CheckRules() to make the object invalid. 

I can then check in the calling GetIdentity factory method to see if it is invalid.  I can then throw an exception if invalid and check its BrokenRulesCollection for the exact reason why it's not valid.

Thank you!

- Andy

Copyright (c) Marimer LLC