Decorating Entities with Attributes

Posts   
 
    
Vitor
User
Posts: 9
Joined: 29-Oct-2005
# Posted on: 29-Oct-2005 22:47:52   

I'm in the beginning stages of designing a framework for handling my domain business/validation logic and I am considering using custom attributes. Am I able to decorate my entity properties with attributes so that when I re-gen my code, I do not lose my changes?

Thanks

Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 30-Oct-2005 09:53:06   

Vitor wrote:

I'm in the beginning stages of designing a framework for handling my domain business/validation logic and I am considering using custom attributes. Am I able to decorate my entity properties with attributes so that when I re-gen my code, I do not lose my changes?

I don't recommend that you mess with the LLBLGen templates directly because when a new release comes out it will be a pain to upgrade without having to manually merge your changes with the new template sets.

You could always inhertit from the Entity class, override the properties and add your attributes there.... This can even be automated by creating a new custom template which will generate the sub classes alongside the LLBLGen classes.

Maybe Frans will have a better suggestion.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 30-Oct-2005 10:09:49   

Vitor wrote:

I'm in the beginning stages of designing a framework for handling my domain business/validation logic and I am considering using custom attributes. Am I able to decorate my entity properties with attributes so that when I re-gen my code, I do not lose my changes? Thanks

Not with attributes, you should look in include templates. Write your code, then make templates which are bound to the include template ID's for entities. See the documentation on ''Adding your own code to the generated classes".

Another way is to add the code to the usercoderegions in the generated classes. These regions are preserved.

Frans Bouma | Lead developer LLBLGen Pro
Vitor
User
Posts: 9
Joined: 29-Oct-2005
# Posted on: 30-Oct-2005 14:42:35   

Thank you both.

Vitor
User
Posts: 9
Joined: 29-Oct-2005
# Posted on: 31-Oct-2005 00:00:31   

Frans, I am having trouble using code regions with attributes in VB.NET. I believe it has to do with VB.NET needing a line continuation character. Am I missing something here?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 31-Oct-2005 09:14:41   

Vitor wrote:

Frans, I am having trouble using code regions with attributes in VB.NET. I believe it has to do with VB.NET needing a line continuation character. Am I missing something here?

Could you give an example, please?

Frans Bouma | Lead developer LLBLGen Pro
Vitor
User
Posts: 9
Joined: 29-Oct-2005
# Posted on: 31-Oct-2005 14:43:29   

' __LLBLGENPRO_USER_CODE_REGION_START AdditionalInterfaces
<Serializable()> _
' __LLBLGENPRO_USER_CODE_REGION_END 
Public Property ID() As Int32
...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 31-Oct-2005 15:41:56   

Vitor wrote:


' __LLBLGENPRO_USER_CODE_REGION_START AdditionalInterfaces
<Serializable()> _
' __LLBLGENPRO_USER_CODE_REGION_END 
Public Property ID() As Int32
...

Ok, but aren't all classes already serializable, due to the fact they implement ISerializable below the surface ? simple_smile

So, my next question then is: what would you like to achieve exactly, so I can give you pointers where to place which code.

Frans Bouma | Lead developer LLBLGen Pro
Vitor
User
Posts: 9
Joined: 29-Oct-2005
# Posted on: 31-Oct-2005 16:41:59   

Otis wrote:

Vitor wrote:


' __LLBLGENPRO_USER_CODE_REGION_START AdditionalInterfaces
<Serializable()> _
' __LLBLGENPRO_USER_CODE_REGION_END 
Public Property ID() As Int32
...

Ok, but aren't all classes already serializable, due to the fact they implement ISerializable below the surface ? simple_smile

So, my next question then is: what would you like to achieve exactly, so I can give you pointers where to place which code.

I just used the Serializable attribute as an example. I created some custom attributes and I need to apply them to my properties, but I am having trouble b/c vb.net is complaining about the syntax. Just try applying any attribute to a property in vb.net with the custom code regions.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 31-Oct-2005 17:43:23   

Ok, so you want to apply an attribute to a generated property? That's indeed not supported. Either override the property in a derived class or create a surrogate property which returns the value of the generated property and apply the attribute there.

Frans Bouma | Lead developer LLBLGen Pro
Vitor
User
Posts: 9
Joined: 29-Oct-2005
# Posted on: 31-Oct-2005 20:33:07   

Ok thanks. It seems the above code works fine for C#, just not VB. flushed confused

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 01-Nov-2005 11:26:25   

Vitor wrote:

Ok thanks. It seems the above code works fine for C#, just not VB. flushed confused

Correct, as vb.net as you stated, requires direct connection between teh attribute line and the code line... I'm sorry.

If the properties are generated by llblgen pro, you can't apply the attributes to them through the usercode region. Though even then, the usercode regions specified in the class now, don't offer you to specify attributes on ALL generated properties. The example you gave is not very helpful in this, sorry.

If the properties are written by you, you should be able to add the attributes to the properties. That's why I'm confused why it doesn't work.

Could you please paste REAL code, and please specify where exactly you want to apply the custom attributes. The small snippet you gave is very confusing as it shows the 'AdditionalInterfaces' region, which has nothing to do with properties

Frans Bouma | Lead developer LLBLGen Pro
Vitor
User
Posts: 9
Joined: 29-Oct-2005
# Posted on: 01-Nov-2005 14:15:16   

I've created some custom attributes that I want to apply to some properties (generated by LLBL), for example, PolicyNumber is required. (Note I just added in the CustomAttributes region name b/c I didn't know what kind to use)



' __LLBLGENPRO_USER_CODE_REGION_START CustomAttributes
<Required("Policy # is required.")> _
' __LLBLGENPRO_USER_CODE_REGION_END 
Public Property PolicyNumber() As System.Int32
Get
Dim valueToReturn As Object = MyBase.GetCurrentFieldValue(CType(AC3FieldIndex.PolicyNumber, Integer))
    If valueToReturn Is Nothing Then
        valueToReturn = TypeDefaultValue.GetDefaultValue(GetType(System.Int64))
    End If
    Return CType(valueToReturn, System.Int64)
End Get
Set
    If SetNewFieldValue(CType(AC3FieldIndex.PolicyNumber, Integer), value) Then
        OnPolicyNumberChanged()
    End If
End Set
End Property

Are you saying using (in VB.NET) when I regenerate my code, even with the code regions, I'm still going to lose my changes? If so, I do not understand why? I thought the whole purpose of the code regions was to avoid losing your code.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 01-Nov-2005 14:53:15   

The regions have to be defined in the template. You can't 'decorate' a piece of code with a usercode region. The template is parsed, a region definition is found there, the region's name is loaded from the existing code and that region's contents is copied to the generated code, at the spot of the region in the template.

Frans Bouma | Lead developer LLBLGen Pro
Vitor
User
Posts: 9
Joined: 29-Oct-2005
# Posted on: 01-Nov-2005 15:40:20   

Ok, now I see what you are referring to. Is there any way around this besides using the derived templates? I already have alot of production code out there. Could there be another region added possibly? simple_smile

Actually, I'll just use the surrogate approach you mentioned.

Thanks again!