I have a dataset that can be created by a few ways, one of them is by parsing some text, the other way is from a CSV format, the third way is by importing it from a database.
Currently, this is the interface:
public interface IMyDataMaker
{
public MyDataSet MakeData();
}
and this is the text parser class:
public class MyDataSetTextParser : IMyDataMaker
{
private readonly string textToParse;
public MyDataSetTextParser(string text)
{
textToParse = text;
}
public MyDataSet MakeDate()
{
// parse the text and return the dataset
}
}
the csv parser is closer to the text parser, this is the database class:
public class DbMyDataSetMaker : IMyDataMaker
{
private readonly SqlConnection connection;
public DbMyDataSetMaker(SqlConnection sqlConn)
{
connection = sqlConn;
}
public MyDataSet MakeDate()
{
// connect to the db and make the dataset
}
}
Is this the correct pattern to use in this case?
I think this is really good. It's the classic Strategy implementation.
You can now chose the real implementation by dependency injection or creation an method to receive one instance of the interface that you need.
Yep, that's absolutely the right way to do it. Use constructors to separate out the connection info for each subtype, and end up with a common 'read' method, just as you've done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With