Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 如何在ASP.NET MVC 3中分离代码(干净的代码分离)?_Asp.net Mvc 3_Entity Framework - Fatal编程技术网

Asp.net mvc 3 如何在ASP.NET MVC 3中分离代码(干净的代码分离)?

Asp.net mvc 3 如何在ASP.NET MVC 3中分离代码(干净的代码分离)?,asp.net-mvc-3,entity-framework,Asp.net Mvc 3,Entity Framework,我现在只想知道如何在MVC3中使用EF以正确的方式分离代码 根据我的项目结构 演示->视图和控制器 域-->模型(业务实体) 数据-->存储库、IRepository、ApplicationDbContext 服务-->第三方服务(贝宝、短信)或应用程序服务 ApplicationDbContext需要模型作为参考 public sealed class ApplicationDbContext : DbContext { public DbSet<CountryModel>

我现在只想知道如何在MVC3中使用EF以正确的方式分离代码

根据我的项目结构

演示->视图和控制器

域-->模型(业务实体)

数据-->存储库、IRepository、ApplicationDbContext

服务-->第三方服务(贝宝、短信)或应用程序服务

ApplicationDbContext需要模型作为参考

public sealed class ApplicationDbContext : DbContext
{

    public DbSet<CountryModel> Country { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}
公共密封类ApplicationDbContext:DbContext
{
公共数据库集国家{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
}
}
1。那么,在数据中放置DbContext好吗?或者我必须将其移动到域中?

现在在控制器中,我必须编写一个代码

using (ApplicationDbContext db = new ApplicationDbContext())
{
    var countryRepository = new Repository<Country>(db);

    if (ModelState.IsValid)
    {
        countryRepository.insert(country);
        db.SaveChanges();
    }
}
使用(ApplicationDbContext db=new ApplicationDbContext())
{
var countryRepository=新存储库(db);
if(ModelState.IsValid)
{
countryRepository.插入(国家);
db.SaveChanges();
}
}
有没有办法将此代码块与任何业务层/服务层分开

所以我们只需从controller调用该层&只需传递特定的业务实体即可执行其余的操作

我想使用MVC 3&EF执行PL-->BLL-->DLL方法


请告诉我正确的方法

u可以在BLL中为每个业务实体创建单独的项目,调用存储库表单,并创建一些需要的基本函数,如insert delete find select with parameter等

var country =new Country();//it's class of BLL

if (ModelState.IsValid)
{
    country.insert(country);
}
诸如此类

那么,在数据中放置DbContext好吗

是的,它就在它该去的地方

现在在控制器中,我必须编写一个代码

using (ApplicationDbContext db = new ApplicationDbContext())
{
    var countryRepository = new Repository<Country>(db);

    if (ModelState.IsValid)
    {
        countryRepository.insert(country);
        db.SaveChanges();
    }
}
不,您绝对不应该在控制器中编写这样的代码,因为您现在正在使控制器与您正在使用的特定数据访问技术(在您的情况下是EF)强耦合。更糟糕的是,您将无法单独对控制器进行单元测试

我建议您对接口中实体的操作进行抽象(您已经在问题-
IRepository
)中提到了它)。现在,您的控制器可以将存储库作为依赖项:

public class CountriesController: Controller
{
    private readonly IRepository repository;
    public CountriesController(IRepository repository)
    {
        this.repository = repository;
    }

    public ActionResult Index(int id)
    {
        Country country = this.repository.Get<Country>(id);
        return View(country);
    }


    [HttpPost]
    public ActionResult Index(Country country)
    {
        if (ModelState.IsValid)
        {
            this.repository.Insert(country); 
            return RedirectToAction("Success");
        }

        return View(country);
    }
}
公共类countries控制器:控制器
{
私有只读存储库;
公共国家/地区控制器(IRepository存储库)
{
this.repository=存储库;
}
公共行动结果索引(int id)
{
Country=this.repository.Get(id);
返回视图(国家);
}
[HttpPost]
公共行动结果指数(国家)
{
if(ModelState.IsValid)
{
this.repository.Insert(国家);
返回操作(“成功”);
}
返回视图(国家);
}
}
现在,您所要做的就是配置您最喜欢的依赖项注入框架,将此IRepository的特定实现注入控制器构造函数。在您的例子中,这个特定的实现可能是实现IRepository接口的某个类,该类在内部使用
ApplicationDbContext


通过这种方式,您可以从控制器中抽象出数据访问逻辑。

Greate,但是如果我有多个接口,我应该怎么做?比如说**CountryRepository:IRepository,ICountryRepository**那么在CountryController(IRepository,ICountryRepository)中,我必须这样写吗?在CountryRepository中,我必须编写与ApplicationDbContext相关的代码?IRepository可以是通用的,如我的示例所示:
IRepository
。因此,您的
CountryRepository
可以实现
IRepository
,然后您的控制器将执行
IRepository
。这就是你所需要的。现在在
CountryRepository
实现中,您可以使用
ApplicationDbContext
。例如,在
CountryRepository
的Get方法中:
使用(ApplicationDbContext db=newapplicationdbcontext()){return db.Countries.FirstOrDefault(c=>c.CountryId==id);}
可以给出代码ApplicationDbContext与RepositoryBase或CountryRepository的示例吗?如果我必须在CountryRepository中实现另一个ICountryInterface,那么在CountryController中,我必须通过iRepository和iCountryRepository权限!!!不需要,您不需要将任何
icontrypository
传递给控制器。没有这样的接口。您的控制器只需要一个
i存储库
。您的
CountryRepository
类实现了
IRepository
。如果您的控制器需要与其他模型一起工作,您只需传递它们的存储库:
IRepository
IRepository
。。。