2

I have written a desktop application, and now I'm working on the ASP.net MVC version of the same program. Many of the business models are in a .dll library which I share between both the desktop program and the MVC program.

I'd like to use features of MVC, like validation, but to this end I'd need to add attributes to the model classes defined in the .dll. Since it is my own library I could do it, but what if I needed to use a library I cannot access. What is best practice? Writing a wrapper class, like this?

As an example, let this be a simple class in the .dll

public class A {
    public string Name {get; set;}
    public A(string _name){
        this.Name = _name
    }
}

In the MVC project I would like to use A, but would also like to add DataAnnotations to its property. I could then define a new class like this:

public class A_Web {
    private _a;

    [Required(ErrorMessage = "A Name is required")]
    public string Name {get {return _a.Name;} set {_a.Name = value;}}

    public A_Web(string _name){ 
        this._a = new A(_name)
    } 
}

Is there a better way? This seems like a lot of code for bigger classes...

EluciusFTW
  • 2,565
  • 6
  • 42
  • 59
  • 1
    Use a [view model](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) and tools such as [automapper](https://github.com/AutoMapper/AutoMapper) to map between your data model and view model –  Jan 27 '15 at 08:33
  • The way i approached it with pure dataclasses is that i just defined an interface and programmed to this interface. the viewmodel can implement this interface and you can adorn the properties with your dataanotations – Florian Schmidinger Jan 27 '15 at 09:14
  • @Florian, that does not help if the data classes are supplied to you. Also, that does not seem to be less code than the way I proposed above. – EluciusFTW Jan 27 '15 at 10:07
  • @ToaoG sorry i overlooked that part that you dont have influence on the library. usually with this technique you seperate definition from implementation and get this kind of flexibility(it takes a little code thats true). But in this case ... automapper solution seems best. – Florian Schmidinger Jan 27 '15 at 10:33

0 Answers0