I have question regarding passing by some information to my repositories classes. Currently I have two repositories, CsvRepository
and SqlRepository
. For both of them, I want to pass some information, currently there is only path to be passed for CsvRepository
. As you can see I also have factory pattern class and GetRepository
method to get specific object on demand. I do not think this is right place to pass that information from CsvRepository
method because it's not the duty of factory method. Could you help me out where this should be passed correctly?
public enum DataSource
{
SqlServer,
Csv,
Oracle
}
public interface IRepo
{
}
public interface IRepository<T> : IRepo where T : class
{
IEnumerable<T> GetRecords();
}
public class RepositoryFactory
{
// here I would need to pass all information for all repositories - bad idea perhaps
public static IRepo GetRepository(DataSource repositoryType)
{
IRepo repo;
switch (repositoryType)
{
case DataSource.Csv:
repo = new CsvRepository("path"); //perhaps path shouldn't be right passed right here - so where?
break;
case DataSource.SqlServer:
repo = new SqlRepository();
break;
default:
throw new ArgumentException("Invalid Repository Type");
}
return repo;
}
}
public class CsvRepository : IRepository<InputData>
{
private string _path;
public CsvRepository(string path)
{
_path = path;
}
public IEnumerable<IBasic> GetRecords()
{
return from line in File.ReadLines(_path)
select line.Split(',') into parts
where parts.Length == 3
select new InputData { Name = parts[0], X = Convert.ToInt32(parts[1]), Y = Convert.ToInt32(parts[2]) };
}
}
// other repository
public class OracleRepository : IRepository<InputData>
{
// to be implemented
}
class Form1
{
private void FetchData(DataSource repositoryType)
{
ClearListBox();
var repository = RepositoryFactory.GetRepository(repositoryType);
var people = repository.GetRecords();
}
private void CSVFetchButton_Click(object sender, RoutedEventArgs e)
{
FetchData(DataSource.Csv);
}
private void SQLFetchButton_Click(object sender, RoutedEventArgs e)
{
FetchData(DataSource.SqlServer);
}
public class InputData : IBasic
{
public string Name { get; set; }
public int X { get; set; }
public int Y { get; set; }
}
public interface IBasic
{
}