This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Just like most people we use forms to enter the data into the values of entity fields. We usually perform this task using a BindingSource. The problem we have, is that when the entity type is String, it does not limit the length of the input to match the database field. This causes a problem, if for example a database accepts 5 characters, but we don;t set the max length of the text bax to be 5 characters also. Whilst the entity will flag a not valid error, it would be better, if we could just limit the entry in the first place. The BindingSource has the ability to map textbox properties to entity fields, so this would be th eidea mechansim to do this. A very old, pre .net orm type tool I used just had a "public const int" property that was the length of string fields. Whilst I am aware, that I could create this manually in the partial class, it would be good, if we could just modify the template in some way that is could do this automatically. So my question is, can we modify the templates to create these properties?and if so, how? Thanks |
|
|
The information is certainly available for you to modify the templates to do this, but I'm not sure if the NVelocity Template Language is up to the job. The problem is that you need to use generics: you need to test $field.HasAttribute<ValidateLengthAttributeModel> and if that returns true, emit something like: public const int Max${field.Name}Length = $field.GetAttribute<ValidateLengthAttributeModel>().Maximum; But I'm not sure if NVelocity supports that C# style generic syntax -- give it a try, or have a look at the NVelocity or VTL (Velocity Template Language) docs to see if there is an alternative syntax. Failing that you could fall back on iterating the $field.Attributes collection and testing GetType().Name: #foreach ($attribute in $field.Attributes) The easiest place to add this would probably be in Fields.vm or FieldNames.vm as both of those already contain loops over the fields collection. See http://www.mindscape.co.nz/blog/index.php/2009/09/16/customising-lightspeed-entity-templates/ for general info about modifying the designer templates. |
|
|
Note also you can get the info at runtime using Reflection without modifying the templates: public static int? GetMaxLength(Entity entity, string propertyName) { (Code written in forums editor so may contain typoes or other egregious errors, but hopefully gives you enough of a pointer that you can fix it up!) |
|
|
This is exactly what I was looking for. But have a little problem, I coied the templates to a project folder, like the link suggests, however, I do not have the option mentiond to set the template? I am using VS2010 on Vista x64, but there is not Lightspeed group in the project properties? Is this in a different location on VS2010? Thanks |
|
|
No, it's still be in the Properties window just like in VS2008. However, you may find that you need to open a .lsmodel file (and then click back on the csproj in Solution Explorer) in order to get it to appear. We also had a bug at one point where it didn't appear if the .lsmodel file was in a project folder rather than the project root. I'm pretty sure we've fixed that, but if your .lsmodel file is in a sub-folder then it might be worth a go. |
|
|
That did it, must open model first. Thanks |
|