Skip to content

Commit f68fe17

Browse files
authored
docs: explain how to use Primitives alongside System.Reactive (#226)
Add a section under the System.Reactive migration guide for projects where another package still brings in System.Reactive, so Subscribe is ambiguous. It covers three fixes: an alias on the System.Reactive package reference, a using directive inside the namespace, and SubscribePrimitives. The note near the top of the README now links to it.
1 parent 2a8b66d commit f68fe17

1 file changed

Lines changed: 48 additions & 2 deletions

File tree

‎README.md‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ cancel the work. `DisposeWith` adds a disposable to a `MultipleDisposable` so yo
185185

186186
> [!NOTE]
187187
> Another package can pull in System.Reactive, which declares its own `Subscribe` extension methods for
188-
> `IObservable`. Both sets are then in scope and the call is ambiguous. Call `SubscribePrimitives` to pick this
189-
> library's implementation. It has the same callback overloads and the same behaviour as `Subscribe`.
188+
> `IObservable`. Both sets are then in scope and the call is ambiguous. See
189+
> [Using both libraries in one project](#using-both-libraries-in-one-project) for the fixes.
190190
191191
## Install
192192

@@ -2704,6 +2704,52 @@ dotnet add xyz.Reactive/xyz.Reactive.csproj package ReactiveUI.Primitives.Maui.R
27042704
8. Build both packages side by side. `xyz` should carry no System.Reactive runtime dependency. `xyz.Reactive` should
27052705
keep its System.Reactive-facing APIs for existing consumers.
27062706

2707+
### Using both libraries in one project
2708+
2709+
Your project can use ReactiveUI.Primitives while another package still brings in System.Reactive. Both libraries declare
2710+
`Subscribe` extension methods for `IObservable` with the same shapes. When both are in scope, the compiler cannot pick
2711+
one and reports error CS0121. Choose one of these fixes.
2712+
2713+
**Give System.Reactive an alias.** This fixes the whole project at once. Add a direct reference to System.Reactive with
2714+
an alias, in the project file or in `Directory.Build.props`:
2715+
2716+
```xml
2717+
<ItemGroup>
2718+
<PackageReference Include="System.Reactive" Version="6.1.0" Aliases="SystemReactive" />
2719+
ItemGroup>
2720+
```
2721+
2722+
An alias keeps a package's types out of normal scope. Every `Subscribe` call then uses ReactiveUI.Primitives, and other
2723+
packages that depend on System.Reactive keep working. Use the same version as those packages need, or a newer one. When
2724+
a file does need System.Reactive, declare the alias at the top of that file:
2725+
2726+
```csharp
2727+
extern alias SystemReactive;
2728+
2729+
using SystemReactive::System.Reactive.Linq;
2730+
```
2731+
2732+
**Import the namespace inside your namespace.** This fixes one file. A `using` directive placed after the `namespace`
2733+
line takes priority over the ones outside it:
2734+
2735+
```csharp
2736+
namespace MyApp;
2737+
2738+
using ReactiveUI.Primitives;
2739+
2740+
public sealed class Worker
2741+
{
2742+
public IDisposable Watch(IObservable<Exception> errors) => errors.Subscribe(e => Console.WriteLine(e.Message));
2743+
}
2744+
```
2745+
2746+
**Call `SubscribePrimitives`.** This fixes one call. It has the same overloads and behaviour as `Subscribe`, under a
2747+
name no other library uses. `SubscribeSafePrimitives` does the same for `SubscribeSafe`.
2748+
2749+
```csharp
2750+
errors.SubscribePrimitives(e => Console.WriteLine(e.Message));
2751+
```
2752+
27072753
### Factory mapping
27082754

27092755
| System.Reactive | ReactiveUI.Primitives | Notes |

0 commit comments

Comments
 (0)