如何使用接口类在vb.net中具有工厂方法模式 [英] How to have factory method pattern in vb.net with interface class

查看:85
本文介绍了如何使用接口类在vb.net中具有工厂方法模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想创建一个结构,其中有一个具有2个功能的源类:MySourceClass.GetData()作为Datatable和MySourceClass.GetSchema()作为Datatable.

当从我的代码中调用此函数时,它应该相应地进入接口类,并从用户选择的文件中读取数据.例如,如果用户选择一个excel文件,则在调用MySourceClass.GetData()时,它应读取excel文件对象类并获取数据,但是如果选择了CSV文件对象类,则在调用MySourceClass.GetData()时,它应读取CSV文件中的数据.

我想要实现的所有事情是,对SourceClass.GetData的调用将通过使用接口类读取适当的文件而去并获取数据表,而接口类规定了要读取的文件类型,而我不必传递MySourceClass,GetData()函数中的任何值,如FileName,FilePath,FileType,HasHeaderRow,Connection String等...,因为接口类已经具有所有这些信息...

我对此并不陌生,因此不胜感激,因此,我希望通过一些示例来说明如何实现这一目标....

这样的东西

Hello,

I want to create a structure where in I have a source class which has got 2 funtions: MySourceClass.GetData() As Datatable and MySourceClass.GetSchema() As Datatable.

When this functions are called from my code it should accordingly go to the interface class and read data from the user selected file. For example, if the user selects an excel file then when calling MySourceClass.GetData() it should read the excel file object class and get the data however if the CSV file object class is selected then when calling MySourceClass.GetData() it should read data from CSV file.

All in all what I want to achieve is that a call to SourceClass.GetData would go and fetch the datatable by reading the appropriate file using the interface class and the interface class dictates which type of file is to be read and I dont have to pass any values into the MySourceClass,GetData() function like FileName, FilePath, FileType, HasHeaderRow, Connection String etc... as the interface class would already have all that information...

I am quite new and thick to this and would therefore appreciate some kind of example to explain how can I achieve this....

Something like this

Public Interface ReadFile
    Function GetData() As Datatable    
End Interface

Class ReadCsV
    implements ReadFile

End Class

Class ReadExcel
    implements ReadFile

End Class

Class MySource
    Public Function GetData() as Datatable
         'This would read the interface and 
         'decide wether to read from CSV file or 
         'from excel file and accordingly returm the datatable
End Class



进行整个练习的原因是,我最终要实现的是用户可以选择多个源,例如CSV文件,excel文件,定界文件,数据库表的组合,并且我允许他们将这些数据映射到现有数据库表,将文件中的数据保存到该表中.

因此,创建一个对象结构,该对象结构具有最顶层的Import Job,在其中包含一个Import Task Collection对象,该对象具有Import任务对象,其中包含Source类,Field Mapping Collection类,Destination表详细信息类.

因此,当我执行ImportJob.ImportTaskCollection(i).ImportTask.DataSource.GetData()时,它将读取相应的文件并返回数据表.

非常感谢您的所有帮助,并希望有一天我能帮助别人...

谢谢.



The reason for this whole exercise is because all I want to finally achieve is that user can select multiple sources like a combination of CSV file, excel file, delimited file, database tables and I allow them to map this data to the existing database tables and save the data from the file in that table.

Therefore creating a object structure which has Import Job at the topmost level, within it a Import Task Collection object which has import task object containg Source Class, Field Mapping Collection Class, Destination table details class.

Therefore what would happen is when i do ImportJob.ImportTaskCollection(i).ImportTask.DataSource.GetData() it will read the appropriate file and return the datatable

All your help is very much appreciated and hopefully someday i would be in a postition to help others...

Thanks.

推荐答案

ReadCsv应该具有GetData函数,该函数查看CSV文件并将数据加载到DataTable中,然后返回该数据表.它会知道路径,因为您将通过构造函数传递路径.

同样,ReadExcel将具有一个GetData函数,该函数返回一个DataTable,该数据表是通过在传递到构造函数的路径中找到的Excel文件中进行读取而填充的.
MySource类将包含实现ReadFile接口的类(ReadCsv或ReadExcel)的实例.在MySource.GetData中,您可以对要引用的ReadFile变量调用GetData函数.您必须根据自己喜欢的条件在较早的时间将该实例设置为ReadCsv或ReadExcel.
ReadCsv should have a function, GetData, that looks at a CSV file and loads the data into a DataTable, which it returns. It will know the path, because you will pass in the path via the constructor.

Similarly, ReadExcel will have a GetData function that returns a DataTable that was populated by reading in from an Excel file found at the path passed into the constructor.

The MySource class would hold an instance of a class (ReadCsv or ReadExcel) that implements the ReadFile interface. From MySource.GetData, you would call the GetData function on the ReadFile variable you have a reference to. You will have to set that instance to either the ReadCsv or ReadExcel earlier on based on whatever conditions you like.


我对在MySource如何知道哪个实例上感到有些困惑它具有ReadExcel或ReadCSV的ReadFile接口?同时,接口如何知道还可以从ReadCSV或ReadExcel类获取数据表?您能否提供一个传入文件名和文件路径的示例?

谢谢
I am a bit confused on the part where how would the MySource know which instance of the ReadFile Interface it has i.e. ReadExcel or ReadCSV? At the same time how would the interface know wether to get the datatable from the ReadCSV or ReadExcel class? Can you please provide an example with the filename and filepath getting passed in?

Thanks


这篇关于如何使用接口类在vb.net中具有工厂方法模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆