where to put initializing code

Posts   
 
    
leon
User
Posts: 9
Joined: 23-Dec-2010
# Posted on: 18-Jul-2011 13:26:00   

Hi,

I want to be sure that every Master-record has 30 Children, so I want to generate those Children after inserting a Master-record. And it should be failsave, so never mind the graph used, allways be sure there are exactly 30 children. So I tried overriding some events in my partial class, but it does not work ( does not generate an error either, haha )

Where should I put that code ?

Leon

   protected override void OnValidateEntityBeforeSave()
    {


        if (IsNew)
        {
            myChildEntity w = null;
            for (int i = 0; i < 20; i++)
            {

                w = new myChildEntity();
                w.birth = 1900 + i;
                w.Naam = "birth:"+i.ToString();
                w.Id = Guid.NewGuid();
                this.myChildren.Add(w);
            }
        }


        base.OnValidateEntityBeforeSave();
    }
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-Jul-2011 14:59:54   

Please check Validation documentation

But in general I recommend to have this in the business layer code as a business rule and not inside the validation code.

P.S. take care your loop counts till 20 not 30. simple_smile

leon
User
Posts: 9
Joined: 23-Dec-2010
# Posted on: 18-Jul-2011 15:35:20   

Walaa,

I did read the documentation on validation, and based on that this seemed to be the best place to interact. So, could you please give me a more detailed response, haha ?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-Jul-2011 15:45:44   

So I tried overriding some events in my partial class, but it does not work

Ok, please explain in more details what did you do, which events did you override and what do you mean by "it does not work"?

leon
User
Posts: 9
Joined: 23-Dec-2010
# Posted on: 18-Jul-2011 15:59:02   

Walaa:

I have a console application, inserting a Master ( gemeente ).

static void Main(string[] args) {

        using (DataAccessAdapter adapter = new DataAccessAdapter())
        {
            GemeenteEntity g = new GemeenteEntity();
            g.Id = Guid.NewGuid();
            g.Naam = "Roelofsarendsveen";
            adapter.SaveEntity(g);
        }

}

I'm looking for an event that inserts 30 roads in that master ( gemeente ), as early as possible in the process, but at last in the "onvalidateentitybeforesave" event ( from gemeenteentity).

When I execute my code, the onvalidateentitybeforesave method fires, it seems the wegentity records are inserted, there is no error message, but when I check there are no wegrecords inserted at all. So apparently the new entered records are not recognised by SaveEntity(g).

protected override void OnValidateEntityBeforeSave() {

        if (IsNew)
        {
            WegEntity w = null;
            for (int i = 0; i < 30; i++)
            {

                w = new WegEntity();
                w.AanlegJaar = 1900 + i;
                w.Naam = "aanlegjaar:"+i.ToString();
                w.Id = Guid.NewGuid();
                this.Wegs.Add(w);
            }

        }
        base.OnValidateEntityBeforeSave();

}

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-Jul-2011 17:16:15   

Alright.

Then back to my recommendation: The thing is that validation is done too late in the process, when the persistence queue has been already calculated.

So anything more entities you are trying to save in the same persistence queue won;t get added.

The following thread discusses this issue. http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=13176

So the best thing to do is to have a business method for this. Which accepts the entity to be saved, adds related entities and use an adapter to save the entire graph.