Skip to content

Commit f283e27

Browse files
authored
Add ViewModelRegistrationType to IViewFor attribute (#341)
* Add ViewModelRegistrationType to IViewFor attribute Introduces ViewModelRegistrationType to the IViewFor attribute and updates generator logic to support separate Splat registration for view models. Documentation, tests, and source generator code are updated to reflect the new option and ensure correct registration behavior. * Add ExcludeFromCodeCoverage to test classes Added [ExcludeFromCodeCoverage] attribute to various test and sample classes across the project to exclude them from code coverage analysis. This helps ensure that code coverage metrics focus on production code rather than test scaffolding. * Remove ExcludeFromCodeCoverage attributes from test classes Deleted the [ExcludeFromCodeCoverage] attribute from several test-related classes to allow code coverage tools to include these files. This change may help improve visibility into test code coverage metrics. * Fix grouping key in ViewModel registrations Changed the grouping key from SplatRegistrationType to SplatViewModelRegistrationType in viewModelRegistrations to ensure correct grouping of ViewModel registration types. * Clarify Splat registration type documentation Updated XML comments to specify that the ViewModel (T) is registered in the Splat service locator, instead of IViewFor<T>. This improves clarity for consumers of the attribute. * Update IViewForAttribute XML docs for Splat registration Clarifies XML documentation to specify that ViewModel (T) is registered in the Splat service locator, instead of IViewFor<T>. Adds new received test file for .NET 10.0 verifying the generated attribute code. * Skip registrations with empty ViewModelTypeName Added checks to skip processing registrations where ViewModelTypeName is null, empty, or whitespace. This prevents potential errors from invalid or incomplete registration data. * Remove unnecessary null/empty check for vmType Simplifies vmType handling by removing redundant string.IsNullOrEmpty checks before prefixing with 'global::'. This change assumes vmType is always non-null and non-empty at this point in the code.
1 parent ecb5527 commit f283e27

File tree

23 files changed

+129
-23
lines changed

23 files changed

+129
-23
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ The class must inherit from a UI Control from any of the following platforms and
560560

561561
### IViewFor with Splat Registration Type
562562

563-
Choose from the following Splat Registration Types:
563+
Choose from the following Splat Registration Types, option for IViewFor Registration and / or ViewModel Registration:
564564
- `SplatRegistrationType.PerRequest`
565565
- `SplatRegistrationType.LazySingleton`
566566
- `SplatRegistrationType.Constant`
@@ -569,7 +569,7 @@ Choose from the following Splat Registration Types:
569569
```csharp
570570
using ReactiveUI.SourceGenerators;
571571
using Splat;
572-
[IViewFor<MyReactiveClass>(RegistrationType = SplatRegistrationType.PerRequest)]
572+
[IViewFor<MyReactiveClass>(RegistrationType = SplatRegistrationType.PerRequest, ViewModelRegistrationType = SplatRegistrationType.LazySingleton)]
573573
public partial class MyReactiveControl : UserControl
574574
{
575575
public MyReactiveControl()

src/ReactiveUI.SourceGenerator.Tests/IVIEWFOR/IViewForGeneratorTests.FromIViewFor#ReactiveUI.SourceGenerators.IViewForAttribute.g.verified.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ namespace ReactiveUI.SourceGenerators;
2121
internal sealed class IViewForAttribute<T> : global::System.Attribute
2222
{
2323
/// <summary>
24-
/// Gets the Splat registration type for Splat registration.
24+
/// Gets the Splat registration type for Splat IViewFor registration.
2525
/// Registers IViewFor<T> in the Splat service locator.
2626
/// </summary>
2727
public SplatRegistrationType RegistrationType { get; init; } = SplatRegistrationType.None;
28+
29+
/// <summary>
30+
/// Gets the Splat registration type for Splat View Model registration.
31+
/// Registers the ViewModel (T) in the Splat service locator.
32+
/// </summary>
33+
public SplatRegistrationType ViewModelRegistrationType { get; init; } = SplatRegistrationType.None;
2834
}
2935

3036
/// <summary>
@@ -39,10 +45,16 @@ internal sealed class IViewForAttribute<T> : global::System.Attribute
3945
internal sealed class IViewForAttribute(string? viewModelType) : global::System.Attribute
4046
{
4147
/// <summary>
42-
/// Gets the Splat registration type for Splat registration.
48+
/// Gets the Splat registration type for Splat IViewFor registration.
4349
/// Registers IViewFor<T> in the Splat service locator.
4450
/// </summary>
4551
public SplatRegistrationType RegistrationType { get; init; } = SplatRegistrationType.None;
52+
53+
/// <summary>
54+
/// Gets the Splat registration type for Splat View Model registration.
55+
/// Registers the ViewModel (T) in the Splat service locator.
56+
/// </summary>
57+
public SplatRegistrationType ViewModelRegistrationType { get; init; } = SplatRegistrationType.None;
4658
}
4759
#nullable restore
4860
#pragma warning restore

src/ReactiveUI.SourceGenerator.Tests/TestHelper.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,17 @@ namespace ReactiveUI.SourceGenerator.Tests;
3030
public sealed class TestHelper<T> : IDisposable
3131
where T : IIncrementalGenerator, new()
3232
{
33-
#pragma warning disable CS0618 // Type or member is obsolete
3433
/// <summary>
3534
/// Represents the NuGet library dependency for the Splat library.
3635
/// </summary>
3736
private static readonly LibraryRange SplatLibrary =
38-
new("Splat", VersionRange.AllStableFloating, LibraryDependencyTarget.Package);
37+
new("Splat", VersionRange.AllStable, LibraryDependencyTarget.Package);
3938

4039
/// <summary>
4140
/// Represents the NuGet library dependency for the ReactiveUI library.
4241
/// </summary>
4342
private static readonly LibraryRange ReactiveuiLibrary =
44-
new("ReactiveUI", VersionRange.AllStableFloating, LibraryDependencyTarget.Package);
45-
#pragma warning restore CS0618 // Type or member is obsolete
43+
new("ReactiveUI", VersionRange.AllStable, LibraryDependencyTarget.Package);
4644

4745
private static readonly string mscorlibPath = Path.Combine(
4846
System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(),

src/ReactiveUI.SourceGenerators.Execute.Maui/IViewForTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// The ReactiveUI and contributors licenses this file to you under the MIT license.
44
// See the LICENSE file in the project root for full license information.
55

6+
using System.Diagnostics.CodeAnalysis;
67
using ReactiveUI.SourceGenerators;
78

89
namespace SGReactiveUI.SourceGenerators.Test.Maui

src/ReactiveUI.SourceGenerators.Execute.Maui/TestViewModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// The ReactiveUI and contributors licenses this file to you under the MIT license.
44
// See the LICENSE file in the project root for full license information.
55

6+
using System.Diagnostics.CodeAnalysis;
67
using System.Reactive;
78
using System.Reactive.Linq;
89
using System.Runtime.Serialization;
@@ -15,6 +16,7 @@ namespace SGReactiveUI.SourceGenerators.Test;
1516
/// <summary>
1617
/// TestClass.
1718
/// </summary>
19+
[ExcludeFromCodeCoverage]
1820
[DataContract]
1921
public partial class TestViewModel : ReactiveObject
2022
{

src/ReactiveUI.SourceGenerators.Execute.Nested1/Class1.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// The ReactiveUI and contributors licenses this file to you under the MIT license.
44
// See the LICENSE file in the project root for full license information.
55

6+
using System.Diagnostics.CodeAnalysis;
67
using ReactiveUI;
78
using ReactiveUI.SourceGenerators;
89

@@ -11,6 +12,7 @@ namespace SGReactiveUI.SourceGenerators.Execute.Nested1;
1112
/// <summary>
1213
/// Class1.
1314
/// </summary>
15+
[ExcludeFromCodeCoverage]
1416
public partial class Class1 : ReactiveObject
1517
{
1618
[Reactive]

src/ReactiveUI.SourceGenerators.Execute.Nested2/Class1.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// The ReactiveUI and contributors licenses this file to you under the MIT license.
44
// See the LICENSE file in the project root for full license information.
55

6+
using System.Diagnostics.CodeAnalysis;
67
using ReactiveUI;
78
using ReactiveUI.SourceGenerators;
89

@@ -11,6 +12,7 @@ namespace SGReactiveUI.SourceGenerators.Execute.Nested2;
1112
/// <summary>
1213
/// Class1.
1314
/// </summary>
15+
[ExcludeFromCodeCoverage]
1416
public partial class Class1 : ReactiveObject
1517
{
1618
[Reactive]

src/ReactiveUI.SourceGenerators.Execute.Nested3/Class1.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// The ReactiveUI and contributors licenses this file to you under the MIT license.
44
// See the LICENSE file in the project root for full license information.
55

6+
using System.Diagnostics.CodeAnalysis;
67
using ReactiveUI;
78
using ReactiveUI.SourceGenerators;
89

@@ -11,6 +12,7 @@ namespace SGReactiveUI.SourceGenerators.Execute.Nested3;
1112
/// <summary>
1213
/// Class1.
1314
/// </summary>
15+
[ExcludeFromCodeCoverage]
1416
public partial class Class1 : ReactiveObject
1517
{
1618
[Reactive]

src/ReactiveUI.SourceGenerators.Execute/InternalTestViewModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
// See the LICENSE file in the project root for full license information.
55

66
using System.Collections.ObjectModel;
7+
using System.Diagnostics.CodeAnalysis;
78
using ReactiveUI;
89
using ReactiveUI.SourceGenerators;
910

1011
namespace SGReactiveUI.SourceGenerators.Test;
1112

13+
[ExcludeFromCodeCoverage]
1214
internal partial class InternalTestViewModel : ReactiveObject
1315
{
1416
[ReactiveCollection]

src/ReactiveUI.SourceGenerators.Execute/Person.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// The ReactiveUI and contributors licenses this file to you under the MIT license.
44
// See the LICENSE file in the project root for full license information.
55

6+
using System.Diagnostics.CodeAnalysis;
67
using ReactiveUI;
78
using ReactiveUI.SourceGenerators;
89

@@ -12,6 +13,7 @@ namespace SGReactiveUI.SourceGenerators.Test;
1213
/// Person.
1314
/// </summary>
1415
/// <seealso cref="ReactiveUI.ReactiveObject" />
16+
[ExcludeFromCodeCoverage]
1517
public partial class Person : ReactiveObject
1618
{
1719
/// <summary>

0 commit comments

Comments
 (0)