Modify rule S4428: LaYC format (#2346)

This commit is contained in:
Mary Georgiou 2023-07-04 13:39:40 +02:00 committed by GitHub
parent 0444d4df30
commit be9c0bc4e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 21 deletions

View File

@ -1,29 +1,32 @@
== Why is this an issue?
include::../description.adoc[]
=== Noncompliant code example
== How to fix it
[source,csharp]
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
using System.ComponentModel.Composition;
[PartCreationPolicy(CreationPolicy.Any)] // Noncompliant
public class FooBar : IFooBar
{
}
public class FooBar : IFooBar { }
----
=== Compliant solution
==== Compliant solution
[source,csharp]
[source,csharp,diff-id=1,diff-type=compliant]
----
using System.ComponentModel.Composition;
[Export(typeof(IFooBar))]
[PartCreationPolicy(CreationPolicy.Any)]
public class FooBar : IFooBar
{
}
public class FooBar : IFooBar { }
----
include::../resources.adoc[]
ifdef::env-github,rspecator-view[]

View File

@ -1,4 +1,6 @@
The ``++PartCreationPolicyAttribute++`` attribute, which is part of the Managed Extensibility Framework (MEF), is used to specify how the exported object will be created. Therefore it doesn't make sense not to export this a class with this attribute using the ``++ExportAttribute++`` attribute.
== Why is this an issue?
To customize the default behavior for an export in the https://learn.microsoft.com/en-us/dotnet/framework/mef/[Managed Extensibility Framework] (MEF), applying the https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.composition.partcreationpolicyattribute[`PartCreationPolicyAttribute`] is necessary.
For the https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.composition.partcreationpolicyattribute[`PartCreationPolicyAttribute`] to be meaningful in the context of an export, it is required to also annotate the class with the https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.composition.exportattribute[`ExportAttribute`].
This rule raises an issue when a class is marked as shared with a ``++PartCreationPolicyAttribute++`` but lacks a ``++ExportAttribute++``.
This rule raises an issue when a class is annotated with the `PartCreationPolicyAttribute` but not with the `ExportAttribute`.

View File

@ -25,5 +25,5 @@
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "unknown"
"quickfix": "targeted"
}

View File

@ -0,0 +1,14 @@
== Resources
=== Documentation
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/framework/mef/[Managed Extensibility Framework (MEF)]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/framework/mef/attributed-programming-model-overview-mef[Attributed programming model overview (MEF)]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.composition.partcreationpolicyattribute[PartCreationPolicyAttribute Class]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.composition.exportattribute[ExportAttribute Class]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.composition.creationpolicy[CreationPolicy Enum]
=== Articles & blog posts
* Stefan Henneken - https://stefanhenneken.net/2015/11/08/mef-part-1-fundamentals-imports-and-exports/[MEF Part 1 Fundamentals, Imports and Exports]
* Stefan Henneken - https://stefanhenneken.net/2019/01/26/mef-part-2-metadata-and-creation-policies/[MEF Part 2 Metadata and creation policies]
* Stefan Henneken - https://stefanhenneken.net/2019/03/06/mef-part-3-life-cycle-management-and-monitoring/[MEF Part 3 Life cycle management and monitoring]

View File

@ -1,21 +1,27 @@
== Why is this an issue?
include::../description.adoc[]
=== Noncompliant code example
== How to fix it
[source,vbnet]
=== Code examples
==== Noncompliant code example
[source,vbnet,diff-id=1,diff-type=noncompliant]
----
Imports System.ComponentModel.Composition
<PartCreationPolicy(CreationPolicy.Any)> ' Noncompliant
Public Class FooBar
Inherits IFooBar
End Class
----
=== Compliant solution
==== Compliant solution
[source,vbnet]
[source,vbnet,diff-id=1,diff-type=compliant]
----
Imports System.ComponentModel.Composition
<Export(GetType(IFooBar))>
<PartCreationPolicy(CreationPolicy.Any)>
Public Class FooBar
@ -23,6 +29,8 @@ Public Class FooBar
End Class
----
include::../resources.adoc[]
ifdef::env-github,rspecator-view[]
'''