簡體   English   中英

ASP.NET MVC中的UnitOfWork錯誤

[英]Errors with UnitOfWork in asp.net mvc

我在mvc5項目中使用UnitOfWork模式。

我有一個帶有服務的BLL層。

 public class StudentService
    {
        private readonly IUnitOfWork _db;

        public StudentService(IUnitOfWork uow)
        {
            _db = uow;
        }

        public IEnumerable<StudentView> GetStudentViews()
        {
            List<Student> students = _db.Students.GetAll().ToList();
            return Mapper.Map<List<Student>, List<StudentView>>(students);
        }
}

但是,當我嘗試在mvc控制器中使用此服務時,出現錯誤:“沒有為此對象定義無參數構造函數。”

public class StudentController : Controller
    {
        private readonly StudentService _service;

        public StudentController(StudentService service)
        {
            _service = service;
        }

        // GET: Student
        public ActionResult Index()
        {
            IEnumerable<StudentView> studentViews = _service.GetStudentViews();
            return View("Index", studentViews);
        }
}

我沒有無參數構造函數,但是如何在控制器中將我的服務與無參數構造函數一起使用?

我將DI用於UnitOf工作:

 public class ServiceModule : NinjectModule
    {
        private string connection;
        public ServiceModule(string connection)
        {
            this.connection = connection;
        }

        public override void Load()
        {
            Bind<IUnitOfWork>().To<UnitOfWork>().WithConstructorArgument(connection);
        }
    }

“沒有為此對象定義無參數構造函數。”

此消息表示您忘記注冊StudentService的依賴項。 這就是為什么它忽略了構造函數

public StudentController(StudentService service)
        {
            _service = service;
        }

然后它開始尋找另一個無參數構造函數。

我建議您應該創建IStudentService接口

public class IStudentService

並使StudentService實現IStudentService

public class StudentService: IStudentService

然后在您的ServiceModule中

public class ServiceModule : NinjectModule
    {
        private string connection;
        public ServiceModule(string connection)
        {
            this.connection = connection;
        }

        public override void Load()
        {
            Bind<IUnitOfWork>().To<UnitOfWork>().WithConstructorArgument(connection);
            Bind<IStudentService>().To<StudentService>();
        }
    }

我已經解決了這個問題:

學生模塊

public class StudentModule : NinjectModule
    {
        public override void Load()
        {
            Bind<IStudentService>().To<StudentService>();
        }
    }
}

Global.asax:

 public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
           ///

            //dependencies
            NinjectModule serviceModule = new ServiceModule("connection");
            NinjectModule studentModule = new StudentModule();
            var kernel = new StandardKernel(studentModule, serviceModule);
            DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
        }
    }

此錯誤不是由於UnitOfWork。 由於您的控制器StudentController發生了這種情況,並檢查了該控制器的構造函數

public StudentController(StudentService service)

您的控制器需要StudentService實例,並且您沒有為該StudentService類注冊任何依賴項。 Asp.Net MVC控制器不需要參數構造函數,或者如果您有任何依賴關系,則需要在創建控制器之前解決該問題。

解決方案是為StudentService類創建一個Interface。

public interface IStudentService
{
     IEnumerable<StudentView> GetStudentViews();
}

並更新您的StudentService類以實現該接口

public class StudentService : IStudentService
{
    private readonly IUnitOfWork _db;

    public StudentService(IUnitOfWork uow)
    {
        _db = uow;
    }

    public IEnumerable<StudentView> GetStudentViews()
    {
        List<Student> students = _db.Students.GetAll().ToList();
        return Mapper.Map<List<Student>, List<StudentView>>(students);
    }
}

更新您的DI注冊代碼以注冊此依賴關系。

public class ServiceModule : NinjectModule
    {
        private string connection;
        public ServiceModule(string connection)
        {
            this.connection = connection;
        }

        public override void Load()
        {
            Bind<IUnitOfWork>().To<UnitOfWork>().WithConstructorArgument(connection);
            Bind<IStudentService>().To<StudentService>();
        }
    }

更新您的StudentController和使用IStudentService代替StudentService

public class StudentController : Controller
    {
        private readonly IStudentService _service;

        public StudentController(IStudentService service)
        {
            _service = service;
        }

        // GET: Student
        public ActionResult Index()
        {
            IEnumerable<StudentView> studentViews = _service.GetStudentViews();
            return View("Index", studentViews);
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM