为多种源类型创建数据访问类
我需要创建一个类,该类将接受数据源类型(MSSQL、Oracle、Access、Excel、ActiveDirectory、CSV 文件、Sharepoint 等)、该类型的连接详细信息,并返回到该数据源的连接。 之后,我需要能够查询数据源并返回可用字段,最后能够从任何字段中提取数据。
我认为这听起来像是接口的工作,因为数据源类型列表会随着时间的推移而增长,但我从未使用过接口。
这可以吗?:
interface IConnectable
{
void Connect();
}
class SourceMSSQL : RDB, IConnectable{}
class SourceOracle : RDB, IConnectable{}
class SourceAccess : RDB, IConnectable{}
class SourceExcel : RDB, IConnectable{}
class SourceCSVFile: FlatFile, IConnectable{}
class SourceSharePoint: FlatFile, IConnectable{}
class SourceActiveDirectory: FlatFile, IConnectable{}
另外,如何将 Read() 添加到该列表中?
PS:我只需要阅读,不需要写作。
I need to create a class that will accept a datasource type (MSSQL, Oracle, Access, Excel, ActiveDirectory, CSV file, Sharepoint etc.), the connection details for that type, and return a connection to that data source.
After that I need to be able to interrogate the data source and return the available fields, and finally be able to extract data from any fields.
I was thinking that this sounds like a job for a interface, as the list of datasource types will grow over time, but I have never used interfaces.
Will this do?:
interface IConnectable
{
void Connect();
}
class SourceMSSQL : RDB, IConnectable{}
class SourceOracle : RDB, IConnectable{}
class SourceAccess : RDB, IConnectable{}
class SourceExcel : RDB, IConnectable{}
class SourceCSVFile: FlatFile, IConnectable{}
class SourceSharePoint: FlatFile, IConnectable{}
class SourceActiveDirectory: FlatFile, IConnectable{}
also, how do I add Read() to that list?
PS: I only need to read, not write.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您走在正确的道路上。接口是一种规范,允许您使用“未知”类,该类为您提供所需的服务,而无需与您正在使用的服务(在本例中为哪种数据库类型)绑定。
在您拥有的代码中,您将继续使用类似以下内容的接口进行构建:
然后使用具体的类来实现它们。此外,您需要一个工厂来创建您需要的任何类:
这样您就不必知道要创建哪个连接。
.NET 框架中已经提供了一些帮助。有一个类 DbConnection 及其子类(如 SqlConnection)可能会有所帮助。还有一些框架可以帮助您连接(创建类并将它们关联在一起),但这是一个更高级的主题,可能需要稍后尝试 - 从创建您自己的接口和实现开始 - 这将更容易理解。
好想法!祝你好运!
PS 为了能够轻松地重新配置您的软件以创建实现相同接口的不同对象(这通常是您想要的灵活性),您不想直接使用构造函数创建对象,而是有一个创建对象的中心位置。那个地方通常被称为工厂(它创建对象......)。工厂可以非常简单(也许只查看配置字符串或命令行参数或任何控制您行为的内容),也可以更高级,其中复杂的规则规定在不同情况下应使用哪些类。
有一些框架为不同的情况提供了不同类型的工厂,structuralmap link 就是其中之一,MEF(包含在 . NET 4 并可通过 链接 获得)是另一个。但不要从这个开始,它会让你立即考虑太多 - 你可以稍后添加,因为你一开始就使用接口!
Yes, you are on the right track. An interface is a specification that allows you to work with an "unknown" class that gives you the service you want without you being tied to which service (in this case which database type) you are using.
In the code you have you would continue to build on you interface with something like:
and then implement these with concrete classes. In addition you need a factory to create whatever class you need:
this so that you do not have to know which connection to create.
There are some help already present in the .NET framework. There is a class DbConnection with subclasses like SqlConnection that might help out. Also there are frameworks that help you wire up (create classes and associate them together) but that is a more advanced topic which may be something to try out later - begin with creating your own interfaces and implementation for those - that will be easier to understand.
Good thinking! Good luck!
P.S. In order to be able to easily reconfigure your software to create different objects implementing the same interfaces (which usually is a flexibility you want) you do not want to create objects using the constructors directly, instead have one central place where the objects are created. That place is usually referred to as a factory (it creates objects...). The factory can be very simple (maybe just look at a configuration string or a command line argument or whatever controls your behaviour), or more advanced where complex rules dictate which classes should be used in different situations.
There are frameworks that provides factories of different sorts for different situations, structuremap link is one and MEF (included in .NET 4 and is available at link) is another. But do not start with that, it will give you too much to think about at once - you can later on add that given that you are using interfaces to begin with!