简体   繁体   中英

ASP.net N tier Architecture presentation layer

I meet a problem that I do not know how presentation layer use service layer. Totally I have three layers: data layer, service layer and presentation layer.

In data layer I used Ado.net and created repository.

In service layer I created Dto entities. And I meet a problem when I call service from Controller.

The error is:

No parameterless constructor was defined for this object. Description: Unhandled exception when executing the current web request. Review the stack trace to see more information about this error and to determine where the error in the code was caused. Exception Details: System.MissingMethodException: No parameterless constructor has been defined for this object.

Source Error:

An unhandled exception was generated while running the current web request. Information about the origin and location of the exception can be viewed using exception stacking.
Below is my code:

        using AutoMapper;
        using OA.Models;
        using OA.Service;
        using OA.Service.DAL;
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.Mvc;

        namespace OA.Controllers
        {
          public class HomeController : Controller
           {
              private readonly IUserService userService;
              private readonly IUserProfileService userProfileService;

          public HomeController(IUserService userService, IUserProfileService userProfileService)
          {
           this.userService = userService;
           this.userProfileService = userProfileService;
           }
    // GET: Home
    [HttpGet]
    public ActionResult Index()
    {
        List<UserViewModel> model = new List<UserViewModel>();
        userService.GetUsers().ToList().ForEach(u =>
        {
            UserProfileDto userProfileDto = userProfileService.GetUserProfile(u.Id);
            UserViewModel user = new UserViewModel
            {
                Id = u.Id,
                Name = $"{userProfileDto.FirstName} {userProfileDto.LastName}",
                Email = u.Email,
                Address = userProfileDto.Address
            };
            model.Add(user);
        });

        return View(model);
    }

  }
}

Does someone know what is the problem? Or I missunderstand how to use N tier achitecture? Thanks

You have to register the HomeController in the IOC.

Exemple with Unity :

public static void RegisterTypes(IUnityContainer container)
{
    container.RegisterType<Controllers.HomeController>(new InjectionConstructor());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM