Using an Entity with GUID as PK - SelfServicing

Posts   
 
    
KastroNYC
User
Posts: 96
Joined: 23-Jan-2006
# Posted on: 25-Apr-2006 08:23:02   

I'm having a problem with some of my entities that use a GUID as a PK

Let's say I have the following Entities:

MembershipUser (PK is a GUID - MembershipUserID) Telephone (PK is a GUID - TelephoneID) ContactInformation (PK is a GUID - ContactInformationID)

I'm using self servicing and on the UI (web) I have a button, in the click event I add some code that might look something like this when creating new entities:


Dim mem As New MembershipUserEntity
Dim tel As New TelephoneEntity
Dim con AS New ContactInformationEntity

mem.UserName = txtUserName.Text
tel.Number = txtPhone.Text
con.FirstName = txtFirstName.Text

mem.MembershipUserID = Guid.NewGuid
tel.TelephoneID = Guid.NewGuid
con.ContactInformationID = Guid.NewGuid

mem.ContactInformation = con
con.Telephone = tel

mem.Save(true)


Is there any way to make the entity automatically create a new guid when the PK is a GUID and you are creating a new record? I don't want my UI layer (or UI design person for that matter) to have to know how the object works (such as a new guid is needed on this entity that entity, etc). It seems that .Save(true) is already figuring out whether an Insert or Update is needed so my question is why doesn't the DQE, when creating the Insert statement see that the table which it is trying to insert to has a PK that is a GUID and then create a new guid automatically at that time?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 25-Apr-2006 08:49:05   

I think you may the code of setting the GUID in the validation logic,

Please refer to "Using the generated code -> Validation per field or per entity" in the LLBLGen Pro manual.

KastroNYC
User
Posts: 96
Joined: 23-Jan-2006
# Posted on: 26-Apr-2006 07:05:15   

Question still remains though, why should I need to set the new GUID anywhere if it is implied that a new id is required when creating a new record in a table who's PK is a GUID?

Here's what I found when looking at the help reference section you pointed me to:

"Validation per entity: implementing IEntityValidator Validation per entity is not done by default, as an entity has not an implementation of IEntityValidator by default. If you want entity-wide validation functionality, you have to implement IEntityValidator and store an instance of this implementation in the entity object you want entity-wide validation logic being applied to. The generated code performs entity-wide validation, if an IEntityValidator instance is present in the entity, when the entity is saved or when Validate() is called. Validate() is a method of every entity object. It normally returns true, if no IEntityValidator object is present, otherwise it calls the Validate() method of the IEntityValidator instance, passing the complete entity object to it.

A special exception is defined to signal validation failures: ORMEntityValidationException(). It is recommended that a validation failure throws this exception as it will fail (and thus roll back!) transactions started for recursive saves. The exception is also useful to define a special catch clause for this exception to correct validation failures.

Implementing IEntityValidator is very simple. Below is an example of IEntityValidator, which implements simple validation logic for the OrderEntity. The implementation as it is given is usable in SelfServicing and Adapter, however only in source code. You can't share a compiled IEntityValidator class which is compiled against SelfServicing classes with an Adapter assembly. "



' VB.NET
Public Class OrderEntityValidator
    Implements IEntityValidator

    Public Function Validate(containingEntity As object) As Boolean _
        Implements IEntityValidator.Validate

        Dim order As OrderEntity = CType(containingEntity, OrderEntity)
        If order.OrderDate>order.RequiredDate Then
            ' error
            Throw New ORMEntityValidationException( _
                "Order.OrderDate is set to a later date than Order.RequiredDate.", _
                containingEntity)
        End If
        ' valid
        Return True
    End Function
End Class


I figure using the senario above is the closest I'm going to get to what I need however it means that for every entity which uses a GUID as a PK I need to create a custom validator class, check whether the entity being passed in, .IsNew = True and then generate the new GUID? So for each of my entities using a GUID as PK I need this:


' VB.NET
Dim mem As New MembershipUserEntity()
Dim membershipValidator As IEntityValidator = New MembershipUserValidator()
order.EntityValidatorToUse = membershipValidator

As well as implementation of MyEntityValidator?

I didn't realize it was so much work just to get a basic entity to "work".

I'm sorry if I'm not understanding this properly from the documentation any help is much appreciated.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 26-Apr-2006 10:02:16   

Another way is to set the GUID in the init routines of the entity, either via an include template or in the user code region. However, if I may, I find it a bit strange that you find it odd that code sets the PK field to a value, while that same code also sets other fields in the same entity. Isn't the PK part of the fields of the entity to set, so just set the PK field to a value, like you have to do with other mandatory fields anyway? (which thus implies your property setting code indirectly knows that these fields are mandatory)

Frans Bouma | Lead developer LLBLGen Pro