问题描述
我对感知到的"最佳实践"感兴趣,在这里充满了一点点现实.
在Web应用程序中,您是否允许您的Web层直接访问DAL,还是首先通过BLL?
我专门谈论的是没有真正涉及的"业务逻辑"的方案 - 例如一个简单的查询:"以'Atwood'的姓氏获取所有客户".绝对有任何逻辑的场景绝对会通过BLL,所以让我们称呼 moo .
当您可以将此方法封装在BLL对象中时,当签名通常与DLL对象完全相同时,它似乎毫无意义,并且代码可能与一个将查询委派给dll的衬里.
如果您选择前者 - 使用BLL对象 - 您称之为这些对象? (假设他们做的不仅仅是向DLL提供查询层).助手? QueryProviders?
请问.
问候
马蒂
推荐答案
我认为,您应该始终使用bll(业务逻辑层)在您的Web层和您的dal之间(数据访问层)./p>
我很欣赏一些更"简单"的查询,BLL将密切模仿DAL(例如,获取所有国家/地区,获取所有产品类型等),但坦白地说,即使在您的示例中也是如此:
(以姓氏的姓氏获取所有客户 'atwood')
这里有"业务逻辑" - 渴望通过姓氏过滤的数据记录,如果没有其他!
通过从项目开始实施BLL,在可能出现需求时,插入验证或其他"逻辑"变得非常容易(如果您的项目是商业应用程序,则需要几乎当然,如果项目开始时不存在,则最终会出现.添加其他逻辑,例如:
当涉及真正的BLL时,获取所有花费的客户 今年超过$ 10000
或
不允许顾客以" Atwood"的姓氏 购买超过$ 1000
的商品
变得更加容易,而不是试图将这种逻辑推入Web层.
请记住,在上面的各种查询中,我们几乎可以肯定地谈论多个实体和数据库表,这些实体和数据库表必须与特定定义的关系一起连接以实现此功能.试图通过直接操纵DAL来实现这一目标,因为您将与多个实体和类打交道.这里的BLL将大大简化您的Web层代码,因为BLL将 encapsulation "> enderity 极其简化的界面背后的关系.
这个" 关注点的分离"界面出现.
现在至少有两次不同的场合,我使用网站用户界面进行了商业Web应用程序,并最终被要求(由于客户需要在其软件产品中寻求更多集成的业务),以生产一个 Web服务接口提供与网站完全相同的功能.
如果我在网络层中嵌入了任何业务逻辑,则在实现我的Web服务时,我将不得不重复并重写该逻辑.实际上,我确保所有业务逻辑都封装在BLL类中,这意味着我只需要设计一系列的Web服务接口方法调用,并根据BLL类上的方法将它们插入幕墙设计模式在简化Web Service API的地方.
).).总的来说,我可以认为不理解在我的dal和我的网络层之间包括一个BLL层.
最简单的时候,当BLL紧密地"模仿" DAL时,是的,似乎确实有代码和功能的重复,而虽然键入更加键入,但这也使实施相对易于实现.<<<<<<<<
当它更多地参与时(例如,从一开始就存在重要的业务逻辑时),关注点的分离有助于减少重复(干燥原理)同时大大简化了未来和持续的维护.
当然,这是假设您"手工"做所有这些.如果您愿意的话,您可以通过使用 orm 有很多! (i. wikipedia.org/wiki/subsonic_(software)" rel =" noreferrer"> subsonic , noreferrer">/a>等)
其他推荐答案
我不同意这里的大多数帖子.
我在Web层中调用我的数据层.如果Web/UI层之间没有任何内容,那么"以防万一"就没有必要创建一层.这是预优化.这是浪费.我不记得一个业务层"救了我"的时间.它所做的只是创造了更多的工作,重复和更高的维护.我花了多年的时间订阅了业务层 - >数据层通过两层之间的实体.我总是感到肮脏的通过方法没有做任何事情.
被介绍给 ericeic evans ,我做有道理的事情.如果UI和数据层之间没有任何内容,则我在UI中调用数据层.
为了进行未来的更改,我将所有数据层类包装在接口中.在UI中,我引用界面,并使用依赖注入来管理实现.经过这些更改后,这就像新鲜空气的呼吸.如果我需要在数据层和UI之间注入某些东西,我会创建一个服务.
我做的另一件事是减少项目数量.在我将进行数据层,业务逻辑,业务实体和某种UI项目的项目之前,这真是痛苦.
我有两个项目:核心项目(实体,业务逻辑和数据层)和UI项目(Web,Web服务等...)
有关更多信息,我建议您查看这些人:
其他推荐答案
您需要区分BLL对象(无论如何这些是什么?域名对象?)和服务.您的域对象不应与您的数据访问层无关.就网络层而言,它可以像其他任何服务一样可以对待您的存储库(Think IRepository).
因此,最重要的是:是的,Web Tier可以直接使用DAL,只要它是属性封装并表示为标准服务层服务.
问题描述
I'm interested in perceived "best practice", tempered with a little dose of reality here.
In a web application, do you allow your web tier to directly access the DAL, or should it go through a BLL first?
I'm talking specifically of scenarios where there's no "business logic" really involved -- such as a simple query : "Fetch all customers with surname of 'Atwood'". Scenarios where there's any kind of logic absolutely are gonna go through the BLL, so lets call that moo.
While you could encapsulate this method inside a BLL object, it seems to be somewhat pointless when often the signature will be exactly the same as that of the DLL object, and the code probably as simple as a one liner delegating the query off to the DLL.
If you choose the former -- employing a BLL object -- what do you call these objects? (Assuming they do little more than provide a query layer into the DLL). Helpers? QueryProviders?
Thoughts please.
Regards
Marty
推荐答案
In my opinion, you should ALWAYS use a BLL (Business Logic Layer) between your web tier and your DAL (Data Access Layer).
I appreciate that for some of the more "simple" queries, the BLL will closely mimic the DAL (e.g. Fetch all countries, Fetch all Product Types etc.), but to honest, even in your example:
(Fetch all customers with surname of 'Atwood')
there is "business logic" being expressed here - A desire for the data records to be filtered by surname, if nothing else!
By implementing a BLL from the start of a project it becomes incredibly easy to insert either validation or additional "logic" as and when the need may arise (and if your project is a commercial application, that need will almost certainly arise eventually if it isn't there at the beginning of the project). Adding in additional logic such as:
Fetch all customers who have spent over $10000 this year
or
Don't allow customers with the surname of 'Atwood' to purchase items over $1000
becomes significantly easier when a true BLL is involved, rather than trying to crowbar this logic into the web tier.
Bear in mind that with the kinds of queries above, we're almost certainly talking about multiple entities and database tables that will have to join together with specifically defined relationships in order to implement this functionality. Trying to achieve this by directly manipulating the DAL becomes messy since you'll be dealing with multiple entities and classes. A BLL here would greatly simplify your web tier code, since the BLL will encapsulate those entity relationships behind a greatly simplified interface.
This "separation of concerns" becomes increasing important when and if the need to change the user interface arises.
On at least two separate occasions now, I've worked on commercial web applications with a web site user interface, and have been eventually asked (due to business need arising from clients seeking greater integration within their software products) to produce a web service interface offering the exact same functionality as the web site.
Had I embedded any business logic within my web tier, I would have had to duplicate and re-write that logic when implementing my web service. As it was, I ensured that all business logic was encapsulated within BLL classes, which meant that I simply had to design a series of web service interface method calls, and plug these up against calls to methods on the BLL classes (I actually used the Facade Design Pattern in places to simplify the web service API).
In all, I can think of no reason to NOT include a BLL layer between my DAL and my web tier.
At it's easiest, when the BLL closely "mimics" the DAL, yes, there does appear to be a duplication of code and functionality, however, whilst being a little more typing, this also makes it relatively easy to implement.
When it's more involved (such as when significant business logic exists from the very beginning), the separation of concerns helps to reduce repetition (the DRY principle) whilst at the same time significantly simplifying future and ongoing maintenance.
Of course, this assumes you're doing all this "by hand". If you so desire, you can significantly simplify the DAL/BLL/UI layers by utilizing an ORM of which there are many! (i.e. LINQ-to-SQL/Entities, SubSonic, NHibernate etc.)
其他推荐答案
I disagree with most the posts here.
I call my data layer in the web tier. If there is nothing in between the WEB/UI tier there is no point creating a layer "just in case." It's pre optimization. It's a waste. I can't recall a time the business layer "saved me." All it did was created more work, duplication and higher maintenance. I spent years subscribing to the Business Layer --> Data Layer passing entities between the layers. I always felt dirty creating pass through methods that did nothing.
After being introduced to Domain Driven Design by Eric Evans, I do what makes sense. If there is nothing in between the UI and the Data Layer then I call the Data Layer in the UI.
To allow for future changes I wrapper all my Data Layer classes in interfaces. In the UI, I reference the interfaces and I use dependency injection to manage the implementation. After making these changes, it was like a breath of fresh air. If I need to inject something in between the data layer and UI, I create a service.
Another thing I did, was to reduce the number of projects. Before I would have a project for the Data Layer, Business Logic, Business Entities and some type of UI Project -- what a pain.
I have two projects: The core project(entities, business logic and data layer) and UI projects (web, web services, etc...)
For more information I recommend looking at these guys:
其他推荐答案
You need to distinguish BLL objects (what the heck are these anyway? Domain objects anyone?) and Services. Your domain objects should have nothing to do with your data access layer. As far as web tier goes, it can treat your repositories (think IRepository) just like any other service it can freely use.
So the bottom line is: yes, web tier can use DAL directly provided it is property encapsulated and is represented as a standard Service Layer service.