Can Castle Validators work with DataAnnotations attributes? - data-annotations

Is there a way of making DataAnnotations attributes such as Required to work with Castle Validators?
I know Castle has its own attributes - but I am wanting to use the same attribute for the view and business logic. So I want the attributes to be used for the view validation and any business logic validation. I am aware this approach is not ideal.

Related

Should I use custom model binder to bind view model to entity?

Just some idea to make more use of custom model binder. I am currently still using IMapper interface to do so, though wondering whether part of the purpose of custom binder is to mapping view model or input model to business entity? I can see there might be some limitations if i use MVC custom binder. What is the advantage to use the custom binder in MVC? Will my app gain better performance?
Short answer would be No, you should not
ModelBinder by itself is part of ASP.NET MVC infrastructure. If you would take a look at ASP.NET MVC pipline (PDF) you would see that it's job is
to convert a posted web form data (a string basically) or query string from URL to an instance of particular class.
ASP.NET MVC framework has a DefaultModelBinder that
is suitable for 99% of cases. Custom model binders could be used in situations where standard data conversion fails e.g. mapping $ 1,234.56 from a textbox to a decimal value of 1234.56
Moreover ModelBinder implements IModelBinder interface with a single BindModel() method. This method expects parameters that would be hard to 'hand-craft' to make any use of them and are totally not relevant to your scenario.
What you are realy looking for is
- either custom object mapping between viewmodels and business objects where you manually assign one object property values to another
- or taking advantage of libs/frameworks such as Automapper or ValueInjecter which take care of object mapping hassle away from you
- or a mix of both

MVC ModelState and EntityFramework Validation

I have a edmx model done by Database First and generating the DBSet with the VS tools. I extended with partials the classes to add dataannotation validation to it.
I am receiving on my controller the view model which I am manually controlling the validation of fields. So eventhough I have a required field on my partial class, I am removing it once in my controller ModelState.Remove("pasajeros[" + count + "].numResidencia"); because of some conditions.
Before, I was using LINQTOSQL and I had no problems. But now Entity framework is not honoring my customization of ModelState.
How do i propagate or GO about this issue with EntityFramework?
thanks
Keep your view models and Entity models separate. Put your validation annotations on your view models. Then use a tool like Automapper to map the Entity to ViewModel fields for you.
On edit you validate your view model then update your Entity fields which you then save.

Model layer dependency on MVC attributes

We have an MVC3 project that uses nHibernate; there is a separate model project that contains all the model classes which is used by the repository and service layers. The models make use of data annotations like DisplayAttribute and RequiredAttribute from System.ComponentModel.DataAnnotations.
There are also attributes such as RemoteAttribute that are contained in System.Web.Mvc.
This of course means that the model project now has a dependency to a particular front end technology.
Assuming the solution could have other front ends what would be the best way to handle this dependency link?
RemoteAttribute does not belong in the model, since it specifies a controller/action to validate the property on the server, and the model shouldn't have knowledge of concepts like controller, action or route. The presentation layer depends on the model, not the other way around.
I would create a view model that inherits the model, overrides the property (must be virtual) and adds the RemoteAttribute. This way you can avoid duplication and mapping, although that's also an alternative.
To reduce dependency between database model and frontend technology, you can use special view model for validation qnd other front end actions in controller and put data from viewmodel to database entity after it.

Concept of ViewModel/Model Validation in ASP.NET MVC + WCF applicaiton

I have the following architecture of my application:
http://clip2net.com/clip/m50879/1303228845-clip-15kb.png
The Model contains a set of POCO objects which should be validated on the web and the services sides.
Also, I have additional ViewModel layer which is being used only on the web side. The ViewModel layer contains the most of validation logic which is the same as the Model validation logic...
The question is:
What is the best approach to avoid copy-paste of the Model validation logic to the ViewModel validation logic?
Maybe this is of interest:
"How to: Validate Model Data Using DataAnnotations Attributes"
http://msdn.microsoft.com/en-us/library/ee256141(VS.100).aspx

Choose the best Client/Server Validation Framework for MVC

Im trying to choose the best server/client validation framework to use with my MVC application. I would like to know what is the best validation framework to use with MVC to do server/client validation.
My options are:
Castle validator
Microsoft Data Annotations
nHibernate Validator
With the upcoming MVC 2, it looks like MS is leaning towards the System.ComponentModel.DataAnnotations library. It's pretty nice - does a lot of code generation for you.
I've been using xVal with great success.
The best thing is you can fully automate validation:
put DataAnnotations (or special rules) to your business layer POCO classes (if you have them) or entity classes' metadata buddy classes if you use entities up to controller layer
write an ActionFilter that will automatically validate parameters to your controller actions - it's best if all your POCOs (or entities) implement a certain interface that defines Validate() method, filter calls this method and fills ModelState.Errors when validation fails.
Add if (ModelState.IsValid) { ... } in your controller action that needs to work differently when model isn't valid
Put <%= Html.ValidationMessage(...) %> in your views that will display validation errors
Add xVal's client validation to your views if desired
This way you've set up automatic object validation. All your controller actions will do is check Model validity.
Asp.net MVC 2 will have something very similar to xVal already built in the framework so it's up to you which version are you using.

Resources