Default value(s)

Posts   
1  /  2
 
    
Kodiak
User
Posts: 92
Joined: 13-Apr-2009
# Posted on: 23-Apr-2009 13:56:31   

Got it: updated entityUsingEntityBase.template to also include

       <[If IsAbstract]>Friend<[Else]>Public<[EndIf]> Sub New(ByVal fields As IEntityFields)
            MyBase.New()
<[If Not IsSubType]>            InitClassEmpty(fields, Nothing)<[EndIf]>
        End Sub
MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 23-Apr-2009 14:00:20   

Thanks for updating.

Matt

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 23-Apr-2009 14:07:02   

Hi Kodiak,

This was the first time in 2 years I have ever touched ANY templates in LLBLGen Pro, even after very extensive use of the framework. Believe me, for everyday use - you do not need to modify any of the templates.

The "proper" way to set Default values on your entities is to create a Manager class for each Entity type. Manager classes, as well as your inherited / partial Entity classes, becomes your business layer. Each Manager class should use a the factory pattern (ie, CustomerManager.Create) which returns a new Customer Entity (complete with Default values).

This was a special case for me and I primarily just wanted to find the root of the problem. So, Hopefully you won't have to mess with any of this. Go for the Manager classes.

Hope this helps!

Ryan D. Hatch

PS. I believe there is a Task to generate the Manager classes automatically, which you can download from the customer area of the website.

Kodiak
User
Posts: 92
Joined: 13-Apr-2009
# Posted on: 23-Apr-2009 17:24:25   

Hi Ryan,

Thanks for your comments. For some reason the entities are still coming back as "New" when the collection is fetched so I'll probably have to move to a manager class. I'm not sure about an extra class and methods for something that seems so easy to have inside a constructor in the class itself.

What other methods do you generally place inside a Manager Class?

In a Customer has Orders scenario would you have an Order Manager and Customer Manager? Order Manager could have things like CreateOrderForCustomer(Customer as CustomerEntity) as Order Entity.

Would you also put soft deletes in a manager class?

Generally when you have filtered lists are they still inside your entity? Eg. Active and Closed Orders for a Customer. The Orders list would return both so would you filter them down in the entity and return a collection from there?

I guess I'm trying to work out what best fits where confused

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 23-Apr-2009 18:11:39   

Hi Kodiak -

Glad to help... I'm going to answer this in 2 different replies:

1.) When using the Default Values method described in this thread - You shouldn't be using the New() constructor to set default values. Instead, override OnInitialized(). See if that works for you.

2.) Manager Class. I am just about to start using these myself. I've got along just fine for the past 2 years by just using the Entities themselves for my business logic. Although, much of my other business logic is in the code-behind of my ASP.NET pages. Much of which should move to the Manager Classes. Things like fetching & filtering certain entities, saving, deleting, etc.

For instance, if you wanted to get the most recent Order for a Customer... you could put this a couple places (up to you). You could put this fetch in the CustomerEntity (ie, CustomerEntity.GetLastOrder() as OrderEntity). Or you could put it in the Manager class, ie. OrderManager.GetLastOrder(Customer as CustomerEntity).

For sure what I'm going to need to move to Manager classes are Saving & Deleting. This should not be in my code-behind. For instance, OrderManager.Save(myOrder) & OrderManager.Delete(myOrder). These belong in the Manager class because saving often involves touching multiple entities & tying them all together in the same transaction, as does delete... which sometimes requires cascade deletes or needs to validate something else before deleting.

3.) Soft deletes (ie, .IsDeleted) is not recommended. Frans recently posted a good article on alternatives to this practice.

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 23-Apr-2009 18:34:36   

Ok, now for your real question:

How to set the default values for an OrderEntity, when creating a new Order for a Customer?

Couple options:

*** OrderManager.Create(Customer as CustomerEntity) as OrderEntity** This will be easy to set default values. Although, I do not prefer this method because I find using the Manager class like this unnatural. I find it natural to begin with the CustomerEntity, and create the Order from it.

*** CustomerEntity.Orders.AddNew()** I like this method - You can use your CustomerEntity to create the OrderEntity. However, this is interesting for default values. As was described already in this thread - You can could handle this by overriding OrderEntity.OnInitialized(). And perhaps this is the best way to do it, because this event will fire automatically.

Anyone else have any advice?

Ryan

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 06-May-2009 19:37:27   

Sorry, wrong thread.

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 06-May-2009 19:39:15   

Ok, I've made a fix to my modified Template - added support for Inheritance:

entity.Template


                ''' <summary>CTor</summary>
                ''' EDIT: by Ryan D. Hatch, 2009.05.06 - Bug fix: http://llblgen.com/tinyforum/Messages.aspx?ThreadID=15323&HighLight=1
        <[If IsAbstract]>Friend<[Else]>Public<[EndIf]> Sub New(ByVal fields As IEntityFields)
<[If IsSubType]>
            MyBase.New(fields)
<[Else]>
            MyBase.New()
            InitClassEmpty(fields, Nothing)
<[EndIf]>
        End Sub

Hope this helps!

Ryan

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 06-May-2009 20:03:37   

I thought I was going crazy.

I was reading a post on Page 1 of this thread, and clicked "New Message" to reply... and then I got really confused because I saw my reply on what appeared to be a different thread.

Turns out this thread had multiple pages & I didn't know it. I got pushed to Page 2, which looked totally different to me.

Gee wiz, talk about confusing a guy! smile

Ryan

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 07-May-2009 06:11:00   

rdhatch wrote:

I thought I was going crazy.

I was reading a post on Page 1 of this thread, and clicked "New Message" to reply... and then I got really confused because I saw my reply on what appeared to be a different thread.

Turns out this thread had multiple pages & I didn't know it. I got pushed to Page 2, which looked totally different to me.

Gee wiz, talk about confusing a guy! smile

smile ... Thanks for updating.

David Elizondo | LLBLGen Support Team
rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 19-May-2009 02:34:49   

Hi Frans, David & Walaa -

FYI - There is one remaining problem with Default Values in OnInitialized(). The Template fixes above worked great.

The Problem is EntityView CreateDummyInstance method.

When it's loading existing entities, it is calling New() constructor - instead of the new New(Fields) constructor. Because of this, existing Entities in Views are calling OnInitialized() with .isNew = True. Thus, default values & default child entities are being assigned to the existing Entities.

I can't fix this because it's in ORMSupportClasses, not in the Templates.

Frans - Please make note of this for v3. Since we'll have a New(Fields) constructor, please have EntityView CreateDummyInstance call the correct constructor when populating an existing entity.

Much Thanks!!

Ryan

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 19-May-2009 10:50:54   

One way to achieve that is that you create a subclass of EntityView2<T> (or EntityView<T> if you're on selfservicing) and override that method (it's protected virtual) and call the right factory method. The only drawback there is then that you have to create your own view instances instead of relying on DefaultView. Not sure if this workaround will work for you.

The method is only used for property descriptor usage, so I don't see any problems with that method at all in the way it is now: the entity created is never used to store data.

Frans Bouma | Lead developer LLBLGen Pro
1  /  2