EntityBase2 and Fields

Posts   
 
    
itPhoenix
User
Posts: 11
Joined: 03-May-2007
# Posted on: 26-Nov-2007 23:43:27   

Hello, Long time lurker, first time thread starter.

I am having a little trouble with a situation where I am trying to use a cutom Validator Class for three different entities...

Imports SD.LLBLGen.Pro.ORMSupportClasses
Imports SelectHealth.EntityClasses
Imports System.Text
Namespace Validator
    <DependencyInjectionInfo(GetType(ProviderContactDataEntity), "Validator", _
                ContextType:=DependencyInjectionContextType.NewInstancePerTarget)> _
                <DependencyInjectionInfo(GetType(OrganizationContactDataEntity), "Validator", _
                ContextType:=DependencyInjectionContextType.NewInstancePerTarget)> _
                <DependencyInjectionInfo(GetType(OfficePersonnelContactDataEntity), "Validator", _
                ContextType:=DependencyInjectionContextType.NewInstancePerTarget)> _
    Public Class ContactDataValidator : Inherits ValidatorBase

        Private errorMsg As String
        Private errorBuilder As StringBuilder
        Private throwException As Boolean
        ''' <summary>
        ''' Method to validate the containing entity right before the save sequence for the entity will start. LLBLGen Pro will call this method right after the
        ''' containing entity is selected from the save queue to be saved.
        ''' </summary>
        ''' <param name="involvedEntity">The involved entity.</param>
        Public Overrides Sub ValidateEntityBeforeSave(ByVal involvedEntity As IEntityCore)
            Dim entity As EntityBase2 = CType(involvedEntity, EntityBase2)
            Dim userName As String = SecurityHelper.GetLoggedUser.LoggedName

            MyBase.ValidateEntityBeforeSave(involvedEntity)
            ValidateEntity(involvedEntity)


            entity.SetNewFieldValue("UserUpdated", userName)
            entity.SetNewFieldValue("DateUpdated", Date.Now())

            If entity.IsNew Then
                entity.SetNewFieldValue("UserInserted", userName)
            End If

        End Sub

        Public Overrides Sub ValidateEntity(ByVal involvedEntity As SD.LLBLGen.Pro.ORMSupportClasses.IEntityCore)
            MyBase.ValidateEntity(involvedEntity)

            throwException = False

            errorBuilder = New StringBuilder("")
            Dim toValidate As EntityBase2 = CType(involvedEntity, EntityBase2)
            If toValidate.Fields("ContactData") Is Nothing OrElse toValidate.Fields("ContactData") = "" Then
                errorBuilder.Append("The Provider Contact Data cannot be empty.  ")
                errorBuilder.AppendLine()
                throwException = True
            End If

            If throwException Then
                errorMsg = errorBuilder.ToString
                Throw New ORMEntityValidationException(errorMsg, toValidate)
            End If

        End Sub
    End Class
End Namespace

All three entities have the same meta-data fields of UserInserted, UserUpdated, and DateUpdated. In fact the only reason that these aren't all the same 'table' is for referential integrity (NO MUCK!).

What I was hoping to do, was use the same validator class for all three entities. When compiled and run, I get an error: Unable to Case object of type 'SD.LLBLGen.Pro.ORMSupportClasses.EntityField2' to Type 'System.String'

I'm sure that I'm not using EntityBase2 or the SetNewFieldValue method properly; however, I'm not sure in what direction to go to fix this.

Has anyone come across a situation like this before?cry

Thanks!

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 27-Nov-2007 06:59:11   

Hi itPhoenix,

Could you please post the Exception exact message and StackTrace?

David Elizondo | LLBLGen Support Team
itPhoenix
User
Posts: 11
Joined: 03-May-2007
# Posted on: 27-Nov-2007 16:48:52   

Sure, Thanks for the help. Stack Trace:

 at PdsBll.Validator.ContactDataValidator.ValidateEntity(IEntityCore involvedEntity) in C:\Documents and Settings\User1\My Documents\Visual Studio 2005\Projects\PdsBll\PdsBll\Validator Classes\ContactDataValidator.vb:line 45
   at PdsBll.Validator.ContactDataValidator.ValidateEntityBeforeSave(IEntityCore involvedEntity) in C:\Documents and Settings\User1\My Documents\Visual Studio 2005\Projects\PdsBll\PdsBll\Validator Classes\ContactDataValidator.vb:line 26
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2.OnValidateEntityBeforeSave()
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2.CallValidateEntityBeforeSave()
   at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.PersistQueue(List`1 queueToPersist, Boolean insertActions, Int32& totalAmountSaved)
   at SD.LLBLGen.Pro.ORMSupportClasses.UnitOfWork2.Commit(IDataAccessAdapter adapterToUse, Boolean autoCommit)
   at PdsBll.ProviderFormSaveManager.SaveProviderFormEntities(ProviderEntity provider, EntityCollection`1 congruities, EntityCollection`1 boardCertification, EntityCollection`1 providerAffiliations, EntityCollection`1 privilegeRequests, EntityCollection`1 attendedInstitution) in C:\Documents and Settings\User1\My Documents\Visual Studio 2005\Projects\PdsBll\PdsBll\Save Entities\ProviderFormSaveManager.vb:line 62
   at PdsGui.ProviderFormController.SaveDirtyProviderFormEntities() in C:\Documents and Settings\User1\My Documents\Visual Studio 2005\Projects\PdsGui\PdsGui\Entity Forms\Provider Form\ProviderFormController.vb:line 492
   at PdsGui.ProviderForm.tsbProviderFormSave_Click(Object sender, EventArgs e) in C:\Documents and Settings\User1\My Documents\Visual Studio 2005\Projects\PdsGui\PdsGui\Entity Forms\Provider Form\ProviderForm.vb:line 438
   at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
   at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
   at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at PdsGui.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
   at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
   at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
   at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
   at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
   at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Exception Thrown:

{"Unable to cast object of type 'SD.LLBLGen.Pro.ORMSupportClasses.EntityField2' to type 'System.String'."}

Also, I edited the post above to include the rest of the Validator Code (which is the Method ValidateEntity).

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 28-Nov-2007 12:08:02   

If toValidate.Fields("ContactData") Is Nothing OrElse toValidate.Fields("ContactData") = "" Then

The exception is thrown because of the above line. entity.Fields[] is an array of EntityField objects, and in the above line you are comparing an EntityFiled with an empty string ("").

Instead you can do the following:

If toValidate.Fields("ContactData").CurrentValue Is Nothing OrElse toValidate.Fields("ContactData").CurrentValue = "" Then
itPhoenix
User
Posts: 11
Joined: 03-May-2007
# Posted on: 29-Nov-2007 14:25:52   

OK... That worked great. I just copied one of my other Validators and forgot to use my brain (Went from a typed entity like 'toValidate.Name' to 'toValidate.Fields("ContactData")'.

Thanks Walaa! sunglasses