使应用程序与多个 ado.net 提供商一起使用的最佳方法是什么?
我们开发了一套使用 SQL Server 的工业应用程序。
随着需求的扩大,客户希望将我们的应用程序与他们自己的 RDBMS(例如 Oracle、MySQL 等)一起使用。
目前,一些应用程序使用 OLEDB 提供程序,而其他应用程序则出于各种原因(包括编程经验和宗教信仰)使用本机 SQLServer。
我们寻求统一的方法。项目经理更喜欢 OLEDB,因为“它适用于一切”。就我个人而言,我讨厌它,因为查询参数的处理方式......
我有两个解决方案:
第一个是使用 SQLOLEDB 保留现有代码并在不兼容的情况下使用分支指令调整每个 SQL 语句。它会进行得非常快,SQL 不会有太大差异,并且会让项目经理满意。然而,它可能会将代码变成意大利面条。
第二种方法是为每个 RDBMS 使用本机 ADO.NET 提供程序,并为每个 RDBMS 开发一个数据访问库。每个库可能包含一个公共部分以避免代码重复。当然这需要一些时间,但它将带来干净的架构和最佳的性能。
它主要是客户端服务器应用程序。我们数据库的某些部分是动态生成的,存在大量的动态查询。这就是为什么使用 ORM 是不可能的。
您将如何尝试实现相同的目标?
We develop a set of industrial applications that use SQL Server.
As demand expands, customers want to use our applications with their own RDBMS such as Oracle, MySQL and others.
Currently, some applications uses OLEDB provider, and other uses native SQLServer for various reasons, including programming experience and religion.
We seek a unified approach. The project manager prefers OLEDB because "it works with everything." Personally, I hate it because how the queries parameters are handled...
I have two solutions in mind :
The first would be to use SQLOLEDB keeping the existing code and adapt each SQL statement in case of incompatibility by using branch instructions. It would go very quickly, the SQL would not differ that much and it would please the project manager. However, it might turn code to spaghetti.
The second would be to use the native ADO.NET provider for each RDBMS, and to develop a library of data access for each. Each library might contain a common part to avoid code duplication. Of course it would take some time, but it will lead to a clean architecture and optimal performances.
It's mainly client server applications. Some parts of our database are dynamically generated, and there is a lot of dynamic queries. That's why using an ORM is not possible.
How would you try to achieve the same objective?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会做以下两件事之一:
如果你能逃脱惩罚,那就走最小公分母路线。但是,听起来您可能需要利用特定于数据库的功能。如果是这样,那么在代码中干净地执行此操作的最佳途径是与一组接口而不是特定的 DAL 进行通信。
只需确保您的接口定义得足够好,以减少以后的更改,并提供足够的开口来支持所有必要的数据库。
那么这只是一个工厂为给定接口提供具体实现的情况。
I would do one of two things:
If you can get away with it, go the lowest-common-denominator route. However, it may sound like you need to take advantage of database-specific features. If so, then your best route to cleanly do this in code is to talk to a set of interfaces instead of a specific DAL.
Just make sure your interfaces are well enough defined to reduce changes later on and also offer enough openings to support all the necessary databases.
It is then just a case of having a factory to provide the concrete implementation for a given interface.
在您的库中,确保仅使用
系统中的基类。 Data.Common
命名空间。因此,不要使用
SqlCommand
,而是使用DbCommand
等。您可以将实际实现注入到 DAL。
或者,编写您自己的抽象(接口/抽象类),为其编写实现类来包装不同的提供程序实现并使用它。
In your library, ensure you are only using the base classes in the
System.Data.Common
namespace.So, instead of
SqlCommand
, useDbCommand
etc.You can inject the actual implementation to the DAL.
Alternatively, write your own abstraction (interface/abstract class), write implementation classes for it that wrap the different provider implementation and use that.