gpt4 book ai didi

c# - 这段代码中的类型检查是否有代码异味?

转载 作者:行者123 更新时间:2023-11-30 14:41:40 25 4
gpt4 key购买 nike

<分区>

我有一个接口(interface)“IPartyCountService”,用于计算客户数量和供应商数量。

实现类“PartyCountService”使用类型检查来检查参与方是客户还是供应商。

实现类PartyCountService使用类型检查会不会有代码味?

欢迎任何反馈、评论和批评。

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()
{
// put together all parties, including customers and suppliers
return allParties;
}
}
internal class Customer:IParty{}
internal class Supplier:IParty{}
public interface IParty{}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com