Question

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

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

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

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) That constructor determines which of its methods is to be called (what corresponds to "Count" in this case).

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

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).

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.

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

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

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.

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

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

(where the appended number indicates which DB to use)

?

or:

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

or...???

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 becomes OMDDAT01)

Was it helpful?

Solution

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();
    }
}

and finally:

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;
    }
} 

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top