Web应用程序架构 - 关于将 HTTPContext 保留在表示层中的建议

发布于 2024-09-17 19:27:00 字数 404 浏览 5 评论 0原文

大多数应用程序架构建议似乎都强烈建议只有表示层才能访问 HTTPContext(以促进松散耦合、减少依赖性、提高可测试性等)。

那么,人们如何处理Caching和Session呢?需要非常具体的数据访问和业务逻辑知识来确定哪些项目需要缓存才能最大程度地提高应用程序性能。但是,对于 ASP.Net Web 应用程序,对这些内容的访问是通过 HTTPContext 提供的。

一种选择是创建一个 CacheFactory 和一个 SessionFactory,以及一个 ICache 和 ISession 接口 - 然后以某种方式使用 DI 原理将 ISession 和 ICache 传递给 BLL 中的每个方法以及随后需要它的 DA 层。

这真的是开发商最终要做的吗?还有另一种更简单的方法来处理这个问题吗?

感谢您的任何建议。

The majority of the Application Architecture advice seems to advise strongly that only Presentation Layer should have access to HTTPContext (to promote loose coupling, decrease dependencies, increase testability etc).

So, how do people deal with Caching and Session? Very specific DataAccess and Business Logic knowledge is required to determine what items need caching to best benefit application performance. However, and an ASP.Net web application, access to these is provided via the HTTPContext.

One option would be to create a CacheFactory and a SessionFactory, and an ICache and ISession interface - and then somehow use DI principle to pass and ISession and ICache to each method in the BLL and subsequently DA Layer that needs it.

Is this really what developers end up doing? Is there another, easier, way to deal with this?

Thanks for any advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一瞬间的火花 2024-09-24 19:27:00

就我个人而言,如果我仅针对 ASP.NET(Web)进行开发,并且我知道类库仅针对 Web,那么根据需要引用 System.Web 或任何其他程序集不会出现任何问题。有时实用性应该是第一位的。

另一方面,如果您知道或不确定 BLL、DAL 或其他层是否可以在客户端服务器或其他环境中重用,您可能需要考虑使用更灵活的方法,例如会话处理程序接口等 。

最主要的是您清楚地了解最佳实践的建议 那时,您可以就哪些内容适用于每个项目或情况做出理性的决定。它并不总是像手套一样合身。

Personally, if I'm developing for ASP.NET (web) only, and I know the class libraries are only going to target the web, I have no issues referencing System.Web or any other assembly as needed. Sometimes practicality should come first.

On the other hand if you know, or are unsure if the BLL, DAL or other layers may be reused in a client-server or other environment, you may need to look at using a more flexible approach such as a session handler interface, etc.

The main thing is you clearly understand what best practice recommends. At that point you can make rational decisions about what should apply to each project or situation. It won't always fit like a glove.

青柠芒果 2024-09-24 19:27:00

根据我的经验,缓存是在某个层完成的 - 并且您所缓存的内容适用于该层的范围内,因此:

  • 您可以将数据缓存在 DAL 中,以便 DAL 从内存返回它,而不是再次访问数据库;所以在这里我们在数据级别缓存“原始”数据。
  • BL 可能会缓存类似的数据(例如 POCO 的)——换句话说,已应用 BL 的“逻辑”数据;所以这里我们在BL层缓存BL数据。
  • 您的 UI 可能会缓存(您猜对了)在 UI 层构建的信息 - 例如以网页、XML 等形式呈现数据。

当您查看缓存时,作为系统的架构师/设计者,您将做出决定关于哪些内容值得缓存,通常这是由平衡需求驱动的:

  • 性能需要达到特定目标。
  • 您有哪些时间(和成本)选择。

假设我们通过缓存来提高性能,您将需要分析您的系统并确定需要消除或避免的瓶颈 - 此分析的第一遍应该至少确定首先关注哪一层。

另一件需要考虑的事情是,您可以缓存的内容越靠近消费者,需要进行的整体处理就越少;换句话说,如果您可以在 UI 层进行缓存,那么 BL 和 DAL 层甚至不会被查看(您不需要在那里添加缓存)。

此时我想问你到底想要实现什么目标。

虽然我所说的一切都是正确的(据我所知),但这一切都基于这样的假设:我们正在提供系统功能的“垂直”部分(例如“创建发票”、“添加产品”等) ;您可以应用另一种模型 - 横切关注点模型。

考虑像日志记录这样的事情 - 系统的每个部分(UI / BL / DAL)都应该能够记录系统事件;我最喜欢的工具是 MS Enterprise Libraries (MSEntLibs)。当我与他们一起工作时,我认为他们是一个“黑匣子”——一个独立的单元。我(自愿)不知道它们是如何构建的 - 重要的是它们是隔离的并且具有相对易于管理的依赖关系。

MSEntLibs 可以登录到数据库,但如果我直接从 UI 调用 MSEntLibs 日志记录方法(登录到数据库)(并跳过我的 BL),我不在乎。

因此,根据您尝试执行的操作,正确的答案将是:

  • 简单地确定需要缓存的内容,让适当的层处理它。
  • 您根本不需要尝试使用 DI 将内容传递给您的 BL - 横切黑盒组件可能合适?

In my experience caching is done ata certain layer - and what you're caching applies within the scope of that layer, so:

  • You might cache data in the DAL so that the DAL returns it from memory and not by hitting the DB again; so here we're caching "raw" data at the data level.
  • The BL might cache similar data (say POCO's) - in other words "logical" data that has had BL applied to it; so here we're caching BL data in the BL level.
  • Your UI might cache (you guessed it) information that is built at the UI layer - liek a presentation of data as a webpage, XML, etc.

When you look at caching, as the architect / designer of the system you'll make decisions about what things are worth caching, generally this will be driven by a need to balance:

  • Performance needs to hit a specific target.
  • What time (and cost) options you have.

Assuming we're caching to increase performance, you'll want to analyse your system and identify the bottlenecks that need to be removed or avoided - the first pass of this analysis should at least identify which layer to focus on first.

Another thing to consider is that the more you can cache closer to the consumer - the less overall processing there is that needs to occur; in otherwords, if you can cache at the UI layer then the BL and DAL layers won't even get a look-in (you won't need to add caching there).

At this point I want to ask you what exactly it is that you're trying to achieve.

While everything I've said is true (as far as I am aware) it's all based on the assumption that we're delivering "vertical" slices of the systems functionality (like "creating an invoice", "adding a product" etc); there's another model you can apply - the cross-cutting concerns model.

Think about something like logging - every part of you system (UI / BL / DAL) should be able to log system events; my favourite tool for this is the MS Enterprise Libraries (MSEntLibs). When I work with them I consider them to be a "black-box" - a self contained unit. I (by choice) have no idea how they are architected - the thing that is important is that they are isolated and have dependencies which are relativly easy to manage.

The MSEntLibs can log to a database, but if I call an MSEntLibs logging method (to log to the DB) direct from my UI (and skipping over my BL) I don't care.

So depending on what you're trying to do the right anmswer will either be:

  • Simply identifying what needs to be cached at letting the appropriate layer handle it.
  • You don't need to try using DI to pass stuff to you BL at all - a cross-cutting black-box component might be appropriate?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文