Best practice design patterns with ASP.NET MVC services

I have an ASP.NET MVC 3 application.

I have a Model, ViewModel, View, Controller.

I use Ninjectas an IoC.

My Controlleruses ViewModelto transmit data View.

I started using Service(and specific types of interface) to receive information from ViewModeland ask for it against the database to manipulate.

Can I use the same Serviceto adjust ViewModel? Or is contrary to the texture of the design template?

those. Can I edit the settings ViewModelon the level Service?

Scenario

Scenario; My Modelthere are many references to the other Models, so when I set up ViewModelthe controller, it must be detailed, and I feel like Controllerdoing too much. So I want to be able to do something like:

var vm = _serviceProvider.SetupViewModel(Guid model1Id, Guid model2Id, /*etc..*/)

And function SetupViewModelin ServiceProviderwill look like this:

public MyModelViewModel SetupViewModel(Guid model1Id, Guid model2Id, /*etc...*/)
{
    var vm = new MyModelViewModel();
    var model1 = _repository.Model1s.FirstOrDefault(x => x.Id.Equals(model1Id));
    var model2 = _repository.Model2s.FirstOrDefault(x => x.Id.Equals(model2Id));
// etc....

    vm.Model1 = model1;
    vm.Model2 = model2;

    return vm;
}

Having done that, I can also add some conditions null, without having to worry about what my Controllerreally really great!

I use one ViewModelfor action Create / Edit. I do not use ViewModelelsewhere.

+2
source share
2 answers

ViewModel .

ViewModels, , .

AutoMapper , ViewModel, .

:

public class Customer
{
    public int Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Telephone { get; set; }

    public string Email { get; set; }

    public virtual ICollection<Order> Orders { get; set; }
}

ViewModel:

public class CustomerWithOrdersModel
{
    public CustomerWithOrdersModel(Customer customer)
    {
        Id = customer.Id;
        FullName = string.Format("{0}, {1}", customer.LastName, customer.FirstName);
        Orders = customer.Orders.ToList();
    }

    public int Id { get; set; }

    public string FullName { get; set; }

    public IEnumerable<Order> Orders { get; set; }
}

EDIT: AutoMapper:

AutoMapper, Customer a CustomerWithOrdersModel:

public class ViewModelProfile : Profile
{
    public override string ProfileName
    {
        get { return "ViewModel"; }
    }

    protected override void Configure()
    {
        CreateMap<Customer, CustomerWithOrdersModel>()
            .ForMember(dest => dest.FullName, opt => opt.MapFrom(src => string.Format("{0}, {1}", src.LastName, src.FirstName)))
            .ForMember(dest => dest.Orders, opt => opt.MapFrom(src => src.Orders.ToList()));
    }
}

Id .

ViewModelProfile:

public static class ViewModelProfileExtensions
{
    public static CustomerWithOrdersModel ToModel(this Customer customer)
    {
        return Mapper.Map<CustomerWithOrdersModel>(customer);
    }

    public static Customer ToEntity(this CustomerWithOrdersModel customerWithOrdersModel)
    {
        return Mapper.Map<Customer>(customerWithOrdersModel);
    }
}

:

public ActionResult Details(int customerId)
{
    Customer customer = _customerService.GetById(customerId);
    CustomerWithOrdersModel customerWithOrders = customer.ToModel();
    return View(customerWithOrders);
}

CustomerWithOrdersModel Customer, customerWithOrdersModel.ToEntity() .

! Customer ViewModel.

+5

, . , .

+1

All Articles