ValidateEntityBeforeSave TargetperEntity

Posts   
 
    
kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 21-Jan-2010 21:39:50   

Hi Guys,

I have two classes and i say person - employee where employee is subtype of person entity. I have set up validation using partial classes and i would like to do some validation in the ValidateEntityBeforeSave routine for both entities to check data pertaining to the same issue. My problem is that the ValidateEntityBeforeSave routine for the person entity seems to be the only one that fires while that for the employee entity does not fire at all.

However when i do not associate the person entity with its validator class by not setting the validator for the person entity in the oninitialized method the onvalidatebeforesave method fires. Is this a bug with llblgen or is there something i am not understanding

I am using llblgen 2.6 sql 2k8 selfservicing

Regards Kioko

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 22-Jan-2010 04:39:51   

Hi Kioko,

Try using TargetKind=DependencyInjectionTargetKind in the attributes of your Validator class. This attribute is part of DependencyInjection. If you have troubles with that please post your code snippet. Related thread: http://llblgen.com/tinyforum/Messages.aspx?ThreadID=12965

David Elizondo | LLBLGen Support Team
kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 22-Jan-2010 08:24:43   

Hi,

Does this mean that in order to solve this issue i have to implement validation via dependence injection/.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 22-Jan-2010 08:49:39   

You don't have to.

Would you please post some code snippets of how you set the validator to the entity in hand?

kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 22-Jan-2010 17:27:21   

Hi this is what i have done.

The Person Entity


    Partial Public Class PersonEntity

        Protected Overrides Sub OnInitialized()
            Me.Validator = New PersonValidator
            MyBase.OnInitialized()
        End Sub
    End Class

In the Validator Entity Class for the PersonEntity


Public Class PersonValidator
        Inherits ValidatorBase
        Public Overrides Sub ValidateEntityBeforeSave(ByVal involvedEntity As SD.LLBLGen.Pro.ORMSupportClasses.IEntityCore)
            
            Dim objPerson As PersonEntity = DirectCast(involvedEntity, PersonEntity)
            Dim sbErrorMessage As New StringBuilder
            '..... some validation code on objPerson goes here
            MyBase.ValidateEntityBeforeSave(involvedEntity)

        End Sub
    End Class

The Employee Class has been set up to in exactly the same way. In my design the Employee is the subtype of the Person entity. I would like to validate the Employee entity when before its being saved and i would also like to validate the Person entity before it is being saved. The bug i am facing is that the validation for the employee class is not firing when i save an employee enity using its save method and it only fires when the person entity is not set up with a validator class. My question is how do i ensure that both validation methods run when i save an employee entity and and in which order will they run.

Regards Kioko

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Jan-2010 00:57:30   

I see now. The problem is that you are setting your validator in OnInitialized method. Apparently is too late in that method for inherited entities. Please overrides CreateValidator in you base class only (PersonEntity), as Employee inherits from Person you don't have to create again the validator in EmployeeEntity. That function is called exactly when the framework needs to initialize it. Here is an example:

Protected Overrides Function CreateValidator() As IValidator
     Return New PersonValidator
End Function

David Elizondo | LLBLGen Support Team
kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 23-Jan-2010 08:33:17   

Hi,

Hay Daelmo,

This is great thanks in a way it does solve my problem. However the way it seams to work is that now when i save the sub type (Employee) the validator method ValidatebeforeSave for the supertype (Person) does not fire. The way i would have liked it to work is that if i was saving the Employee entity the validatebeforesave for the employeeentity would fire and then the validatebeforesave for the person entity to save. This is because there is validation code the runs for person enitity and there is validation code for member entity.

Regards Kioko

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 24-Jan-2010 21:35:01   

Hi Kioko,

As the EmployeeEntity inherits from PersonEntity when you CreateValidator for Employee, this is used as the validator of the entity. You wont get two validators called. One thing you could do is Inherit the validator. I will explain with an example:

  1. Create the validator for PersonEntity:
public class PersonValidator : ValidatorBase
{
    public DummyValidatorBase()
    {
    }

    public override void ValidateEntityBeforeSave(IEntityCore involvedEntity)
    {
        // validate things
        //...

        // validate base
        base.ValidateEntityBeforeSave(involvedEntity);
    }
}
  1. Create a Validator for EmployeeEntity. This validator will inherit from PersonValidator
public class EmployeeValidator : PersonValidator
{
    public DummyValidatorBase()
    {
    }

    public override void ValidateEntityBeforeSave(IEntityCore involvedEntity)
    {
        // validate things
        //...

        // validate base (PersonEntity)
        base.ValidateEntityBeforeSave(involvedEntity);
    }
}
  1. In PersonEntity override CreateValidator this way:
protected override IValidator CreateValidator()
{
    return PersonValidator();
}
  1. In EmployeeEntity override CreateValidator this way:
protected override IValidator CreateValidator()
{
    return EmployeeValidator();
}

This is the recommended approach. This way you have flexibility. Look at the possible cases:

A. When you instantiate and save only PersonEntity, only ValidateEntityBeforeSave method of the PersonValidator is called.

B. When you instantiate and save an EmployeeEntity, the ValidateEntityBeforeSave method of the EmployeeValidator is called, and the ValidateEntityBeforeSave of the PersonEntity as well. You can call the base.ValidateEntityBeforeSave before or after your custom employee validation rules are performed.

C. If you want to customize the validation rules of the employeeEntity, you can do that in the EmployeeValidator, without disturbing the PersonValidator.

Hope helpful wink

David Elizondo | LLBLGen Support Team
kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 25-Jan-2010 09:48:17   

Fantastic

This is awesome. This is what i was looking for. Let me give it a shot but the logic seems to add up and i'm sure it will give me the results i was looking for.

Kioko