DataAnnotations via Metadatatype ?

Posts   
 
    
happyfirst
User
Posts: 215
Joined: 28-Nov-2008
# Posted on: 03-Mar-2011 00:01:01   

I was trying to add DataAnnotations to various properties using the metadatatype attribute

http://msdn.microsoft.com/en-us/library/cc679243.aspx

but it doesn't seem to work. I would expect that using reflection on that property, GetCustomAttributes would return them. But it always returns 0 custom attributes.

object[] customAttributes = entityPropertyInfo.GetCustomAttributes(true);

Also, if I was to choose to use your designer features for adding annotations such as described in this post

http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=17054&HighLight=1

where in the documentation do I find all the various fields I have access to like $length ?

happyfirst
User
Posts: 215
Joined: 28-Nov-2008
# Posted on: 03-Mar-2011 00:30:22   

I finally figured out how to get the custom attributes from the metadatatype.

                object[] metadataTypes = businessObjectEntityType.GetCustomAttributes(typeof(MetadataTypeAttribute), true);
                MetadataTypeAttribute metadata = (MetadataTypeAttribute) metadataTypes[0];
                if (metadata != null)
                {
                        PropertyInfo[] properties = metadata.MetadataClassType.GetProperties();
                        foreach (PropertyInfo propertyInfo in properties)
                        {
                                object[] ca=propertyInfo.GetCustomAttributes(true);
                        }
                }

Still, where do I find documentation on all those variables like $length should I try to go that route instead?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 03-Mar-2011 04:03:12   

happyfirst wrote:

Still, where do I find documentation on all those variables like $length should I try to go that route instead?

It's in the docs

David Elizondo | LLBLGen Support Team
happyfirst
User
Posts: 215
Joined: 28-Nov-2008
# Posted on: 03-Mar-2011 13:54:45   

Thanks!

How for example would I be able to add a [Required] attribute to the necessary fields, based on the field not allowing nulls?

What would you recommend, modify templates or use the feature in the designer to inject the attribute?

What's an example of what I would need to type into the template or designer to get this attribute added to the necessary fields?

Is there a minlength property in LLBL Designer?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 04-Mar-2011 07:06:19   

I see. You want conditional attribute generation based on some rules. You can do two things:

a) Set the Attribute on every field you want in LLBLGen Designer. I know, this can be hard. However you can create a plugin that set those attributes for you based on some entity rules you code in your plugin.

b) Create your own template. This would generate partial classes for your entities. Here is example code for a guy who wanted to generate buddy classes to generate custom DataAnnotation for Scaffold.

David Elizondo | LLBLGen Support Team