我怎么能告诉Web API /温莎城堡路由引擎在我的仓库使用不同的数据库实例? [英] How can I tell the Web API / Castle Windsor routing engine to use a different database instance in my Repository?

查看:152
本文介绍了我怎么能告诉Web API /温莎城堡路由引擎在我的仓库使用不同的数据库实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动与使用模式,仓库和控制器的ASP.NET Web API城堡Windsorized应用程序流的理解:

My understanding of the flow of events with an ASP.NET Web API Castle Windsorized app that uses Models, Repositories, and Controllers:

0)的客户端调用通过REST方法的URI,例如:

0) The client calls a REST method via a URI such as:

http://localhost:28642/api/platypi/Count

1),温莎城堡的路由引擎映射传入呼叫,发送实现接口platypiController注册的具体类具有作为其构造方法的ARG拦截。

1) Castle Windsor's routing engine maps intercepts that incoming call, sending the registered concrete class that implements the interface platypiController has as an arg in its constructor.

2)表示的构造决定了它的方法是被称为(什么对应在这种情况下,以计数)。

2) That constructor determines which of its methods is to be called (what corresponds to "Count" in this case).

3)该控制器方法调用的库相应的方法。

3) That Controller method calls a corresponding method on the Repository.

4)code运行时,数据收集和返回,用户认为这一切都那么容易的(一个极端的观点)或魔法(另一个略少极端的观点)。

4) The code is run, the data gathered and returned, and the user thinks it's all so easy (one extreme viewpoint) or magical (another, slightly less extreme, viewpoint).

我创建了一个对利用这一项目,它至今工作只是花花公子。我们为表差不多,但不完全相同(不保证保持这样的)不同用户(DB1为特定的客户群,DB2再等)数据库的多个实例,以及对这些表查询是相似的。

I've created a pair of projects that utilize this and it so far works just dandy. We have several instances of databases for different users (DB1 for a particular set of customers, DB2 for another, etc.) The tables are almost but not quite identical (not guaranteed to remain such), and the queries against those tables are similar.

我的难题/挑战是如何或在哪里拦截路由走这条路,或者在此基础上的用户下课呼吁。

My conundrum/challenge is how or where to intercept the routing to go this way or that based on which "class" of user is calling.

我在想,我要n个库实现每个接口,如:

I'm thinking that I need N Repositories implementing each interface, such as:

interface FooBar

class PhooBar : FooBar // targets DB#1
class PhooeyBar : FooBar // targets DB#2
class PoohBear : FooBar // targets DB#3

不过,我怎么告诉温莎城堡或Web API的具体类/存储库我想要的?

But then, how do I tell Castle Windsor or Web API which concrete class/Repository I want?

在任何给定的时间,就会有进入的网络API /温莎城堡的应用程序从需要谁送达DB#1的数据,谁需要DB#2数据的其他客户,但谁需要DB#3用户客户端的请求数据。

At any given time, there will be requests coming into the Web API / Castle Windsor app from clients who need to be served DB#1 data, other clients who need DB#2 data, and yet users who need DB#3 data.

这东西是在URI来实现,如:

Is this something that's accomplished in the URI, such as:

http://localhost:28642/api/platypi/Count/1 

(其中,在所附的数字表示要使用哪个DB)

(where the appended number indicates which DB to use)

http://localhost:28642/api/platypi/Count/PhooBar

或... ???

在很多情况下,这将有唯一一个repository类之间,改变另一个是在构造函数中的连接字符串。具体做法是:

In many cases, the ONLY thing that will have to change between one Repository class and another is the connection string in the constructor. Specifically, this:

@"Provider=Microsoft.ACE.OLEDB.12.0;User ID=qypav1;Password=QqPamPoamMSET;Data Source=C:\CatcherNTheRye\DATA\OMDDAT03.MDB;Jet OLEDB:System database=C:\Catch22\Data\trip.mdw"))

...将需要:

...will need to be:

@"Provider=Microsoft.ACE.OLEDB.12.0;User ID=qypav1;Password=QqPamPoamMSET;Data Source=C:\CatcherNTheRye\DATA\OMDDAT01.MDB;Jet OLEDB:System database=C:\Catch22\Data\trip.mdw"))

(OMDDAT03成为OMDDAT01)

(OMDDAT03 becomes OMDDAT01)

推荐答案

您可以使用依赖注入到你的DbContext放入的UnitOfWork:

you can use dependency injection to place your dbContext into UnitOfWork :

public interface IRepository<T> where T : class
{
    IQueryable<T> GetAll();

    void Add(T entity);

    void Delete(T entity);

    void DeleteAll(IEnumerable<T> entity);

    void Update(T entity);

    bool Any();
}

public class Repository<T> : IRepository<T> where T : class
{
    private readonly IDbContext _context;
    private readonly IDbSet<T> _dbset;

    public Repository(IDbContext context)
    {
        _context = context;
        _dbset = context.Set<T>();
    }

    public virtual IQueryable<T> GetAll()
    {
        return _dbset;
    }

    public virtual void Add(T entity)
    {
        _dbset.Add(entity);
    }

    public virtual void Delete(T entity)
    {
        var entry = _context.Entry(entity);
        entry.State = EntityState.Deleted;
        _dbset.Remove(entity);
    }

    public virtual void DeleteAll(IEnumerable<T> entity)
    {
        foreach (var ent in entity)
        {
            var entry = _context.Entry(ent);
            entry.State = EntityState.Deleted;
            _dbset.Remove(ent);
        }
    }

    public virtual void Update(T entity)
    {
        var entry = _context.Entry(entity);
        _dbset.Attach(entity);
        entry.State = EntityState.Modified;
    }

    public virtual bool Any()
    {
        return _dbset.Any();
    }
}

最后:

public interface IUnitOfWork : IDisposable
{
    IRepository<TEntity> GetRepository<TEntity>() where TEntity : class;

    void Save();
}

public class UnitOfWork<TContext> : IUnitOfWork where TContext : IDbContext, new()
{
    private readonly IDbContext _ctx;
    private readonly Dictionary<Type, object> _repositories;
    private bool _disposed;

    public UnitOfWork()
    {
        _ctx = new TContext();
        _repositories = new Dictionary<Type, object>();
        _disposed = false;
    }

    public IRepository<TEntity> GetRepository<TEntity>() where TEntity : class
    {
        if (_repositories.Keys.Contains(typeof(TEntity)))
        {
            return _repositories[typeof(TEntity)] as IRepository<TEntity>;
        }
        var repository = new Repository<TEntity>(_ctx);

        _repositories.Add(typeof(TEntity), repository);

        return repository;
    }

    public void Save()
    {
        _ctx.SaveChanges();
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (this._disposed) return;

        if (disposing)
        {
            _ctx.Dispose();
        }

        this._disposed = true;
    }
} 

我只是复制和过去的从我的项目之一的code,使用你可以有几个的DbContext在您的应用程序是一样的。

I just copy and past the code from one of my projects, by using the same way you can have several dbcontext in your application.

也看看这个解决方案:
在N层应用程序 多DbContexts

also take a look at this solution: Multiple DbContexts in N-Tier Application

这篇关于我怎么能告诉Web API /温莎城堡路由引擎在我的仓库使用不同的数据库实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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