0

Prior to using a ViewModel, I could easily pass the "soon to be edited" object directly to the view without needing to fuss over setting individual properties etc as the View conveniently accepted the Employee type directly..

    [HttpGet]
    public ActionResult EditEmployee(int? id)
    {
        EmployeeRepository ER = new EmployeeRepository();
        Employee SomeEmployee = ER.GetEmployee(id.Value);

        if(SomeEmployee!=null)
            return View(SomeEmployee);

But now I'm using a ViewModel with DataAnnotations attributes applied over the top of various properties for validation purposes. Which creates a problem..

After fetching the "soon to be edited" object from the db, setting the ViewModel's values is suddenly a whole lot more complicated. I can't simply pass the retrieved object straight to the view, as the View now expects the VMEmployee type instead.

I would like to be able to do something like:

    [HttpGet]
    public ActionResult EditEmployee(int? id)
    {
        EmployeeRepository ER = new EmployeeRepository();
        Employee SomeEmployee = ER.GetEmployee(id.Value);

        if(SomeEmployee!=null)
            return View(new VMEmployee(SomeEmployee));

All paths seem to lead to a huge constructor which manually sets the values of each individual property. But I never had to do that before when I wasn't using a ViewModel. Model binding was a blessing!

My objects also have complex child objects, which my form is also collecting values for, so this would be a huge/verbose task against DRY principals.

I don't even really want to use a ViewModel, but am forced to because I need two different DataAnnotations rule sets for different validation scenarios applied to the same object.

All I want to do is be able to have two different DataAnnotations rule sets for different scenarios. I.e. public-facing www site vs internal-facing admin site. DataAnnotations doesn't seem to be flexible enough to easily cater for this common need.

I've tried AutoMapper, but it throws an error saying it can't map my object types, I suspect because Employee was auto-generated by LINQ to SQL.

What is the most elegant way to achieve this while sticking to DRY principals?

1 Answer 1

0

I would define a mapping class where its sole purpose will be to map an Employee to a VMEmployee. The mapping will still need to exist but ideally the viewmodel should not need to be concerned with doing it. You can then unit test this to make sure that everything is mapped.

To facilitate this you could look into using something like Automapper. However, I tend find that manually writing mapping code ends up being easier.

The WhoCanHelpMe gives an example of how to implement this with Automapper using a decent OOP structure and IOC.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response. But I'm not looking to over-engineer by adding even more classes just for the purpose of mapping. If you multiply the number of different forms I'm going to need by the number of different complex objects I'm dealing with it isn't a scalable/practical solution. Old school hard-coded validation routines look more and more attractive. I also want to stick with KISS and DRY principals and also avoid third party add-on DLLs.
Well you're going to need to manually do the mapping at some stage. The SRP would suggest that mapping should not be the responsibility of the object being mapped. I'd suggest that separating out this functionality actually keeps it quite simple despite the increased number of satellite classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.