First Repo
IpersonRepository repository = new ServiceRepository();
var people =repository.GetPeople();
foreach(var person in people)
{
personListBox.Items.Add(person);
}
------------------------------------------
Second Repo
IpersonRepository repository = new CSVRepository();
var people =repository.GetPeople();
foreach(var person in people)
{
personListBox.Items.Add(person);
}
-------------------------------------
ThirdRepo
IpersonRepository repository = new SQLRepositroy();
var people =repository.GetPeople();
foreach(var person in people)
{
personListBox.Items.Add(person);
}
-------------------------------------------------
Add new class Repository Factory class
----------------------------------------------
public static class RepositoryFactory
{
public static IPersonRepository GetRepository(string repositoryType)
{
IPersonRepository repo =null;
switch(repositoryType)
{
case RepoType.Service:
return new ServiceRepository();
case RepoType.CSV:
return new CSVRepository();
case RepoType.SQL:
return new SQLRepository();
default:
throw new ArgumentException("Invalid Repository Type");
}
return repo;
}
}
---------------------
public enum RepoType
{
Service,
CSV,
SQL
}
Take Method and Extract to method FetchData Method that takes string argument
private void FetchData(string repositoyType)
{
IpersonRepositroy repositroy = RepositoryFactory.GetRepository(repositroyTpye);
var people =repository.GetPeople();
foreach(var person in people)
{
personListBox.Items.Add(person);
}
}
----------------------------------
Now going back to the each method and replace the code
FetchData( RepoType.Service);
FetchData( RepoType.CSV);
FetchData( RepoType.SQL);