Click here to Skip to main content
15,879,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ReadFileEnd Class

Class ReadExcel    
      implements ReadFileEnd 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


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

From reading a bit I understand that the 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 I 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 I have a reference to. I will have to set that instance to either the ReadCsv or ReadExcel earlier on based on whatever conditions I like.

Knowing all this is fine theoretically but how do i put it in code is what I am failing to understand...

Can anybody please post me a code example for me to understand this properly???

Thanks.
Posted

Visit DoFactory.com or see this[^] example. It must help.
 
Share this answer
 
Here is one. There are plenty of articles (related to the Factory pattern) on CP here - do a search and you should find some good results.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900