什么是IRepository,它使用的是什么? [英] What is a IRepository and what is it used for?

查看:427
本文介绍了什么是IRepository,它使用的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是IRepository?为什么使用简单和简单的例子就不会受到伤害。

What is a IRepository? Why is it used, brief and simple examples won't hurt.

推荐答案

MVC促进了关注点分离,但是,这并不在并购V c时的水平停下来。

MVC promotes separation of concerns, but that doesn't stop at the M V C level.

数据访问本身就是一个问题。它应该在MVC的M位来完成,即模型。你如何构建你的模​​型是你的,但人们通常遵循久经考验的模式(为什么重新发明轮子?)。 Repository模式是目前的标准。不要指望一个简单的公式,但是,因为变化是多达有开发商,差不多了。

Data Access is a concern in itself. It should be done in the M bit of MVC, ie the model. How you structure your model is up to you, but people usually follow tried and tested patterns (why reinvent the wheel?). The Repository Pattern is the current standard. Don't expect a simple formula, however, because the variations are as many as there are developers, almost.

IRepository就是你创建一个接口(它不是MVC或ASP.NET或.NET的一部分)。它可以让你从真正的实现脱钩您的存储库。去耦是好事,因为这意味着你的code ...

IRepository is just an interface that you create (it is not part of MVC or ASP.NET or .NET). It allows you to "decouple" your repositories from real implementations. Decoupling is good because it means your code...:


  1. 您$​​ C $ c是更加重用。这只是普通的好。

  2. 您$​​ C $ C可使用控制反转(或依赖注入)。这是很好的保持你的关注很好的分离。这是特别好,因为这使得单元测试...

  3. 您$​​ C $ C进行单元测试。这与复杂的算法,大项目特别好。它无处不好事,因为它会增加你的,你正在使用的技术,您想在软件建模的领域的理解。

  4. 您$​​ C $ C成为各地的最佳实践,遵循共同的模式构建。这是好事,因为它使维护变得更加容易。

所以,已经卖给你去耦,回答你的问题是,IRepository是你创建一个接口,而且你让你的资料库继承。它为您提供了一个可靠的类层次的工作。

So, having sold you decoupling, the answer to your question is that IRepository is an interface that you create and that you make your Repositories inherit from. It gives you a reliable class hierarchy to work with.

我通常使用一个通用的IRepository。即:

I generally use a generic IRepository. Ie:

IRepository

IRepository

在哪里Tentity,没错,是一个实体。在code我用的是:

Where Tentity is, well, an entity. The code I use is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wingspan.Web.Mvc
{
    public interface IRepository<TEntity> where TEntity : class
    {
        List<TEntity> FetchAll();
        IQueryable<TEntity> Query {get;}
        void Add(TEntity entity);
        void Delete(TEntity entity);
        void Save();
    }
}

此接口的具体实现是:

A concrete implementation of this interface would be:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

using Wingspan.Web.Mvc;

namespace ES.eLearning.Domain
{
    public class SqlRepository<T> : IRepository<T> where T : class
    {
        DataContext db;
        public SqlRepository(DataContext db)
        {
            this.db = db;
        }

        #region IRepository<T> Members

        public IQueryable<T> Query
        {
            get { return db.GetTable<T>(); }
        }

        public List<T> FetchAll()
        {
            return Query.ToList();
        }

        public void Add(T entity)
        {
            db.GetTable<T>().InsertOnSubmit(entity);
        }

        public void Delete(T entity)
        {
            db.GetTable<T>().DeleteOnSubmit(entity);
        }

        public void Save()
        {
            db.SubmitChanges();
        }

        #endregion
    }
}

这让我写:

SqlRepository<UserCourse> UserCoursesRepository = new SqlRepository<UserCourse>(db);

其中db是DataContext的实例注入,比如说,一个服务。

Where db is a DataContext instance injected into, say, a Service.

使用UserCoursesRepository现在我可以写我的服务类的方法,如:

With UserCoursesRepository I can now write methods in my Service class like:

public void DeleteUserCourse(int courseId)
        {
            var uc = (UserCoursesRepository.Query.Where(x => x.IdUser == UserId && x.IdCourse == courseId)).Single();
            UserCoursesRepository.Delete(uc);
            UserCoursesRepository.Save();
        }

而现在在我的控制器,我可以这样写:

And now in my controllers, I can just write:

MyService.DeleteUserCourse(5);
MyService.Save();

也就是说,您的应用程序的开发变得更加导致了一个非常简单的控制器装配线。每一件流水线可以独立一切的进行测试,因此错误是扼杀在萌芽状态。

Ie, the development of your app becomes more of an assembly line that leads up to a VERY simple controller. Every piece of the assembly line can be tested independently of everything else, so bugs are nipped in the bud.

如果这是一个长期的,笨拙回答这是因为真正的答案是:

If this is a long, unwieldly answer it is because the real answer is:

采购史蒂芬·桑德森的书临ASP.NET MVC 2框架,学会思考MVC。

Buy Steven Sanderson's book Pro ASP.NET MVC 2 Framework and learn to think in MVC.

这篇关于什么是IRepository,它使用的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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