1

我的活動與使用模型,倉庫和控制器的ASP.NET Web API城堡Windsorized應用程序流的理解:如何告知Web API/Castle Windsor路由引擎在我的Repository中使用不同的數據庫實例?

0)客戶端調用通過REST方法的URI如:

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

1)Castle Windsor的路由引擎映射攔截傳入的調用,發送實現了接口platypiController的註冊具體類在其構造函數中作爲arg。

2)該構造函數決定調用哪個方法(在這種情況下對應於「Count」)。

3)Controller方法調用Repository上的對應方法。 4)代碼運行,數據收集和返回,用戶認爲它很容易(一個極端的觀點)或神奇的(另一個,稍微不太極端的觀點)。

我已經創建了一對利用它的項目,它迄今爲止工作只是丹迪。對於不同的用戶,我們有幾個數據庫實例(針對特定客戶的DB1,針對另一個客戶的DB2等)。表格幾乎不完全相同(不保證保持不變),對這些表格的查詢也是類似的。

我的難題/挑戰是如何或在哪裏攔截路由以這種方式進行,或基於哪個「用戶類」正在呼叫。

我想,我需要ň庫實現每個接口,如:

interface FooBar 

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

但後來,我怎麼告訴溫莎城堡或網頁API,具體的類/存儲庫我想要什麼?

在任何給定時間,都會有來自需要服務的客戶端的Web API/Castle Windsor應用程序請求DB#1數據,需要DB#2數據的其他客戶端,以及需要DB# 3數據。

這東西是在URI來實現,如:

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

(其中附加的數字表示要使用哪個DB)

或:

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

或... ???

在許多情況下,必須在一個Repository類和另一個Repository類之間進行更改的唯一事情是構造函數中的連接字符串。具體來說,這個:

@"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")) 

...將需要:

@"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)

+1

可能注入不同的DbContext到你的倉庫可能是解決方案。 – Fals

+0

這是如何完成的? –

回答

1

你可以使用依賴注入到你的DbContext放入的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; 
    } 
} 

我只是從我的一個項目中複製並通過代碼,通過使用與dbcontext相同的方式你的應用程序。

也看看這個解決方案: Multiple DbContexts in N-Tier Application

相關問題