简体   繁体   English

ASP.NET MVC 应用程序的架构

[英]Architecture of an ASP.NET MVC application

I'm in the process of doing the analysis of a potentially big web site, and I have a number of questions.我正在分析一个潜在的大型 web 站点,我有很多问题。

The web site is going to be written in ASP.NET MVC 3 with razor view engine. web 站点将使用 razor 视图引擎在 ASP.NET MVC 3 中编写。 In most examples I find that controllers directly use the underlying database (using domain/repository pattern), so there's no WCF service in between.在大多数示例中,我发现控制器直接使用底层数据库(使用域/存储库模式),因此两者之间没有 WCF 服务。 My first question is: is this architecture suitable for a big site with a lot of traffic?我的第一个问题是:这种架构适合流量很大的大网站吗? It's always possible to load balance the site, but is this a good approach?总是可以对站点进行负载平衡,但这是一个好方法吗? Or should I make the site use WCF services that interact with the data?还是应该让网站使用与数据交互的 WCF 服务?

Question 2: I would like to adopt CQS principles, which means that I want to separate the querying from the commanding part.问题2:我想采用CQS原则,也就是说我想把查询和指挥分开。 So this means that the querying part will have a different model (optimized for the views) than the commanding part (optimized to business intend and only containing properties that are needed for completing the command) - but both act on the same database.因此,这意味着查询部分将具有不同的 model(针对视图进行了优化)与命令部分(针对业务意图进行了优化并且仅包含完成命令所需的属性) - 但两者都作用于同一个数据库。 Do you think this is a good idea?你认为这是个好主意吗?

Thanks for the advice!感谢您的建议!

  1. For scalability, it helps to separate back-end code from front-end code.对于可扩展性,它有助于将后端代码与前端代码分开。 So if you put UI code in the MVC project and as much processing code as possible in one or more separate WCF and business logic projects, not only will your code be clearer but you will also be able to scale the layers/tiers independently of each other.因此,如果您将 UI 代码放在 MVC 项目中,并将尽可能多的处理代码放在一个或多个单独的 WCF 和业务逻辑项目中,那么您的代码不仅会更清晰,而且您还可以独立地扩展层/层其他。

  2. CQRS is great for high-traffic websites. CQRS 非常适合高流量网站。 I think CQRS, properly combined with a good base library for DDD, is good even for low-traffic sites because it makes business logic easier to implement.我认为 CQRS 与 DDD 的良好基础库适当结合,即使对于低流量站点也很好,因为它使业务逻辑更容易实现。 The separation of data into a read-optimized model and a write-optimized model makes sense from an architectural point of view also because it makes changes easier to do (maybe some more work, but it's definitely easier to make changes without breaking something).从架构的角度来看,将数据分离为读取优化的 model 和写入优化的 model 也是有意义的,因为它使更改更容易进行(可能需要更多的工作,但在不破坏某些东西的情况下进行更改肯定更容易)。

However, if both act on the same database, I would make sure that the read model consists entirely of Views so that you can modify entities as needed without breaking the Read code.但是,如果两者都作用于同一个数据库,我会确保读取的 model 完全由视图组成,以便您可以根据需要修改实体而不会破坏读取代码。 This has the advantage that you'll need to write less code, but your write model will still consist of a full-fledged entity model rather than just an event store.这样做的好处是您需要编写更少的代码,但是您编写的 model 仍将包含一个成熟的实体 model 而不仅仅是一个事件存储。

EDIT to answer your extra questions:编辑以回答您的额外问题:

What I like to do is use a WCF Data Service for the Read model.我喜欢做的是使用 WCF 数据服务来读取 model。 This technology (specific to .NET 4.0) builds an OData (= REST + Atom with LINQ support) web service on top of a data model, such as an Entity Framework EDMX. This technology (specific to .NET 4.0) builds an OData (= REST + Atom with LINQ support) web service on top of a data model, such as an Entity Framework EDMX.

So, I build a Read model in SQL Server (Views), then build an Entity Framework model from that, then build a WCF Data Service on top of that, in read-only mode. So, I build a Read model in SQL Server (Views), then build an Entity Framework model from that, then build a WCF Data Service on top of that, in read-only mode. That sounds a lot more complicated than it is, it only takes a few minutes.这听起来比实际复杂得多,只需要几分钟。 You don't need to create yet another model, just expose the EDMX as read-only.您无需再创建另一个 model,只需将 EDMX 公开为只读即可。 See also http://msdn.microsoft.com/en-us/library/cc668794.aspx .另请参阅http://msdn.microsoft.com/en-us/library/cc668794.aspx

The Command service is then just a one-way regular WCF service, the Read service is the WCF Data Service, and your MVC application consumes them both.然后,命令服务只是单向常规 WCF 服务,读取服务是 WCF 数据服务,您的 MVC 应用程序同时使用它们。

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

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