当前位置: 首页 > 编程语言 > C#

如何告诉WebAPI-CastleWindsor路由引擎为我的存储库使用不同的数据库实例?

时间:2023-04-10 22:42:48 C#

我如何告诉WebAPI/CastleWindsor路由引擎为我的存储库使用不同的数据库实例?我有一个ASP.NETWebAPICastleWindsorized应用程序,它使用模型、存储库和控制器来了解事件流:0)客户端通过URI调用REST方法,例如:http://localhost:28642/api/platypi/Count1)CastleWindsor的路由引擎映射拦截传入调用,并在其构造函数中将实现接口platypiController的注册具体类作为arg发送。2)这个构造函数决定调用哪个方法(对应本例中的“Count”)。3)Controller方法调用Repository上对应的方法。4)代码运行,数据被收集并返回,用户认为这一切都很简单(一个极端的观点)或神奇的(另一个稍微不那么极端的观点)。我已经创建了几个利用它的项目,到目前为止它只是花花公子。我们有多个数据库实例(针对特定客户端的DB1,针对另一个客户端的DB2,等等)用于不同的用户。这些表几乎但不完全相同(不保证保持相同),并且针对这些表的查询是相似的。我的困境/挑战是如何或在哪里以这种方式或基于用户调用的“类”拦截路由。我想我需要N个存储库来实现每个接口,例如:interfaceFooBarclassPhooBar:FooBar//targetsDB#1classPhooeyBar:FooBar//targetsDB#2classPoohBear:FooBar//targetsDB#3But,IhowdoI告诉CastleWindsor或WebAPI我想要的特定类/存储库?在任何给定时间,都会有来自WebAPI/CastleWindsor应用程序的请求,这些请求来自需要服务DB#1数据的客户端、需要DB#2数据的其他客户端以及需要DB#3数据的用户。这是在URI中完成的,例如:http://localhost:28642/api/platypi/Count/1(附加的数字表示要使用哪个数据库)?或者:http://localhost:28642/api/platypi/Count/PhooBar或者...???在许多情况下,一个Repository类与另一个Repository类之间唯一需要改变的是构造函数中的连接字符串。具体来说,这个:@"Provider=Microsoft.ACE.OLEDB.12.0;UserID=qypav1;Password=QqPamPoamMSET;DataSource=C:CatcherNTheRyeDATAOMDDAT03.MDB;JetOLEDB:Systemdatabase=C:Catch22Datatrip.mdw"))……将需要:@"Provider=Microsoft.ACE.OLEDB.12.0;UserID=qypav1;Password=QqPamPoamMSET;DataSource=C:CatcherNTheRyeDATAOMDDAT01.MDB;JetOLEDB:Systemdatabase=C:Catch22Datatrip.mdw"))(OMDDAT03变成OMDDAT01)您可以使用依赖注入将dbContext注入UnitOfWork:publicinterfaceIRepositorywhereT:class{IQueryableGetAll();无效添加(T实体);voidDelete(T实体);voidDeleteAll(IEnumerable实体);无效更新(T实体);布尔任何();}publicclassRepository:IRepositorywhereT:class{privatereadonlyIDbContext_context;私有只读IDbSet_dbset;公共存储库(IDbContext上下文){_context=上下文;_dbset=context.Set();}publicvirtualIQueryableGetAll(){return_dbset;}publicvirtualvoidAdd(Tentity){_dbset.Add(entity);}publicvirtualvoidDelete(Tentity){varentry=_context.Entry(entity);entry.State=EntityState.Deleted;_dbset.Remove(实体);}publicvirtualvoidDeleteAll(IEnumerableentity){foreach(varentinentity){varentry=_context.Entry(ent);entry.State=EntityState.Deleted;_dbset.Remove(ent);}}publicvirtualvoidUpdate(Tentity){varentry=_context.Entry(entity);_dbset.Attach(实体);entry.State=EntityState.Modified;}publicvirtualboolAny(){return_dbset.Any();}}最后:publicinterfaceIUnitOfWork:IDisposable{IRepositoryGetRepository()whereTEntity:class;无效保存();}publicclassUnitOfWork:IUnitOfWorkwhereTContext:IDbContext,new(){privatereadonlyIDbContext_ctx;私有只读字典_repositories;私人布尔_disposed;publicUnitOfWork(){_ctx=newTContext();_repositories=新字典();_disposed=假;}publicIRepositoryGetRepository()whereTEntity:class{if(_repositories.Keys.Contains(typeof(TEntity))){return_repositories[typeof(TEntity)]asIRepository;}varrepository=newRepository(_ctx);_repositories.Add(typeof(TEntity),repository);返回存储库;}publicvoidSave(){_ctx.SaveChanges();}publicvoidDispose(){Dispose(true);GC.SuppressFinalize(这个);}protectedvirtualvoidDispose(booldisposing){if(this._disposed)返回;如果(处置){_ctx.Dispose();}this._disposed=true;我只是复制并浏览了我的一个项目中的代码,就像我在我的应用程序中使用多个dbcontext一样,另请参阅此解决方案:MultipleDbContextsinanN-TierApplication以上是C#学习教程:HowtotelltheWebAPI/CastleWindsor路由引擎在我的存储库中使用不同的数据库实例?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:

最新推荐
猜你喜欢