如何架构师EF一个ASP.Net MVC应用程序? [英] How do architect an ASP.Net MVC app with EF?

查看:110
本文介绍了如何架构师EF一个ASP.Net MVC应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦概念化一点点如何构建一个MVC应用程序与实体框架成n层应用程序。

I'm having a little bit of trouble conceptualizing how to architect an MVC App with Entity Framework into a n-tiered app.

正常,教材3层的应用应该是这个样子。

The normal, textbook 3-tiered app should look something like

数据访问 - >业务逻辑 - > presentation

Data Access->Business Logic->Presentation

在presentation不应该知道的数据访问任何东西。随着EF,所有层需要了解模型。所以,我现在的建筑看起来更像

The presentation shouldn't know anything about the data access. With EF, ALL layers need to know about the model. So my architecture now looks more like

Data Access->Business Logic
     |               |
      ---------------
             |
            MVC

我失去了一些东西在这里还是我在错误的方式思考这个?

Am I missing something here or am I thinking about this in the wrong way?

我应该在业务逻辑思维EF本身作为数据访问层,并把实体?

Should I be thinking of EF itself as the data access layer and put the entities in the business logic?

推荐答案

好吧,我想你的问题是,如何构建层的MVC应用程序。
看看这个简单的架构,我用它为我的MVC应用程序,它似乎是清洁和高效。

Well, I suppose your question is, how to architect "layers" in MVC application. Take a look at this simple architecture, I use it for my MVC apps and it seems to be clean and efficient.


  1. 在解决方案项目 - 商业模式 - 简单的类库充满POCO类的再presenting业务领域。您可以使用此数据注解,元数据类验证逻辑,等等。

  1. project in solution - business model - simple class library full of POCO classes representing business domain. You can use data annotation here, metadata classes for validation logic, etc.

项目 - EF-基础信息库 - 另一个简单的类库;这里定义的上下文(EF code首先是伟大的,但你可以先使用EF数据库第一或模型 - 你只需要POCO T4模板添加到商业模式的类库,没什么大不了的),并设置类 - 库

project - EF-based repositories - another simple class library; here is defined context (EF code first is great, but you can use EF database first or model first - you just have to add POCO T4 template to the business model class library, no big deal) and set of classes - repositories

项目 - 我通常把它称为ServiceLayer左右(我打开的建议为更好的名字:) - 它仅包含接口,仓库及其他服务(在单独的项目来实现),将我的MVC(或任何其他技术)基础的应用使用;从二,项目库实现这些接口

project - i usually call it "ServiceLayer" or so (i am open for suggestion for better name :) - it contains only interfaces, for repositories and other services (implemented in separate projects) which will my MVC (or any other technology) based application use; repositories from 2.project implement these interfaces

项目 - MVC的网站。它使用依赖注入(建立DependencyResolver,我爱用Ninject容器)映射库(和其他服务);然后就可以使用构造注入控制器,或者一些懒的方法(见下文)

project - MVC website. It uses dependency injection (build in DependencyResolver, and i love to use Ninject container) for mapping repositories (and other services); Then you can use constructor injection into controllers, or some "lazy" approach (see below)

这看起来是这样的:

瘦控制器:


public class SomethingController : BaseController
{
    public ActionResult DoSomething(SomeBusinessThing input)
    {
         if (ModelState.IsValid)
         {
             var result = CustomerRepository.DoSomeBusinessLogicAndPersistenceAndStuff(input);
             return View(result); // you can use AutoMapper here, if you dont want to use business object as viewmodels
         }
    }
}

我的仓库财产是从我BaseController继承:

My repository "property" is inherited from my BaseController :


public class BaseController : Controller 
{
    // ... other stuff used by all (or multiple) controllers

    private ICustomerRepository _customerRepository;
        protected ICustomerRepository CustomerRepository
        {
            get
            {
                if (_customerRepository== null)
                    _customerRepository= DependencyResolver.Current.GetService();
                return _customerRepository;
            }
        }
}

可以,如果你的控制器使用的许多服务都使用这种懒惰的DI,但每个动作只有1-2,所以这将是一种低效与构造注入所有的人。有人可以告诉你是隐藏的依赖这种方式,但是如果你把所有这些东西在一个地方 - BaseController,它没有什么大不了的。

You can use this "lazy" DI if your controller use many services, but only 1-2 per action, so it would be kind of inefficient to inject all of them with constructor. Somebody could tell you are "hiding" dependency this way, but if you keep all this stuff in one place - BaseController, its no big deal.

好吧,实现仓库的真的是你的业务。 MVC应用程序甚至不知道自己使用的是EF,它知道服务的唯一接口和犯规约垫层实施护理(你可以切换以后,如果你需要的任何时间!)

Well, implementation of repository is really your business. MVC application dont even know you are using EF, it knows only interfaces of services and doesnt care about underlaying implementation (which you can switch any time later if you need to !)

Conslusion:


  • 控制器是瘦的 - 没有业务逻辑

  • Controllers are skinny - no business logic

型号是FAT - 在这种情况下,库封装了所有的业务逻辑(可以肯定的是使用其他类型的服务,以及,例如一些计算器处理等,记住,MVC不关心,只知道接口)

Model is FAT - and in this case, repositories encapsulate all the business logic (you can sure use other type of services as well, for example some calculators for processing etc., remember, MVC doesnt care, knows only interfaces)

的ViewModels是意见输入(和视图模型可以是你的商业模式直接,或者您可以使用AutoMapper创建纯粹的ViewModels)

ViewModels are input for Views (and ViewModel can be your business model directly, or you can use AutoMapper for creating "pure" ViewModels)

这篇关于如何架构师EF一个ASP.Net MVC应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆