如何在mvc 5中创建存储库模式中的搜索 [英] how to create searching in repository pattern in mvc 5

查看:51
本文介绍了如何在mvc 5中创建存储库模式中的搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

这里我想使用存储库模式创建搜索功能。在这里我使用了存储过程。请帮助我,因为我是mvc5的新手,所以需要技术支持。

hi to all,
Here i want to create searching functionality using repository pattern. And here i used store procedure .Kindly help me because i'm new for mvc5 so kindly need technical support.

推荐答案

有几种实现存储库模式的方法。 br />


通常取决于你需要这种模式的原因。


例如,如果你需要这种模式创建一种访问不同类型的数据库(sql,mysql,mongo)的常用方法,然后你需要创建一个可以在它们两个中工作的搜索。



如果你需要一个简单的包装器实体框架

这就是我使用的那个:



there are several way to implement repository pattern.

usually it depends on the reason you need this pattern.

for example if you need this pattern to create a common way to access different kinds of databases (sql, mysql, mongo) then you need to create a "searching" that can works in both of them.

if you need a simple wrapper over entity framework
that's the one i use :

public interface IRepository<TEntity>
 where TEntity : class
{
    int BulkInsert(System.Linq.IOrderedEnumerable<TEntity> items);
    int Delete(TEntity item);
    int DeleteRange(System.Collections.Generic.IEnumerable<TEntity> items);
    System.Collections.Generic.IEnumerable<TEntity> Get();
    TEntity Get(params object[] keys);
    System.Collections.Generic.IEnumerable<TEntity> Get(Func<tentity,bool> lambda);
    int Insert(TEntity item);
    int Update(TEntity item);
    void Dispose();
}

public abstract class BaseRepository<TContext,TEntity> : IDisposable, IRepository<TEntity>
    where TEntity : class
    where TContext : DbContext, new()
{
    public TContext Context { get; private set; }
    private DbSet<TEntity> DbSet { get { return Context.Set<TEntity>(); } }

    public BaseRepository()
    {
        Context = new TContext();
    }

    public TEntity Get(params object[] keys)
    {
        Context.Configuration.AutoDetectChangesEnabled = false;
        var e = DbSet.Find(keys);
        Context.Configuration.AutoDetectChangesEnabled = true;
        return e;
    }
    public IEnumerable<TEntity> Get()
    {
        Context.Configuration.AutoDetectChangesEnabled = false;
        var e = DbSet.ToList();
        Context.Configuration.AutoDetectChangesEnabled = true;
        return e;
    }
    public IEnumerable<TEntity> Get(Func<tentity,> lambda)
    {
        Context.Configuration.AutoDetectChangesEnabled = false;
        var e = DbSet.Where(lambda).ToList();
        Context.Configuration.AutoDetectChangesEnabled = true;
        return e;
    }
}

///
/// Instance of your entity framework context
///
public class TestRepository<TEntity> : BaseRepository<dbtest,>
    where TEntity : class
{
    public TestRepository()
        : base()
    {
    }
}

// Usage
var users = new TestRepository< User>().Get(u => u.PhoneNumber.StartsWith("00") && u.IsDeleted == 0);





请注意我的完整版实现了idisposable,因此您可以将新的TestRepository<用户>()在using语句



note my full version implements idisposable so you can put your new TestRepository< User>() inside a using statement


这篇关于如何在mvc 5中创建存储库模式中的搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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