views:

41

answers:

2

Hello,

I have a Windows Form project that I would like to migrate toward a web application using ASP.NET MVC2.

In this project I have some POCO classes as in this example that are part of a class library and that I would like to use with a binary reference

public class Person
{
    public int PersonID { get; set; }

    public string Name { get; set; }

    public DateTime BornDate { get; set; }

    ...
}

Is there a way to use these classes inside my Web MVC project and adding, for example validation attributes without modifying the original assembly?

thanks for helping

+1  A: 

You may take a look at FluentValidation. It integrates nicely with ASP.NET MVC and allows you to unobtrusively add validation rules without modifying your POCO objects.

Darin Dimitrov
Good tool. Thanks!
Lorenzo
+1  A: 

You can add Meta Information like Validation by using a Partial Class

namespace xxx.Data.yyy
{
    [MetadataType(typeof(Posting_Validation))]
    public partial class Posting { 
    }

    public class Posting_Validation {
        [Required(ErrorMessage = "Need title")]
        [StringLength(50, ErrorMessage = "Must be under 50 characters")]
        [DisplayName("Title")]
        public string Title { get; set; }

        [Display(AutoGenerateField = false)]
        [HiddenInput(DisplayValue=false)]
        public int PostingId { get; set; }

        [UIHint("tiny_mce")]
        public string HtmlContent { get; set; }
    }
}
Henrik P. Hessel
The MetadataType attribute should go to the original POCO class?
Lorenzo
yes, like Posting is my original class.
Henrik P. Hessel
Well...That's out of scope for me because it breaks compatibility with the window form project that I should continue to support. That's the way I have stated "without modifying the origina assembly"... Thanks anyway ;)
Lorenzo
You don't have to modify your original assembly! You just extending it in your own code!
Henrik P. Hessel
@Henrik: Sorry but I did not understand. If I have to decorate the original class with an attribute why you're saying that I dont have to modify the original assembly? Do you mean by using the partial class modifier? And how this work if the original class has not the partial modifier?
Lorenzo