简体   繁体   中英

ASP.NET 5 MVC with EF controllers using services

I'm new to the ASP.NET 5 MVC with Entity Framework. This is probably a simple problem, but I can't find a tutorial for this online, or I don't know how to search for it. So here's my problem.

I've took the task of finishing a simple project. Here's how the solution looks like:

在此处输入图像描述

Task says that there's this code-first approach, hence the Data, Entity, and Services libraries.

  • Data - provides the DataContext
  • Entity - provides objects (C# classes that represent DB tables)
  • Services - provide CRUD and other functions for each entity

How do you call service for object A from controller for object B without using the DataContext class?

All tutorials I've seen don't even use services. Each of their controllers use the DataContext instance. If I used DataContext in my controller for object BI would render services library useless. I know I can, but the goal is to use the services.

It might go without saying for me, but just to be clear: services library has 2 interfaces and 2 classes that implement the two interfaces, one pair of interface-class is for object A and the other is for object B. So: IServiceA, ServiceA, IServiceB, ServiceB.

The reason I need ServiceA to call from ControllerB is because B contains an instance of A, thus for my practice I need to list all As in a view dedicated to B.

I just checked and saw that each of the services is using the DataContext, so I could have just added a new function there and continue using the service in the controller.

Silly silly me.

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