两个具有相同结构、不同名称的表,如何在运行时切换
我没有计划这样做,因为需求才刚刚出现,但使用实体框架,我们有成对的表(我将它们称为双胞胎,A 和 B),它们具有相同的数据结构但名称不同。当然,这通过 EF 映射为不同类型的对象对。
我想做的是假装我只有一个表/对象,并且在某处(可能在存储库中)有一个开关,我可以抛出该开关以从 B 组表而不是 A 组中获取数据。
我无法弄清楚是否有使用存储库的有用路线,使用结构图和/或多态性来使其发挥作用。
另一种选择可能是将双胞胎“B”表放入第二个数据库中,并与其“A”双胞胎同名,如果这有帮助的话?
(直到今天,我还以为我有两个不同的数据库,没有交叉,只需要实现一个连接字符串切换 - 事实证明情况并非如此,因为 80% 的表在两个状态之间共享,而只有 3 或 4 个状态是共享的。是双胞胎)
I'd not planned for this as the requirement has only just emerged but Using Entity Framework we have pairs of tables (I'll call them Twins, A & B) with identical data structures but different names. This of course maps through via EF as pairs of objects of different types.
What I'd like to do is pretend I only have one table/object and have a switch somewhere (in the repository perhaps) that I can throw to get the data from the B group of tables rather than the A group.
I can't figure out whether there is a useful route using the repo, using structuremap and or polymorphism to enable this to work.
Al alternative might be to put the twin 'B' tables in a second database, and with the same name as their 'A' twin, if that would help at all ?
(Up until today I thought I had two different databases with no crossover and just needed to implement a connection string switch - turns out thats not the case as 80% of the tables are shared between the two states and its just the 3 or 4 that are twinned)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将通过依赖注入和多态性的组合来实现这一点。
我不会直接在实体上操作(让我们称它们为 TwinA 和 TwinB),而是创建以下类型...
(请原谅这个名称...问题中没有太多上下文信息)
然后,您将...
取决于根据需要,将在运行时使用结构图(通过绑定配置)绑定正确的存储库。实现差异在于利用一个实体集而不是另一个实体集(TwinA 或 TwinB)。
从编码的角度来看,您仍然针对 ITwinRepository 进行编码并在 TwinModel 上进行操作,因此如果您决定实现 TwinC 表,消费者将不需要受到未来更改的影响。 :哦
I would implement this through a combination of dependency injection and polymorphism.
Rather than operating directly on the entities (let's call them TwinA and TwinB), I would create the following types...
(pardon the name...not much contextual info in the question)
Then, you would have...
Depending on the need, the correct Repository would be bound at runtime using structure map (through binding configuration). The implementation differences would be to leverage one Entity set over another (TwinA or TwinB).
From a coding standpoint, you're still coding against ITwinRepository and operating on TwinModel, so consumers won't need be affected with future changes, should you decide to implement a TwinC table. :O
我发现创建两个数据库,从第一个数据库生成 EF 对象,然后在第二个数据库中删除公用表并用同名的视图替换它们回到第一个数据库,效果很好。这样我就可以非常简单地在存储库中选取正确的连接字符串(尽管用结构图交换存储库可能会更整洁)。
I've found that creating two databases, generating the EF objects from the 1st, then in the 2nd dropping the common tables and replacing them with views back to the 1st with the same name works just fine. This then allows me to quite simply pick up the right connection string in the repository (although swapping repo's with structuremap would probably be neater).