Customizing data annotation attributes for ASP.NET MVC - asp.net

I would like to customize at runtime the attributes that MVC sees on a view model property. As far as I know, MVC relies internally on type descriptors to enumerate the attributes. Is there a way to hook a type descriptor somewhere to return a custom list of attributes for a property?

Is there a way to hook a type descriptor somewhere to return a custom
list of attributes for a property?
It depends. If you want to override the Data Annotations used by the metadata provider then you could write your own custom ModelMetadataProvider and replace the default one (DataAnnotationsModelMetadataProvider). This allows you to have a custom metadata provider for a given type and return this information at runtime.
If on the other hand you are doing validation, then you are a bit out of luck. For more flexibility I would recommend you using FluentValidation.NET instead of data annotations.

Related

What is the point of #WebInitParam?

#WebInitParam is an annotation that goes at class level.
It defines initialization parameters for the servlet.
I would like to know, what is the difference between doing this and using static variables, and why do it the #WebInitParam way rather than using statics?
What does defining k/v pairs as #WebInitParams allow you to do that you couldn't do if you declared static variables instead?
I have looked and all I can find is a million people saying how to define #WebInitParams. Well yes that's the easy bit. It's what I can do with that that is really what is of interest.
Thanks very much.
From a "raison d'etre" perspective, the annotation exists as a better design and architecture alternative to just peppering a class with static fields. #WebInitParam is a self-documenting approach to the initialization parameters a servlet or filter class needs, available via web.xml as well. It serves this purpose for the end developers as well as the JavaEE platform as a whole.
Think about it this way: in a vanilla project, you have the option of hardcoding a bunch of parameters in the class as static fields, or defining the same parameters in a property file. Which would you pick? Under what circumstances?
From a purely functional angle, apart from the function of using the annotation to override defaults set in web.xml, one other major reason is that you will sometimes need to get a hold of those initialization parameters from another component in your application. Using the annotation essentially increases the visibility of the parameter. In JSF for example, the API allows you to retrieve FacesServlet initialization parameters with:
//get all the parameters
FacesContext.getCurrentInstance().getExternalContext().getInitParameterMap()
//get a specific parameter
FacesContext.getCurrentInstance().getExternalContext().getInitParameter("aParameter");
In JSF-2.3 , it gets even more convenient with the following CDI-enabled injection:
#InitParameterMap Map<String,String> servletParameterMap;
Bear in mind that because it's CDI, it means this facility is available throughout the JavaEE platform, not just in web applications/JSF.
It's going to be a hassle to retrieve init parameters if the only mechanism available is a static field in the servlet class - you'll need to obtain an instance of the filter or servlet to get the static fields in it.
Separately, one could make the argument that maybe one should favour context-params over servlet-params because then, you get even more flexibility that isn't tied to any given servlet. That's a separate matter entirely :)

Why is ViewBag called ViewBag?

In asp.net mvc, why is ViewBag called ViewBag?
I'm looking for the history or reason why it's called ViewBag over some other name.
ViewBag is a dynamic mapping of the ViewData dictionary. It's called a "bag" because there's no order or sequence to it.. it's just a bunch of data accessible from a dynamic property, much like if you had a bag of stuff.
The underlying ViewData has order to it, but when it's mapped to the dynamic collection it loses that order.. thus it's a bag.
See a definition here:
http://www.cs.miami.edu/~geoff/Courses/MTH517-00S/Content/ArrayBasedADTs/BagsStacksQueues.html
Its a bag full of information which is made available to the view.
It enables you to dynamically share values from the controller to the view. It is a dynamic object which means it has no pre-defined properties. You define the properties you want the ViewBag to have by simply adding them to the property. In the view, you retrieve those values by using same name for the property.

ASP.NET MVC3 Generate View Model including validation attributes

I am wondering if there is a way to generate a View Model from database table including validation (required,length,datatype). Of course I will edit it and enhance it.Otherwise it is a waste as I have to type in everything
We are implementing a "Dynamic" approach to validation in one of our projects where the validation rules are encoded in a database table and we override GetValidators() in a custom implementation of DataAnnotationsModelValidatorProvider to inject custom and/or standard validation attributes. These can then be propagated to the client if we want them to. As for creating a viewModel from the existing database, you can use t4 files to read the db schema and generate classes for you and inject validation attributes on the model as it is being created.

asp.net Profile Provider Search on specific property

I am using the SqlProfileProvider class in one of my projects and I would like to be able to search for profiles on an arbitrary profile property (e.g. Birthday).
The only search method that I have seen is the ProfileManager.FindByUserName.
Has anyone implemented that? Or do I have to iterate over the list of profiles (very inefficient) and filter?
If you're going to only use the provider mechanisms, your only option is to iterate over the profiles.
However, you can certainly role your own method to search on other properties. Instead of calling ProfileManager.FindByBirthday, you'll just call your method directly off of your custom class.

Grails date binding

In Grails, one can 'bindData' in controller:
Book b = new Book()
bindData(b, params)
What if I have a date field with specific format (e.g. yyyy-MM-dd) from user input? In Spring, we can use registerCustomEditor(). How about Grails?
With Grails 1.1.1, you can implement a PropertyEditorRegistrar and use that to specify a format. See http://grails.1312388.n4.nabble.com/Grails-1-1-1-change-in-binding-date-properties-td1323105.html
Have you already come across the Extended Data Binding Plugin?
From the documentation on the site, it appears to offer both aspects which you are referring to
Allow customization of the DataBinder that will be used to parse user-defined input and populate objects (typically domain objects) with custom PropertyEditors on both application-wide and controller-specific levels.as String.
Extend controllers with dynamic methods to allow data binding and bean wrapping.

Resources