Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASp.NET MVC - Is it possible to simplify my architecture?

I have just started working on an MVC project and things are going ok but it looks like I am creating alot of spaghetti code with just too many objects. Can anyone see how I can simplify this solution before the whole projects gets out of hand?

ok, here's my set up:

DAL - has Entity framework connections and methods to obtain data then convert the data to my model objects in the model layer

BLL - sends the data back up to the UI Model - this contains all the model objects that are used throughout the site, anything coming from the DAL is converted into these objects by creating a new object then populating the variables.

UI - my MVC solution

The DAL,BLL and Model are also used by other solutions.

Now with MVC, I am trying to use the validation annotations ([Required], etc) which means I have to re-create the model objects with the annotations. This is fine but if I want to save the data back into the database I need to convert the classes which is just messy.

Can anyone see how I can use my current model class library with MVC model objects that use the validation annotations?

If I have not explained myself clearly please let me know and I will provide more details.

Thanks

like image 235
Funky Avatar asked May 03 '12 11:05

Funky


1 Answers

Ideally there needs to be a separation from the domain models on one hand and MVC models (they are really ViewModels) on the other hand. This separation is really crucial and strongly advised.

These will look a lot similar in most cases although ViewModel can contain extra stuff. Then you can use AutoMapper to convert from one to the other.

For example:

public class User // in entity DLL
{
    [Required]
    public string Name {get; set;}
}

public class UserViewModel : User // in MVC DLL
{
    public string LastVisitedPage {get; set;} // which only MVC needs to know
}

Mapper.Map<User, UserViewModel>();
Mapper.Map<UserViewModel, User>();
like image 183
Aliostad Avatar answered Oct 03 '22 12:10

Aliostad