PropertyInfo<Guid> ... = RegisterProperty(...) doesn't generate a new Guid

PropertyInfo<Guid> ... = RegisterProperty(...) doesn't generate a new Guid

Old forum URL: forums.lhotka.net/forums/t/5412.aspx


k2so posted on Tuesday, September 16, 2008

Hi,

using CSLA3.5, .Net3.5


In a child BO, I have the property definition as following:

        private static PropertyInfo<Guid> RowGuidProperty =
            RegisterProperty<Guid>(typeof(Document), new PropertyInfo<Guid>("RowGuid", "Row guid", Guid.NewGuid()));
        [System.ComponentModel.DataObjectField(true, true)]
        public Guid RowGuid
        {
            get
            {
                return GetProperty<Guid>(RowGuidProperty);
            }
        }

// in data access, I have a Child_Create() defined, with just a line of ValidationRules.CheckRules

1. Seems like the RegisterProperty line is only called once (at the first time the object is created, when a new object is created, it is not executed). It is generating the same Guid everytime a new child object is created Is this normal?
2. If 1. is true, how can I default a new Guid everytime a new child object is created, without calling the Guid.NewGuid() inside Child_Create()?

skagen00 replied on Tuesday, September 16, 2008

This is by design, actually - and I raised this similar issue before -

A similar circumstance where one might do a ChildCollection.NewCollection() as an initial value for a child property -- you could create two root objects and both would have a handle to the same "NewCollection" - because code isn't rerun - it just stores the result of the code as the default value.

So your problem is similar to this but not quite the same... 

What is similar is that the initial value of the property does not end up being the code that gets run each time for a created instance. Obviously, as you're discovering, the generated guid from Guid.NewGuid is getting stored as the default value for this property.

You'll just need to do this assignment in DP_Create rather than in registering the property.

Fintanv replied on Tuesday, September 16, 2008

1. Yes as it is static.

2. What about:

private static PropertyInfo<Guid> RowGuidProperty = RegisterProperty<Guid>(typeof(Document), new PropertyInfo<Guid>("RowGuid", "Row guid", Guid.Empty));

[System.ComponentModel.DataObjectField(true, true)]

public Guid RowGuid

{

get

{

if (ReadProperty<Guid>(RowGuidProperty) == Guid.Empty)

{

LoadProperty<Guid>(RowGuidProperty, Guid.NewGuid());

}

return GetProperty<Guid>(RowGuidProperty);

}

}

skagen00 replied on Tuesday, September 16, 2008

You may want to be careful about doing this here and rather put it in the DP_Create because you'll only get the guid established if the property is accessed.

Generally when accessing my properties for data access/etc I am using ReadProperty, and my Guid would not be generated.

Just my two cents

 

k2so replied on Tuesday, September 16, 2008

thx y'all for the insights!
that was quick :]

Copyright (c) Marimer LLC