简体   繁体   English

这两个实体中的哪个实现在ASP.net MVC中更好

[英]Which of those two entities implementations are better in ASP.net MVC

I'm a beginner to ASP.net MVC. 我是ASP.net MVC的初学者。 And liking it a lot more than WebForms. 并且比WebForms更喜欢它。

So i decided to start a project which contains about 6 tables: 因此,我决定启动一个包含约6个表的项目:

  1. Gallery 画廊
  2. Admins 管理员
  3. Sessions 届会
  4. SessionImages SessionImages
  5. Offers 优惠
  6. Info 信息

I've created two projects : FP.WebUI and FP.Domain . 我创建了两个项目: FP.WebUIFP.Domain

Inside my domain project i've created 3 folders Abstract , Concrete and Entities . 在我的域项目中,我创建了3个文件夹AbstractConcreteEntities

Inside Abstract folder there are 6 interfaces IGallery , ISessions .. etc. Each interface has something like this : 在Abstract文件夹中,有6个接口IGalleryISessions等。每个接口都具有以下内容:

namespace FP.Domain.Abstract
{
    public interface IGallery
    {
        IQueryable<Gallery> Gallery { get; }

    }
}

And inside Concrete folder there are another 7 classes : EFDbGallery , EFDbSessions ... and EFDbContext which inherites from DbContext class. Concrete文件夹中还有另外7个类: EFDbGalleryEFDbSessions ...和EFDbContext ,它们继承自DbContext类。

Each class of the above (except EFDbContext ), implements each of the corresponding interface. 上面的每个类( EFDbContext除外)都实现了每个对应的接口。

Now when i thought about it i found that i could make one interface which defines all the entities and only one class inside Concrete folder which implements that interface. 现在,当我想到它时,我发现我可以创建一个定义所有实体的接口,而在实现该接口的Concrete文件夹中只有一个类。


I really don't know what's better : 我真的不知道有什么更好的方法:

6 interfaces , 6 classes for each entity. 6个接口 ,每个实体6个类

OR 要么

1 interface , 1 class which returns all entities. 1个接口1个类 ,返回所有实体。

You seem to have stumbled upon the Repository pattern. 您似乎偶然发现了Repository模式。 The typical architectural decision is to create an interface 典型的架构决策是创建一个接口

interface IRepository
{
    IQueryable<Gallery> Query { get; }
}

Then have your ORM class implement the repository interface. 然后让您的ORM类实现存储库接口。

class MyDbContext : DbContext , IRepository
{

}

Now at this point, to answer your question, should I use 6 classes and 6 interfaces or 1 class and 1 interface the answer is categorically NO! 现在,在这一点上,要回答您的问题,我应该使用6个类和6个接口还是1个类和1个接口,答案是肯定的否!

The typical pattern calls for a GENERIC interface (so in effect you have 6 interfaces, but a single interface source, if you need extra calls you can extend the generic). 典型的模式需要一个GENERIC接口(因此实际上有6个接口,但是只有一个接口源,如果需要额外的调用,则可以扩展通用接口)。

//Actually this implementation is edging on 
//Unit Of Work
interface IRepository<T>  
{
    IQueryable<T> Query { get; }
    void Insert(T item);
    void SaveChanges();
}

Then your EfContext exposes all of the interfaces. 然后,您的EfContext公开所有接口。 The reason being is that your controllers only typically need a single interface to work, and this makes mock/fake testing MUCH MUCH easier on your controllers (you don't need to create implementations of unused methods like an InfoQuery for a GalleryControllerTest). 原因是您的控制器通常只需要一个接口即可工作,这使得在控制器上进行模拟/伪造测试变得更加容易(您不需要为GalleryControllerTest创建未使用方法的实现,例如InfoQuery)。

If you find you need to have domain specific code for any interface you could extend the IRepository interface. 如果发现需要任何接口都有特定于域的代码,则可以扩展IRepository接口。

interface IGalleryRepository : IRepository<Gallery>  
{
    void SomeSpecialOperation(Gallery item);
}

Finally, implementation of this pattern will make life much easier if/when you start introducing Inversion of Control. 最后,如果/当您开始引入控制反转时,此模式的实现将使生活变得更加轻松。

PS I typically refactor out the ACTUAL EF code (ie the concrete Repository) into a separate assembly, this is simply in case you ever decide to drop EF from your project. PS I通常会将ACTUAL EF代码(即具体的存储库)重构为单独的程序集,以防万一您决定从项目中删除EF。

I would use 7 classes because they will have different responsibilities. 我将使用7个类,因为它们将承担不同的职责。

.

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

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