此修复是否针对“PostSharp 抱怨 CA1800:DoNotCastUnnecessarily”?最好的一个?

发布于 2024-08-27 18:13:59 字数 5834 浏览 9 评论 0原文

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

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

PostSharp 抱怨 CA1800:DoNotCastUnnecessarily。原因是我将同一对象两次转换为同一类型。该代码在最好的情况下将投射 2 次(如果它是标准许可证),在最坏的情况下将投射 4 次(如果它是限时许可证)。我知道可以使规则无效(这是我的第一种方法),因为这里对性能没有太大影响,但我正在尝试最好的方法。

 //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”改进1版本。不要抱怨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”方法(在代码中复制到下面)。我认为这个版本最少只会投射一次,最多会投射三次。 您对改进 2 有何看法?有最好的方法吗?

This question is about "is" and "as" in casting and about CA1800 PostSharp rule. I want to know if the solution I thought is the best one possible or if it have any problem that I can't see.

I have this code (named OriginaL Code and reduced to the minimum relevant). The function ValidateSubscriptionLicenceProducts try to validate a SubscriptionLicence (that could be of 3 types: Standard,Credit and TimeLimited ) by casting it and checking later some stuff (in //Do Whatever).

PostSharp complains about CA1800:DoNotCastUnnecessarily. The reason is that I am casting two times the same object to the same type. This code in best case will cast 2 times (if it is a StandardLicence) and in worst case 4 times (If it is a TimeLimited Licence). I know is possible to invalidate rule (it was my first approach), as there is no big impact in performance here, but I am trying a best approach.

 //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...


        }

This is Improved1 version using "as". Do not complain about CA1800 but the problem is that it will cast always 3 times (if in the future we have 30 or 40 types of licences it could perform bad)

    //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...
        }

But later I thought in a best one. This is the final version I am using.

    //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;
        }

It requires a change in the classes StandardSubscriptionLicence, CreditSubscriptionLicence and TimeLimitedSubscriptionLicence to have a "tryparse" method (copied below in the code). This version I think it will cast as minimum only once and as maximum three. What do you think about improve 2? Is there a best way of doing it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞觅青森が 2024-09-03 18:13:59

在您的三个代码片段中,“improve 2”似乎是最好的一个。
不过我认为你可以改进你的设计,从而完全消除铸造的需要。

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

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

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

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

希望这是有道理的。

Of your three code snippets, "improve 2" seems to be the best one.
However I think you could improve your design in a way that would completely remove the need for casting.

Add an abstract method called ValidateProducts to SubscriptionLicence and let each child licence implement the logic that is specific for that particular type of licence. This way, you place the business logic with the data, thus avoiding an anemic domain model.

this way, the implementation of your method would merely be:

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

Furthermore, by making the method abstract on the base class, you enforce each "child licence" to implement the method, so you do not need to check anything. Therefore the ValidateSubscriptionLicenceProducts method will never have to be changed even though new types of licences are added in the future.

Hope it makes sense.

胡大本事 2024-09-03 18:13:59

我认为 PostSharp 不会抱怨。而是FxCop。

I don't think PostSharp complains. Rather FxCop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文