Default Values

Posts   
 
    
edalzell
User
Posts: 44
Joined: 27-May-2008
# Posted on: 02-Jun-2008 21:37:14   

Is there any way to have the Enity objects create default values when they are added to the DB?

DvK
User
Posts: 318
Joined: 22-Mar-2006
# Posted on: 02-Jun-2008 21:50:09   

You could set those "default" values before the insert event is fired for the to-be-inserted entity ?!

edalzell
User
Posts: 44
Joined: 27-May-2008
# Posted on: 02-Jun-2008 21:52:34   

DvK wrote:

You could set those "default" values before the insert event is fired for the to-be-inserted entity ?!

Yes, I could, but I was hoping to have something more along the lines of: 1) set the default values (i.e. createdDate = DateTime.Now) 2) create the entity 3) the default values get set magically

Otherwise I either have to check for IsNew or check each default field for null and then set it.

I was just hoping for something easier.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 03-Jun-2008 07:31:10   

Hi edalzell, three things come to my head:

  1. You could write some custom code at the entity initialization routine:
namespace Northwind.EntityClasses
{
    public partial class CustomerEntity
    {
        protected override void OnInitialized()
        {
            if (this.IsNew) {
                 this.CreatedDate = DateTime.Now();
            }

            base.OnInitialized();
        }
    }
}
  1. Use Auditors. In this case, if you want to store the creation date, an auditor fit there. public class GeneralAuditor : AuditorBase
{

    public override void AuditInsertOfNewEntity(IEntityCore entity)
    {
        ((SomeEntity) entity).CreationDate = DateTime.Now();
    }

     ...
}

(for more info about Auditing, see LLBLGenPro Help -> Using the generated code -> Setting up and using Auditing)

  1. You also could use Validators:
public class CustomerValidator : ValidatorBase
{
    public override void ValidateEntityBeforeSave( IEntityCore involvedEntity )
    {
                // .... some validations

                // set the creation date
                ((SomeEntity) involvedEntity).CreationDate = DateTime.Now();


        base.ValidateEntityBeforeSave( involvedEntity );
    }
}

(for more info about Validation, see LLBLGenPro Help -> Using the generated code -> Validating per field or per entity

As you are setting a CreationDate field (in your example), I would recommend go for Auditing. You only need to set that once, then every time you save a new SomeEntity the CreationDate would be filled.

Hope helpful wink

David Elizondo | LLBLGen Support Team
edalzell
User
Posts: 44
Joined: 27-May-2008
# Posted on: 10-Jun-2008 22:50:46   

If there are values I need to pass in, am I better to override the Constructor?

Thanks

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 11-Jun-2008 10:25:43   

If you need to pass values, then these wouldn't be default values, would they?

Normally I'd use ValidateEntityBeforeSave for such things, but if the values are needed to be passed in, then you might override the CTor as you have suggested.

edalzell
User
Posts: 44
Joined: 27-May-2008
# Posted on: 01-Sep-2008 20:31:41   

I was trying to use the Auditing method to do this, but it seems that the OnAuditInsertOfNewEntity got called after it was saved to the DB (and therefore I still got my error).

Any ideas?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 02-Sep-2008 06:48:41   

Mmm. You're right (my oversight, sorry flushed ): The entity just has been saved (not transaction commited though). That method is useful when you create a new entity (AuditInfo, for instance) for auditing purposes.

Instead you should should use either **OnValidateEntityBeforeSave **or OnInitialized.

David Elizondo | LLBLGen Support Team