I have an IPartyCountService interface that counts the number of customers and the number of providers.
The implementation class "PartyCountService" uses type checking to check whether a party is a Customer or a Supplier.
Does the PartyCountService implementation class produce type checking, does it smell code?
Any feedback, comments, criticism are welcome.
public interface IPartyCountService
{
int GetTotalNumberOfCustomers();
int GetTotalNumberOfSuppliers();
}
internal class PartyCountService:IPartyCountService
{
IPartyRepository _partyRepository;
public PartyCountService(IPartyRepository partyRepository)
{
_partyRepository = partyRepository;
}
public int GetTotalNumberOfCustomers()
{
var counter = 0;
foreach(var customer in _partyRepository.GetAll())
{
if (customer is Customer) counter++;
}
return counter;
}
public int GetTotalNumberOfSuppliers()
{
var counter = 0;
foreach (var customer in _partyRepository.GetAll())
{
if (customer is Supplier) counter++;
}
return counter;
}
}
public interface IPartyRepository
{
IList<IParty> GetAll();
}
internal class PartyRepository:IPartyRepository
{
public IList<IParty> GetAll()
{
return allParties;
}
}
internal class Customer:IParty{}
internal class Supplier:IParty{}
public interface IParty{}
source
share