gpt4 book ai didi

c# - 这个对 "PostSharp complains about CA1800:DoNotCastUnnecessarily"的修复是最好的吗?

转载 作者:行者123 更新时间:2023-11-30 21:21:44 26 4
gpt4 key购买 nike

这个问题是关于类型转换中的"is"“作为”以及CA1800 PostSharp规则。我想知道我认为的解决方案是否是最好的解决方案,或者它是否存在我看不到的任何问题。

我有这段代码(名为 OriginaL Code 并减少到最低限度)。 ValidateSubscriptionLicenceProducts 函数尝试通过强制转换并稍后检查一些内容(在//Do Whatever 中)来验证 SubscriptionLicence(可能有 3 种类型:Standard、Credit 和 TimeLimited)。

PostSharp 提示 CA1800:DoNotCastUnnecessarily。原因是我将同一对象两次转换为同一类型。此代码在最好的情况下将转换 2 次(如果它是 StandardLicence),在最坏的情况下将转换 4 次(如果它是 TimeLimited Licence)。我知道有可能使规则无效(这是我的第一种方法),因为这里对性能没有太大影响,但我正在尝试一种最佳方法。

 //Version Original Code
//Min 2 casts, max 4 casts
//PostSharp Complains about CA1800:DoNotCastUnnecessarily
private void ValidateSubscriptionLicenceProducts(SubscriptionLicence licence)
{
if (licence is StandardSubscriptionLicence)
{
// All products must have the same products purchased
List<StandardSubscriptionLicenceProduct> standardProducts = ((StandardSubscriptionLicence)licence).SubscribedProducts;
//Do whatever
}
else if (licence is CreditSubscriptionLicence)
{
// All products must have a valid Credit entitlement & Credit interval
List<CreditSubscriptionLicenceProduct> creditProducts = ((CreditSubscriptionLicence)licence).SubscribedProducts;
//Do whatever
}
else if (licence is TimeLimitedSubscriptionLicence)
{
// All products must have a valid Time entitlement
// All products must have a valid Credit entitlement & Credit interval
List<TimeLimitedSubscriptionLicenceProduct> creditProducts = ((TimeLimitedSubscriptionLicence)licence).SubscribedProducts;
//Do whatever
}
else
throw new InvalidSubscriptionLicenceException("Invalid Licence type");

//More code...


}

这是使用 "as"改进版本。不要提示 CA1800 但问题是它总是会投 3 次(如果将来我们有 30 或 40 种类型的许可证,它可能会表现不好)

    //Version Improve 1
//Minimum 3 casts, maximum 3 casts
private void ValidateSubscriptionLicenceProducts(SubscriptionLicence licence)
{
StandardSubscriptionLicence standardLicence = Slicence as StandardSubscriptionLicence;
CreditSubscriptionLicence creditLicence = Clicence as CreditSubscriptionLicence;
TimeLimitedSubscriptionLicence timeLicence = Tlicence as TimeLimitedSubscriptionLicence;

if (Slicence == null)
{
// All products must have the same products purchased
List<StandardSubscriptionLicenceProduct> standardProducts = Slicence.SubscribedProducts;
//Do whatever
}
else if (Clicence == null)
{
// All products must have a valid Credit entitlement & Credit interval
List<CreditSubscriptionLicenceProduct> creditProducts = Clicence.SubscribedProducts;
//Do whatever
}
else if (Tlicence == null)
{
// All products must have a valid Time entitlement
// All products must have a valid Credit entitlement & Credit interval
List<TimeLimitedSubscriptionLicenceProduct> creditProducts = Tlicence.SubscribedProducts;
//Do whatever
}
else
throw new InvalidSubscriptionLicenceException("Invalid Licence type");

//More code...
}

但后来我想到了一个最好的。这是我使用的最终版本。

    //Version Improve 2
// Min 1 cast, Max 3 Casts
// Do not complain about CA1800:DoNotCastUnnecessarily
private void ValidateSubscriptionLicenceProducts(SubscriptionLicence licence)
{
StandardSubscriptionLicence standardLicence = null;
CreditSubscriptionLicence creditLicence = null;
TimeLimitedSubscriptionLicence timeLicence = null;

if (StandardSubscriptionLicence.TryParse(licence, out standardLicence))
{
// All products must have the same products purchased
List<StandardSubscriptionLicenceProduct> standardProducts = standardLicence.SubscribedProducts;
//Do whatever
}
else if (CreditSubscriptionLicence.TryParse(licence, out creditLicence))
{
// All products must have a valid Credit entitlement & Credit interval
List<CreditSubscriptionLicenceProduct> creditProducts = creditLicence.SubscribedProducts;
//Do whatever
}
else if (TimeLimitedSubscriptionLicence.TryParse(licence, out timeLicence))
{
// All products must have a valid Time entitlement
List<TimeLimitedSubscriptionLicenceProduct> timeProducts = timeLicence.SubscribedProducts;
//Do whatever
}
else
throw new InvalidSubscriptionLicenceException("Invalid Licence type");

//More code...

}


//Example of TryParse in CreditSubscriptionLicence
public static bool TryParse(SubscriptionLicence baseLicence, out CreditSubscriptionLicence creditLicence)
{
creditLicence = baseLicence as CreditSubscriptionLicence;
if (creditLicence != null)
return true;
else
return false;
}

它需要更改 StandardSubscriptionLicence、CreditSubscriptionLicence 和 TimeLimitedSubscriptionLicence 类以具有“tryparse”方法(复制在代码下方)。这个版本我认为它最少只投一次,最多投三个。 你觉得 improve 2 怎么样?有没有最好的方法?

最佳答案

在您的三个代码片段中,“改进 2”似乎是最好的一个。不过,我认为您可以通过一种完全消除类型转换需求的方式来改进您的设计。

将名为 ValidateProducts 的抽象方法添加到 SubscriptionLicence 并让每个子许可证实现特定于该特定类型许可证的逻辑。这样,您将业务逻辑与数据放在一起,从而避免 anemic domain model .

这样,您的方法的实现将只是:

private void ValidateSubscriptionLicenceProducts(SubscriptionLicence licence)
{
if(!licence.ValidateProducts())
throw new Exception("Failed to validate products");
}

此外,通过在基类上抽象方法,您强制执行每个“子许可证”来实现该方法,因此您无需检查任何内容。因此,即使将来添加了新类型的许可证,也永远不必更改 ValidateSubscriptionLicenceProducts 方法。

希望它有意义。

关于c# - 这个对 "PostSharp complains about CA1800:DoNotCastUnnecessarily"的修复是最好的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2598982/

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