Home
Help
Register
Log in

Search

 
   Active Threads  

You are here: Home > LLBLGen Pro > LLBLGen Pro Runtime Framework> Where to add code to set default values for new entities
 

Pages: 1
LLBLGen Pro Runtime Framework
Where to add code to set default values for new entities
Page:1/1 

  Print all messages in this thread  
Poster Message
ww
User



Location:

Joined on:
01-Oct-2004 22:58:25
Posted:
65 posts
# Posted on: 21-Jul-2016 02:52:09.  
I'm using the LLBLGEN framework, migrating from version 4.1 to 5.0.

When a new entity is created, I need to set default values for many of the fields. In the past I did this by modifying the standard templates to add a call to an initialization routine that gets called in all constructors except the one that takes an IEntityFields2 and the one that takes a SerializationInfo.

I'd like to avoid modifying the templates, but I'm not sure where to hook in for this. Is there a method I can override to set defaults only when the entity isn't being fetched or deserialized?
  Top
daelmo
Support Team



Location:
Guatemala City
Joined on:
28-Nov-2005 23:35:24
Posted:
8071 posts
# Posted on: 21-Jul-2016 07:12:41.  
You could override OnInitialized on a partial class. Example:
Code:
public partial class CustomerEntity
{
    protected override void OnInitialized()
    {
        this.City = "XYZ";
    }
}


Tests:
Code:
// new
var customer = new CustomerEntity();
Assert.AreEqual("XYZ", customer.City);

// fetched
customer = new CustomerEntity("ALFKI");
using (var adatper = new DataAccessAdapter())
{
    adatper.FetchEntity(customer);

}

Assert.IsFalse(customer.IsNew);
Assert.AreNotEqual("XYZ", customer.City);


For more info see Tapping into actions on entities and collections.


David Elizondo
LLBLGen'ing (articles and code snippets) | linkedin | twitter
 
Top
ww
User



Location:

Joined on:
01-Oct-2004 22:58:25
Posted:
65 posts
# Posted on: 21-Jul-2016 14:50:01.  
I failed to mention I'm using the Adapter scenario.

From looking at the code I was thinking that OnInitialized gets called after field values have been set from the database. Isn't that what the fields parameter to InitClassEmpty is doing? So setting defaults in OnInitialized would override the db values.

The solution I ended up with is a Custom_EntityInitializationTemplate that calls a method to set default values only when the fields parameter is null.
  Top
Walaa
Support Team



Location:

Joined on:
21-Aug-2005 16:03:48
Posted:
14484 posts
# Posted on: 21-Jul-2016 19:08:25.  
Well you have OnInitializing and OnInitialized which are called at the start / end resp. of InitClassEmpty.

And even in OnInitialized, you can test whether the entity is new and this.Fields.State == EntityState.New before setting fields to values.


  Top
ww
User



Location:

Joined on:
01-Oct-2004 22:58:25
Posted:
65 posts
# Posted on: 21-Jul-2016 21:25:01.  
Thanks. I'll take a look at OnInitializing and also Fields.State.
  Top
Pages: 1  


Powered by HnD ©2002-2007 Solutions Design
HnD uses LLBLGen Pro

Version: 2.1.12172008 Final.