Lorenzo
Lorenzo

Reputation: 29427

Which is the way to add MVC behavior to existing classes?

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

Upvotes: 2

Views: 133

Answers (2)

Henrik P. Hessel
Henrik P. Hessel

Reputation: 36637

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; }
    }
}

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

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.

Upvotes: 1

Related Questions