简体   繁体   English

ASP.NET MVC通用存储库的实现和用法

[英]ASP.NET MVC generic repository implementation and usage

I've decided to develop a generic repository inside my ASP.NET MVC application, nevertheless I have some issues with implementation. 我已经决定在ASP.NET MVC应用程序中开发一个通用存储库,尽管如此,我在实现方面还是有一些问题。 I will try to describe my problem below. 我将在下面尝试描述我的问题。

This is my generic repository: 这是我的通用存储库:

public interface IGenericRepository<T> where T : class
{
    IQueryable<T> GetAll();
    IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
    void Add(T entity);
    void Delete(T entity);
    void Edit(T entity);
    void Save();
}

public interface IArtistRepository : IGenericRepository<Artist>
{
    Artist GetSingle(int fooId);
}

public abstract class GenericRepository<C, T> : IGenericRepository<T> where T : class where C : DbContext, new()
{
    public C Context { get; set; } = new C();

    public virtual IQueryable<T> GetAll()
    {
       IQueryable<T> query = Context.Set<T>();
       return query;
    }

    public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
    {
       IQueryable<T> query = Context.Set<T>().Where(predicate);
       return query;
    }

    public virtual void Add(T entity)
    {
       Context.Set<T>().Add(entity);
    }

    public virtual void Delete(T entity)
    {
       Context.Set<T>().Remove(entity);
    }

    public virtual void Edit(T entity)
    {
       Context.Entry(entity).State = EntityState.Modified;
    }

    public virtual void Save()
    {
       Context.SaveChanges();
    }
}

Concrete implementations: 具体实现:

public class ArtistRepository :
      GenericRepository<MusicContextSqlServer, Artist>, IArtistRepository
{

    public Artist GetSingle(int artistId)
    {
       var query = GetAll().FirstOrDefault(x => x.ArtistId == artistId);
       return query;
    }
}    

Now when building controller, let's take Artist controller as example: 现在在构建控制器时,让我们以Artist控制器为例:

This will be the constructor: 这将是构造函数:

public class ArtistsController : Controller
{
     private readonly IArtistRepository _artistRepo;

     public ArtistsController(IArtistRepository artistRepo)
     {
         _artistRepo = artistRepo;
     }
}

Now when it comes to Get artists, I could use: 现在,当涉及到获取艺术家时,我可以使用:

public ActionResult Index()
{
    var model = _artistRepo.GetAll();
    return View(model.ToList());
}

however I also need to include ArtistDetails dbset, normally using context embedded inside controller I could do like this: 但是我还需要包括ArtistDetails dbset,通常使用嵌入控制器内部的上下文,我可以这样:

(db is in this case normal context embedded in controller) (在这种情况下,db是嵌入在控制器中的普通上下文)

public ActionResult Index()
{
    var artist = db.Artist.Include(a => a.ArtistDetails);                     
    return View(artist.ToList());
}

But after using repositories, I am not sure where could I add this Include() function to make it work along my repositories. 但是在使用存储库之后,我不确定在哪里可以添加此Include()函数来使其在我的存储库中正常工作。 At the moment I can just do GetAll() coming from my repository. 目前,我可以从存储库中GetAll() Same situation with call to db.ArtistDetails inside SelectList. 与调用SelectList中的db.ArtistDetails情况相同。

Same situation is with Create , normally I would use: (db is in this case normal context embedded in controller) Create情况相同,通常我会使用:(在这种情况下,db是嵌入控制器中的普通上下文)

public ActionResult Create()
{
     ViewBag.ArtistId = new SelectList(db.ArtistDetails, "ArtistId", "Bio");       
     return View();       
}

public ActionResult Create([Bind(Include = "ArtistId,Name")] Artist artist)
{
      if (ModelState.IsValid)
      {
          db.Artist.Add(artist);
          db.SaveChanges();
          return RedirectToAction("Index");
      }

      ViewBag.ArtistId = new SelectList(db.ArtistDetails, "ArtistId", "Bio", artist.ArtistId);

       return View(artist);
}

How can I convert those methods using repositories I use. 如何使用我使用的存储库转换那些方法。 Summarizing how to change those methods after I implemented my repository, where should I make changes and how it would look like, could anybody show solution as an answer. 总结在实现存储库后如何更改这些方法,应该在哪里进行更改以及外观如何,任何人都可以将解决方案作为答案。 Thanks. 谢谢。

Since you inherit a public property Context , which will be available for artist repository to use. 由于您继承了公共属性Context ,因此艺术家存储库可以使用它。 I would introduce another method to Artist repository as below: 我将向Artist repository介绍另一种方法,如下所示:

public Artist GetSingleWithDetails(int artistId)
    {
       return = Context.Set<Artist>().Include(a => a.ArtistDetails).FirstOrDefault(x => x.ArtistId == artistId);

    }

Ideally Context property should be protected so that its available to subclasses but not available publicly 理想情况Context应该保护Context属性,以便其可用于子类,但不可公开使用

EDIT : For ArtistDetailsRepository to GetAll ArtistDetails 编辑 :对于ArtistDetailsRepository到GetAll ArtistDetails

Interface: 接口:

public interface IArtistDetailsRepository : IGenericRepository<ArtistDetails>
{        
}

Repository: 库:

public class ArtistDetailsRepository :
      GenericRepository<MusicContextSqlServer, ArtistDetails>, IArtistDetailsRepository
{   
}    

Create: 创造:

ViewBag.ArtistId = new SelectList(artistDetailRepository.GetAll.ToList(), "ArtistId", "Bio", artist.ArtistId);

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

相关问题 ASP.NET MVC通用存储库 - ASP.NET MVC generic repository ASP.NET 5 MVC 6通用存储库模式 - ASP.NET 5 MVC 6 Generic Repository Pattern ASP.NET 核心,基于通用存储库接口的通用 controller 实现 - ASP.NET Core, generic controller implementation based on generic repository interface 在我的asp.net MVC Web应用程序中获取通用存储库+子存储库+ UnitOfWork - Getting Generic Repository + Child repository + UnitOfWork inside my asp.net mvc web application asp.net通用存储库按属性查找 - asp.net Generic repository find by property 在ASP.NET MVC 5中使用具有通用存储库模式UnitOfWork的继承接口的方法 - Using Method of Inherited Interface with Generic Repository Pattern, UnitOfWork in ASP.NET MVC 5 在Asp.Net MVC Core中创建Controller时的IdentityUser通用存储库 - IdentityUser generic repository when we create Controller in Asp.Net MVC Core 应用程序设计:NH会话管理,通用存储库,ASP.NET MVC - Application design: NH-session management, generic repository, ASP.NET MVC 如何从SP和ASP.NET MVC中的通用存储库获取行数 - How to get count of rows from SP & generic repository in asp.net mvc ASP.NET MVC HTML Helper的用法 - ASP.NET MVC Html Helper usage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM