[英]With top down architecture my business layer can't access my data access layer, what's wrong?
Using Asp.Net MVC I wanted a top down approach. 使用Asp.Net MVC,我想要一种自上而下的方法。 I started using a top down approach but now it seems like it's an onion approach because my repository interfaces in the data access layer need to reference my model layer so it understands the objects it's returning.
我开始使用自顶向下方法,但现在似乎是一种洋葱方法,因为我在数据访问层中的存储库接口需要引用我的模型层,以便它可以理解要返回的对象。 So now I'm referencing up and not down.
所以现在我参考的是向上而不是向下。
Now I have a method (LoadAllAppointmentsInDateRange) inside a model (DiaryEvent) in the business layer that needs to call the repository in the data access layer below, but it can't because there would be circular references. 现在,我在业务层的模型(DiaryEvent)中有一个方法(LoadAllAppointmentsInDateRange),该方法需要在下面的数据访问层中调用存储库,但是因为有循环引用,所以不能。 How did I screw this architecture up and how can I fix it?
我如何弄乱这种架构,以及如何解决呢? Should I create another layer (service layer) that takes care of this and puts the method (LoadAllAppointmentsInDateRange) inside it or should I move my interfaces?
我应该创建另一个层(服务层)来处理此问题并将方法(LoadAllAppointmentsInDateRange)放入其中,还是应该移动接口?
Here is what I have and what I want to do on a simplified level. 这是我所拥有的以及我想要简化的工作。
Controller (top layer) - 控制器(顶层)-
public JsonResult GetDiaryEvents(double start, double end)
{
var apptListForDate = DiaryEvent.LoadAllAppointmentsInDateRange(start, end);
//do some other stuff
return Json(rows, JsonRequestBehavior.AllowGet);
}
Business layer (middle layer) - 业务层(中间层)-
public class DiaryEvent
{
public int ID;
//other memebers here
public static List<DiaryEvent> LoadAllAppointmentsInDateRange(double start, double end)
{
// logic here
// want to access the repo here, but can't because data access layer is referencing this layer (business layer)
IYogaSpaceEventRepository eventRepository = new YogaSpaceEventRepository();
IQueryable<YogaSpaceEvents> events = eventRepository.FindEvents(startTime, endTime);
//do some processing of the returned data
return result;
}
}
Access layer (bottom layer) - it references the business layer. 访问层(底层)-它引用业务层。
public class YogaSpaceEventRepository : IYogaSpaceEventRepository
{
YogaContext context = new YogaContext();
public IQueryable<YogaSpaceEvent> FindEvents(DateTime start, DateTime end)
{
var rslt = context.YogaSpaceEvents.Where(s => s.DateTimeScheduled >= start
&& DbFunctions.AddMinutes(s.DateTimeScheduled, s.AppointmentLength) <= end);
return rslt;
}
public void Dispose()
{
context.Dispose();
}
}
public interface IYogaSpaceEventRepository : IDisposable
{
// here my repo (data access layer) is referencing my business layer to return YogaSpaceEvent
IQueryable<YogaSpaceEvent> FindEvents(DateTime start, DateTime end);
}
Here's how you should structure your project: 这是构建项目的方式:
In you Business Logic Layer store your business entities and repository interfaces as follows: 在您的业务逻辑层中,存储您的业务实体和存储库接口,如下所示:
public class YogaSpaceEvent
{
// Function and Data Members
}
public interface IYogaSpaceEventRepository
{
IQueryable<YogaSpaceEvent> FindEvents(DateTime start, DateTime end);
}
public class DiaryEvent
{
private IYogaSpaceEventRepository _repository;
public DiaryEvent(IYogaSpaceEventRepository repository)
{
this._respository = repository;
}
public static List<DiaryEvent> LoadAllAppointmentsInDateRange(double start, double end)
{
// Call appropriate method on _repository
}
}
In your Data Access Layer (DAL), Add a reference to your BLL and implement the YogaSpaceEventRepository
repository: 在您的数据访问层(DAL)中,添加对BLL的引用并实现
YogaSpaceEventRepository
存储库:
public class YogaSpaceEventRepository : IYogaSpaceEventRepository
{
public IQueryable<YogaSpaceEvent> FindEvents(DateTime start, DateTime end)
{
// Retrieve Data from Database
}
}
In your presentation Layer (MVC Controller) you first have to add a reference to your BLL and DAL, and then create an DiaryEvent object injecting an instance of the YogaSpaceEventRepository
repository, like so: 在表示层(MVC控制器)中,首先必须添加对BLL和DAL的引用,然后创建一个DiaryEvent对象,该对象注入
YogaSpaceEventRepository
存储库的实例,如下所示:
public class PresentationLayer
{
public void Test(double start, double end)
{
DiaryEvent diaryEvent = new DiaryEvent(new YogaSpaceEventRepository());
var apptListForDate = DiaryEvent.LoadAllAppointmentsInDateRange(start, end);
}
}
If what you call the Business layer is equivalent to the Domain Layer in DDD, it shouldn't know about data access. 如果您所谓的业务层等效于DDD中的域层,则它不应该知道数据访问。
Delegate data access to an Application layer that knows the current application execution state, and keep your core domain devoid of persistence details. 将数据访问委派给知道当前应用程序执行状态的应用程序层,并使您的核心域没有持久性详细信息。 This way it is nicely decoupled -- you can put whatever persistence you wish and any number of applicative systems around it without changing the core business rules.
这样,它就很好地分离了-您可以在不改变核心业务规则的情况下,将所需的持久性和任何数量的应用系统置于其周围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.