Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Best way to Add New Records in Related BO/Table
Message
From
26/11/2010 14:51:27
 
 
To
25/11/2010 22:12:38
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
General information
Forum:
ASP.NET
Category:
The Mere Mortals .NET Framework
Miscellaneous
Thread ID:
01490394
Message ID:
01490608
Views:
29
>>>>>>>>>>Hi,
>>>>>>>>>>
>>>>>>>>>>using MM.NET 3.6 in an ASP.NET project with C#.
>>>>>>>>>>
>>>>>>>>>>I have a Receipt table, with a one to many relationship with a Receipt Details (Line Items) table. For particular types of line items, I need to generate commission records in the commissions table.
>>>>>>>>>>
>>>>>>>>>>What's the best way to handle this?
>>>>>>>>>
>>>>>>>>>Frank,
>>>>>>>>>Are you just trying to add records behind the scenes for the commissions? Can't you just instantiate the business object for commissions and add the records you need? I may not be understanding the full context of your question.
>>>>>>>>>Tim
>>>>>>>>
>>>>>>>>Yeah, I had been thinking about this post and thought I had not stated it clearly.
>>>>>>>>
>>>>>>>>I see this as being a business object thing, again I go back to having a business object manager that handles the relationships between the multiple business objects which is what I am used to in my VFP framework. So I would like a Receipt BO Manager that would have the Receipt BO as the main BO, ReceiptDetailBO as a child and CommissionsBO as another child. Then, whenever a "ReceiptDetailBO.AddRecord" takes place, a "CommissionBO.AddRecord" will get called if the correct receipt detail type has been added.
>>>>>>>>
>>>>>>>>Since I don't think MM has this concept of a BO Manager, I was looking at somehow implementing it as part of the ReceiptDetailBO, but have no idea where to start.
>>>>>>>>
>>>>>>>>I guess I can implement this all at the form level pretty easily, but it just doesn't feel right as it is a Business Process thing to my mind.
>>>>>>>
>>>>>>>Hi Frank,
>>>>>>>Your thinking is correct in that it really shouldn't be in the form. I honestly don't see any reason why it can't be in your receipt business object or another class you create in your business object library. There doesn't have to be only the regular business objects in that project you can create other classes that broker activity between business objects.
>>>>>>>That said, if this is part of the receipt process, then I don't see why the receipt business object cannot have a method such as SaveReceiptWithCommission() and you call this from your form. This method can save the rows of the receipt and also create an instance of the Commission business object to ask that biz obj to save rows of commission type.
>>>>>>>Tim
>>>>>>
>>>>>>Thanks Tim,
>>>>>>
>>>>>>I'm struggling to get started with this.
>>>>>>
>>>>>>I see this as something that should automatically be done when a DetailReceipt record gets saved. The logic should be something like:
>>>>>>
>>>>>>Form's save is called, which calls the ReceiptBO.Save(), which then calls the ReceiptDetail.Save(), now somewhere in that Save I need to check "is this ReceiptDetail a Premium Payment?" If it is, then I need to call the CommissionBO.AddRecord() (or whatever it is called) and update the entity values with the calculated commission, then call the CommissionBO.Save(). Of course, this should all be part of the same update transaction so that if the save at any level is unsuccessful, then the entire save is unsuccessful.
>>>>>>
>>>>>>Could you suggest what method I should be overriding to add this code in?
>>>>>
>>>>>Hi Frank,
>>>>>
>>>>>I would be happy to give you some good ideas on this when I get back home on Saturday. However if the Receipt information has everything that is needed to be able to create the commission record you could put this in a Hook Pre Save method where it would then call the Commission biz object passing in a copy of the values needed from the receipt entity. That would allow you to use the form save method as you are without overriding any of the actual save methods. If the receipt entity does not contain enough information to generate the commission record, then you may need to override the business objects save method that gets called by the form method. I can look at that closer when I get home if the first method isn't enough.
>>>>>
>>>>>If in your receipt business object you type override Hook... you should get intellisense letting you know which hook methods are available around the saves. You can put code in the pre or post save but the pre would provide better control over the save and would have the current entity available for information.
>>>>>Tim
>>>>
>>>>OK, it looks like my code needs to be in the HookPreSavEntityList()
>>>
>>>Frank, this is problematic as different HookPreSave methods fire depending on if a list or a single entity and it is different if using the Entity Framework. You may have to put a hook in more than one method to call your method of what your trying to do. That way you can be sure it gets captured regardless if there is a single record or a list of them.
>>>Tim
>>
>>Thanks Tim.
>>
>>Here's what my code looks like so far, but the records are not saving:
>>
>>
            // loop through the collection and if saving a receipt type of Premium
>>            // generate the commission records for this premium
>>
>>            // create a commissionpaid entity
>>            CommissionsPaid oCommissionsPaid = new CommissionsPaid();
>>
>>            // get the Policy details
>>            Policy oPolicy = new Policy();
>>            oPolicy.GetPolicyByPolicyHeaderFK(PolicyFK);
>>            
>>
>>            for (int i = 0; i < entityList.Count; i++)
>>            {
>>                if (entityList[i].ReceiptTypeFK == 3)   // premium payment
>>                {
>>                    // add a new record
>>                    oCommissionsPaid.NewEntity();
>>                    
>>                    // set the correct values on the record
>>                    oCommissionsPaid.Entity.ReceiptDetailFK = entityList[i].ReceiptDetailPK;
>>                    oCommissionsPaid.Entity.AgentFK = oPolicy.Entity.AgentFK;
>>                    oCommissionsPaid.Entity.Amount = 1;
>>
>>                    // save the record
>>                    oCommissionsPaid.SaveEntity();
>>                }
>>            }
>>
>>
>>Is this how I should approach it?
>
>At a glance it looks like an appropriate way to do it. You may want however for strong typing to loop through the actual entities though. A foreach loop on each entity in the entityList collection. Then you can refer directly to the entity.property. Not sure why it isn't saving unless the rules are being broken. You may want to wrap the save in a test of mmSaveDataResult so you can verify the rules are getting broken.
>Tim

Hi Tim,

I just realised that the HookPreSave... may not be the place I really want to do this. My code will run before the rules code gets run so if a rule fails after my code runs then I've got records added to the commissions table but nothing added to the receipt details.

Is there a place that I can run this code after all the rules have passed?
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform