简体   繁体   English

我需要分开业务对象和业务逻辑吗? C#中具有存储库模式的Asp.net MVC

[英]Do I need to Separate Business Objects and Business Logic? Asp.net MVC with Repository Pattern in C#

I'm having trouble finding an answer to this. 我很难找到答案。 Basically, right now I have these layers: 基本上,现在我有以下几层:

  • Data Access Layer (where my repositories are). 数据访问层(我的存储库所在的位置)。 It has Namespace Hello.Data 它具有命名空间Hello.Data
  • Business Layer (where my business objects are). 业务层(我的业务对象所在的位置)。 It has Namespace Hello.Business 它具有命名空间Hello.Business

My repositories will return business objects. 我的存储库将返回业务对象。 For example, GetCustomer will return Customer. 例如,GetCustomer将返回客户。

However, in my business layer, I also want to add logic that will use the repository to add/update/delete records, so I can reuse those methods in my MVC controllers. 但是,在我的业务层中,我还想添加将使用存储库添加/更新/删除记录的逻辑,以便可以在MVC控制器中重用这些方法。 This doesn't work though, since I can't have my Data Access Layer reference my Business Layer and also have my Business Layer reference my Data Access Layer. 但是,这不起作用,因为我无法让我的数据访问层引用我的业务层,也无法让我的业务层引用我的数据访问层。 (It creates a circular reference that is not allowed). (它创建了不允许的循环引用)。

What do you guys think is the best solution? 你们认为什么是最好的解决方案? Should I put my Business Logic into it's own project? 我应该将业务逻辑放入自己的项目中吗?

So instead of: 所以代替:

Hello.Data
Hello.Business

I'll have: 我将有:

Hello.Data
Hello.Business
Hello.BusinessLogic

Or am I thinking about this all wrong? 还是我在想这一切错了? Thanks! 谢谢!

Why would you separate business logic from business models? 您为什么要将业务逻辑与业务模型分开? The logic should be in the models. 逻辑应该模型中。

The business logic layer shouldn't have to reference anything, aside from maybe small common utility libraries. 除了可能的小型通用实用程序库之外,业务逻辑层也不必引用任何内容。 Basically, it should have no dependencies on infrastructure concerns (like application technologies, database technologies, etc.). 基本上,它应该不依赖于基础结构问题(例如应用程序技术,数据库技术等)。 It should contain just business logic. 它应该包含业务逻辑。

All other layers should reference the business logic layer. 所有其他层都应引用业务逻辑层。

This doesn't work though, since I can't have my Data Access Layer reference my Business Layer and also have my Business Layer reference my Data Access Layer. 但是,这不起作用,因为我无法让我的数据访问层引用我的业务层,也无法让我的业务层引用我的数据访问层。

Correct! 正确! The mistake in this design is that the business logic layer references the data access layer at all . 本设计中的错误是把商业逻辑层引用了数据访问层 It shouldn't. 不应该这样

It should , however, contain interfaces for data access (and other infrastructure concerns) which are implemented by the data access layer. ,然而,包含用于由所述数据访问层实现的数据访问(和其他基础设施问题) 接口 The business logic layer only needs to know about the interfaces, it doesn't know or care what implements those interfaces. 业务逻辑层只需要了解接口,就不知道或不在乎什么实现了这些接口。

A dependency injection container would then be used to connect implementations to interfaces. 然后将使用依赖项注入容器将实现连接到接口。

  • The application layer references the dependency injection layer to initialize it and pass the container (which itself implements a generic business logic interface) to the business logic layer. 应用程序层引用依赖项注入层以对其进行初始化,并将容器(其本身实现通用业务逻辑接口)传递给业务逻辑层。
  • The dependency injection layer references the business logic layer to know about the interfaces and references the data access (and other infrastructure) layer to know about the implementations. 依赖项注入层引用业务逻辑层以了解接口,并引用数据访问(和其他基础结构)层以了解实现。
  • The data access (and other infrastructure) layer references the business logic layer to know about the interfaces to implement. 数据访问(和其他基础结构)层引用业务逻辑层以了解要实现的接口。
  • The business logic layer doesn't reference anything. 业务逻辑层不引用任何内容。 It requires a configured dependency injection container and uses that container to get implementations for interfaces. 它需要配置的依赖项注入容器,并使用该容器获取接口的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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