Deep Save ... Again

Posts   
1  /  2
 
    
kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 01-Feb-2010 16:44:24   

Hi Guys,

I am facing a problem very similar that which was asked in the following thread http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=14615&HighLight=1

I am using SQL2K8 and Llblgen 2.6

I have the following tables

Class ID (PK)(Identity) Name

Student ID(PK)(Identity) Name Age

ClassRegistration ID(PK)(Identity) StudentID(FK) To Student both StudentID and ClassID constitute a unique constraint ClassID(FK) To Class

Invoice ID(PK)(Identity) ClassRegistrationID(FK)(UC)

InvoiceItems ID(PK)(Identity) InvoiceID(FK) ItemID(FK) Quantity Price LineTotal

Item ID(PK)(Identity)(FK) Desc

The UI is a form bound to a single instance of the class ID which contains a gridview that is bound to a collection class of the class registration id.

What i am trying to achieve is to enable users register students to a class using the gridview and a look up combo. This works fine. When they save the class I would like to create an invoice for each class registration. Now given that i would like to implement this without dependency injection but rather with partial classes how would i go about this. I have tried using the validation without much success. I know i can be able to achieve this by putting logic in the UI but i really would like the BL to handle all this.

Thanks in advance for your help.

Kioko

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 01-Feb-2010 17:24:44   

You can use an Auditor as shown in the thread you mentioned. And it's not a must to use DI to link the Auditor to an entity.

You can jsut override OnAuditInsertOfNewEntity in a partial class of the entity. Or Create an Auditor class and Setting and then you should override OnInitializing in an entity to add the creation of the Auditor class.

Also you can override the Entity method CreateAuditor. This is a protected virtual (Protected Overridable) method which by default returns null / Nothing. You can override this method in a partial class or user code region of the Entity class to create the Auditor to use for the entity.

kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 01-Feb-2010 17:51:40   

Hi,

Thanks let me give it a shot and see if it will work for me.

Kioko

kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 01-Feb-2010 21:15:51   

Hay

It kinda worked for me but now i am having another issue that i need you help. Now imagine i was to extend the design by including two new entities. ClassInvoice which is a SubType of Invoice. This heriacy being implemented via TargetPerEntity. And an InvoiceRegister Entity which has a one to one relationship with the InvoiceEntity. The Entities look as follows Class ID (PK)(Identity) Name

Student ID(PK)(Identity) Name Age

ClassRegistration ID(PK)(Identity) StudentID(FK) To Student both StudentID and ClassID constitute a unique constraint ClassID(FK) To Class

ClassInvoice ID(PK)(FK) on Invoice ClassRegistrationID(FK)(UC)

Invoice ID(PK)(Identity)

InvoiceItems ID(PK)(Identity) InvoiceID(FK) ItemID(FK) Quantity Price LineTotal

InvoiceRegister ID(PK)(IDENTITY) InvoiceID(FK)(UC)

Item ID(PK)(Identity)(FK) Desc

I have managed to set up an auditor for the classRegistration entity as explained in the referenced thread DeepSave to create an ClassInvoice Entity. However would i be able to automatically create and persist an InvoiceRegisterEntity everytime an invoice is created?

Kioko

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 02-Feb-2010 06:30:04   

kioko wrote:

I have managed to set up an auditor for the classRegistration entity as explained in the referenced thread DeepSave to create an ClassInvoice Entity. However would i be able to automatically create and persist an InvoiceRegisterEntity everytime an invoice is created?

Yes, you will have to create an Auditor of ClassInvoice (or just Class, as that is the parent) and create the InvoiceRegisterEntity in there.

David Elizondo | LLBLGen Support Team
kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 02-Feb-2010 10:04:51   

Hi David,

I have done just that. The ClassRegistrationEntity has an Auditor that creates a ClassInvoice. This works fine. My problem is that I have configured an auditor for the InvoiceEntity and assigned it in an overriden oncreateauditor method of the InvoiceEntity. I want this entity to create and invoiceregisterEntity for the invocieentity that is being persisted. However in the save process the AuditInsertOfNewEntity method in the invoice auditor does not get called. What do i do to get it called.

Kioko

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 02-Feb-2010 11:46:02   

Are you instantiating from ClassInvoice or from Invoice? Does the oncreateauditor get called?

Would you please provide simple code snippets of what you have done?

kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 02-Feb-2010 12:01:04   

Hi

I am instantiating a ClassInvoice and the oncreateauditor method for class invoice is being called but the AuditInsertOfNewEntity for the ClassInvoice auditor does not get called. What do you think is the problem

Kioko

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 02-Feb-2010 12:07:59   

I don't know. But if you supplied some code snippets, or better attach a solution, we can check this out for you.

kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 02-Feb-2010 16:40:15   

This is the Code


Partial Class ClassRegistration
        Protected Overrides Function CreateAuditor() As SD.LLBLGen.Pro.ORMSupportClasses.IAuditor
            Return New ClassRegistrationAuditor
        End Function
       '
       '
       ' Other Code
       '
       '
       '
End Class

Public Class ClassRegistrationAuditor
        Inherits AuditorBase
        Private _ClassInvoices As List(Of ClassInvoiceEntity)
        Sub New()
            _ClassInvoices = New List(Of ClassInvoiceEntity)()

        End Sub
        Public Overrides Sub AuditInsertOfNewEntity(ByVal entity As SD.LLBLGen.Pro.ORMSupportClasses.IEntityCore)

            Dim objInvoice As New ClassInvoiceEntity
            Dim objClassReg As ClassRegistrationEntity = DirectCast(entity, ClassRegistrationEntity)
            CreateClassRegistrationInvoice(objInvoice, objClassReg)
            objInvoice.ClassRegistration = objClassReg
            _ClassInvoices.Add(objInvoice)
            MyBase.AuditInsertOfNewEntity(entity)
        End Sub
        Public Overrides Function GetAuditEntitiesToSave() As System.Collections.IList
            Return _ClassInvoices
        End Function
        Public Overrides Sub TransactionCommitted()
            _ClassInvoices.Clear()
        End Sub
        Private Sub CreateClassRegistrationInvoice(ByRef objInvoice As ClassInvoiceEntity, ByVal objClassReg As ClassRegistrationEntity)
            objInvoice.InvoiceDate = System.DateTime.Today

            objInvoice.InvoiceAmount = 0D
            objInvoice.AccountReceivableId = 1001

            Dim objInvoiceLineItem As InvoiceItemEntity = objInvoice.InvoiceItem.AddNew
            objInvoiceLineItem.ItemId = 1
            objInvoiceLineItem.Quantity = 1
            objInvoiceLineItem.UnitPrice = 200
            objInvoiceLineItem.LineTotal = objInvoiceLineItem.Quantity * objInvoiceLineItem.UnitPrice
            objInvoice.InvoiceAmount = objInvoiceLineItem.LineTotal
        End Sub

    End Class

I have set up the InvoiceEntity and InvoiceAuditor in the same way as the one above but the the AuditInsertOfNewEntity does not fire for invoiceentities that are created in the method createclassregistration method in the invoice auditor

Kioko

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 03-Feb-2010 03:41:45   

Please post the code snippet for the ClassInvoice and its Auditor. It may be something different .

David Elizondo | LLBLGen Support Team
kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 03-Feb-2010 08:32:27   

Hi,

I have re-written my code and it still does not work.

The ClassRegistrationEntity and ClassRegistrationAuditor remain the same.

The following Enitities are as follows


    Partial Public Class ClassInvoiceEntity
        Protected Overrides Function CreateAuditor() As SD.LLBLGen.Pro.ORMSupportClasses.IAuditor
            Return New ClassInvoiceAuditor
        End Function
        'Other Code
End Sub

    Public Class ClassInvoiceAuditor
        Inherits InvoiceAuditor
        Public Sub New()
            MyBase.New()
        End Sub
        Public Overrides Sub AuditInsertOfNewEntity(ByVal entity As SD.LLBLGen.Pro.ORMSupportClasses.IEntityCore)
        'I would like to do some stuff here. Specifically Create an InvocieRegisterEnitity and set some variables specific to this ClassRegistration
            MyBase.AuditInsertOfNewEntity(entity)
        End Sub
        Public Overrides Sub TransactionCommitted()
            MyBase.TransactionCommitted()
        End Sub
        Public Overrides Function GetAuditEntitiesToSave() As System.Collections.IList
            Return MyBase.GetAuditEntitiesToSave()
        End Function
    End Class

    Partial Public Class InvoiceEntity
        Protected Overrides Function CreateAuditor() As SD.LLBLGen.Pro.ORMSupportClasses.IAuditor
            Return New InvoiceAuditor
        End Function
        'Other Code
    End Class
    Public Class InvoiceAuditor
        Inherits AuditorBase
        Private _InvoiceRegistrations As List(Of InvoiceRegisterEntity)
        Public Sub New()
            _InvoiceRegistrations= New List(Of InvoiceRegisterEntity)
        End Sub
        Public Overrides Function GetAuditEntitiesToSave() As System.Collections.IList
            Return _InvoiceRegistrations
        End Function
        Public Overrides Sub TransactionCommitted()
            _InvoiceRegistrations.Clear()
        End Sub
        Public Overrides Sub AuditInsertOfNewEntity(ByVal entity As SD.LLBLGen.Pro.ORMSupportClasses.IEntityCore)
            Dim objInvReg As New InvoiceRegisterEntity
            Dim objInvoice As InvoiceEntity = DirectCast(entity, InvoiceEntity)
            CreateInvoiceFinTransaction(objInvReg , objInvoice) 'Does the actuall creation of the invoiceRegisterEnitity
            objInvReg .Invoice = objInvoice
            _InvoiceRegistrations.Add(objFinTrans)
            MyBase.AuditInsertOfNewEntity(entity)
        End Sub

The change is that the ClassInvoiceAuditor Inherits from the InvoiceAuditor. The problem i have is that the AuditInsertOfNewEntity for both ClassInvoice and Invoice Auditors do not fire and hence i cannot run the routine to create the InvoiceRegister Entity

Kioko

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 03-Feb-2010 10:10:42   

And the code that performs the Inserts, please?

kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 03-Feb-2010 10:18:57   

Hi Walaa,

As at yet i have not put any code to do the inserts. I had put a breakpoint to determine that the code would actually stop here.

If you look in the ClassRegistrationAuditor i have code in the AuditInsertOfNewEntity methode that creates an invoice. My challenge is the AuditInsertOfNewEntity for the ClassInvoiceEntity and for the InvoiceEntity do not fire even when i have attached their respective validators in the CreateAuditor() for both classes. Furthermore when i debug the application auditor classes for both the InvoiceAuditor and ClassInvoiceAuditor are both being instantiated (Sub New() fires)

Kioko

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 03-Feb-2010 10:49:13   

I'm a little bit confused.

The call will go to AuditInsertOfNewEntity only when the respective entity is being Inserted. So you have to try it with some Insert/Save code.

kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 03-Feb-2010 11:40:23   

Let me explain.

I have a ClassRegistrationEntity. This class takes a StudentEntity and a ClassEntity and Registration date. When i save this entity I want to automatically create an Class Invoice for this ClassRegistration. To do this i implmented an Auditor for the ClassRegistration which does exactly this.

Now the ClassInvoice is a sub type of InvoiceEntity in a target per entity hierarchy This is because Classinvoices have a StudentID field in addition to the normal fields that the invoiceentity has. By putting code in the AuditInsertOfNewEntity() method of the ClassRegistragtionAuditor I have been able to successfully create the invoice.

I would also like to log everyinvoice created by the system be it a ClassInvoice or a normal invoice in a InvoiceRegister. I tried implementing this by creating a auditor for both the classINvoice and the Invoice Entities. this is where i face a challenge as the AuditInsertOfNewEntity for both these auditor classes do not get called for invoices that have been created in the ClassRegistrationAuditor AuditInsertOfNewEntity method. Is this a bug with LLBLGen or should i put this loging code somewhere else.

Kioko

kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 03-Feb-2010 13:25:21   

Hi Guys,

I seem to have been able to noarrow down the problem. The Auditor Mechanisim seems to be erratic it works sometimes and other times it does not work. It works fine for ClassRegistrationEntity but will not work for the InvoiceEnitity even when assign the respective auditor properly. Is there a known bug with the mechanisim?

Regards Kioko

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 03-Feb-2010 16:46:00   

this is where i face a challenge as the AuditInsertOfNewEntity for both these auditor classes do not get called for invoices that have been created in the ClassRegistrationAuditor AuditInsertOfNewEntity method. Is this a bug with LLBLGen or should i put this loging code somewhere else.

The question is does these enities get inserted in the database? Or you just create them in the ClassRegistration, but you have forgot to persist them. That's why the AuditEntityAfterInsert is not called.

kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 03-Feb-2010 22:14:41   

Hi Walaa

I have attached project to better explain the problem i am facing.

Kioko

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 04-Feb-2010 01:59:59   

There's no such attachment simple_smile

David Elizondo | LLBLGen Support Team
kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 05-Feb-2010 10:03:52   

Hi Guys

Here is the extra code. And attached is the project solution.

Kioko


Imports SD.LLBLGen.Pro.ORMSupportClasses
Imports ClassProject.Auditors
Namespace ClassProject.EntityClasses
    Partial Public Class ClassRegistrationEntity
        Protected Overrides Function CreateAuditor() As SD.LLBLGen.Pro.ORMSupportClasses.IAuditor
            Return New ClassRegistrationAuditor
        End Function
    End Class
End Namespace

Imports SD.LLBLGen.Pro.ORMSupportClasses
Imports ClassProject.Auditors
Namespace ClassProject.EntityClasses
    Partial Public Class ClassInvoiceEntity
        Protected Overrides Function CreateAuditor() As SD.LLBLGen.Pro.ORMSupportClasses.IAuditor
            Return New ClassInvoiceAuditor
        End Function
    End Class
End Namespace

Imports System.Collections.Generic
Imports ClassProject.EntityClasses
Imports SD.LLBLGen.Pro.ORMSupportClasses
Namespace ClassProject.Auditors
    Public Class ClassRegistrationAuditor
        Inherits AuditorBase
        Private _ClassInvoice As List(Of ClassInvoiceEntity)
        Sub New()
            _ClassInvoice = New List(Of ClassInvoiceEntity)

        End Sub
        Public Overrides Sub AuditInsertOfNewEntity(ByVal entity As SD.LLBLGen.Pro.ORMSupportClasses.IEntityCore) 'Runs when ClassRegistration.Save is called

            Dim objInvoice As New ClassInvoiceEntity
            Dim objClassReg As ClassRegistrationEntity = DirectCast(entity, ClassRegistrationEntity)
            CreateClassRegistrationInvoice(objInvoice, objClassReg)
            objInvoice.ClassRegistration = objClassReg
            _ClassInvoice.Add(objInvoice)
            MyBase.AuditInsertOfNewEntity(entity)
        End Sub
        Public Overrides Function GetAuditEntitiesToSave() As System.Collections.IList
            Return _ClassInvoice
        End Function
        Public Overrides Sub TransactionCommitted()
            _ClassInvoice.Clear()
        End Sub
        Private Sub CreateClassRegistrationInvoice(ByRef objInvoice As ClassInvoiceEntity, ByVal objClassReg As ClassRegistrationEntity)

            objInvoice.InvoiceDate = objClassReg.RegistrationDate

            objInvoice.InvoiceAmount = 0D
            Dim objInvoiceLineItem As InvoiceItemEntity = objInvoice.InvoiceItem.AddNew
            objInvoiceLineItem.ItemId = 2
            objInvoiceLineItem.Quantity = 1
            objInvoiceLineItem.UnitPrice = 5000
            objInvoiceLineItem.LineTotal = objInvoiceLineItem.Quantity * objInvoiceLineItem.UnitPrice
            objInvoice.InvoiceAmount += objInvoiceLineItem.LineTotal
        End Sub

    End Class
End Namespace

Imports System.Collections.Generic
Imports ClassProject.EntityClasses
Imports SD.LLBLGen.Pro.ORMSupportClasses
Namespace ClassProject.Auditors
    Public Class ClassInvoiceAuditor
        Inherits AuditorBase
        Private _InvoiceRegister As List(Of InvoiceRegisterEntity)
        Private mintMemberID As Integer
        Public Sub New()
            _InvoiceRegister = New List(Of InvoiceRegisterEntity)
        End Sub
        Public Overrides Sub AuditInsertOfNewEntity(ByVal entity As SD.LLBLGen.Pro.ORMSupportClasses.IEntityCore) 'Never gets called 
            Dim objInvoiceRegister As New InvoiceRegisterEntity
            Dim objInvoice As ClassInvoiceEntity = DirectCast(entity, ClassInvoiceEntity)
            CreateInvoiceRegister(objInvoiceRegister, objInvoice)
            objInvoiceRegister.Invoice = objInvoice
            _InvoiceRegister.Add(objInvoiceRegister)
            MyBase.AuditInsertOfNewEntity(entity)
        End Sub
        Public Overrides Function GetAuditEntitiesToSave() As System.Collections.IList
            Return _InvoiceRegister
        End Function
        Public Overrides Sub TransactionCommitted()
            _InvoiceRegister.Clear()
        End Sub
        Private Sub CreateInvoiceRegister(ByVal objInvoiceRegister As InvoiceRegisterEntity, ByVal objInvoice As ClassInvoiceEntity)

            Try
                objInvoiceRegister.InvoiceDate = objInvoice.InvoiceDate
                objInvoiceRegister.Student = objInvoice.ClassRegistration.Student

            Catch ex As Exception
                Throw ex
            End Try
        End Sub
    End Class
End Namespace

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 05-Feb-2010 11:07:33   

Would you please answer the following 2 questions:

1- Does ClassRegistrationAuditor.GetAuditEntitiesToSave() get called? 2- Do the ClassInvoice entities get saved in the database?

kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 05-Feb-2010 13:05:50   

Dear Walaa,

1- Does ClassRegistrationAuditor.GetAuditEntitiesToSave() get called?

Yes it does

2- Do the ClassInvoice entities get saved in the database?

No they don't

kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 05-Feb-2010 15:09:36   

Any Help.....

Its getting desperate.

kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 05-Feb-2010 16:32:05   

Hi Guys

On further examiniation i have realised that if i create a new ClassInvoice Entity and i try and save it the ClassInvoiceAuditor does not fail. Is there a bug in the Auditor in LLBLGen that i don't know about? I am using the latest version.

Kioko

1  /  2