Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add ContainerBuilder update to AutofacDependencyResolver #242

Merged
merged 3 commits into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/Splat.Autofac.Tests/DependencyResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.Linq;
using Autofac;
using ReactiveUI;
using Shouldly;
Expand Down Expand Up @@ -84,5 +86,43 @@ public void AutofacDependencyResolver_Should_Resolve_Screen()
screen.ShouldNotBeNull();
screen.ShouldBeOfType<MockScreen>();
}

/// <summary>
/// Shoulds register ReactiveUI binding type converters.
/// </summary>
[Fact]
public void AutofacDependencyResolver_Should_Register_ReactiveUI_BindingTypeConverters()
{
// Invoke RxApp which initializes the ReactiveUI platform.
var scheduler = RxApp.MainThreadScheduler;
var builder = new ContainerBuilder();
var container = builder.Build();
Locator.Current = new AutofacDependencyResolver(container);

var converters = container.Resolve<IEnumerable<IBindingTypeConverter>>().ToList();

converters.ShouldNotBeNull();
converters.ShouldContain(x => x.GetType() == typeof(StringConverter));
converters.ShouldContain(x => x.GetType() == typeof(EqualityTypeConverter));
}

/// <summary>
/// Shoulds register ReactiveUI creates command bindings.
/// </summary>
[Fact]
public void AutofacDependencyResolver_Should_Register_ReactiveUI_CreatesCommandBinding()
{
// Invoke RxApp which initializes the ReactiveUI platform.
var scheduler = RxApp.MainThreadScheduler;
var builder = new ContainerBuilder();
var container = builder.Build();
Locator.Current = new AutofacDependencyResolver(container);

var converters = container.Resolve<IEnumerable<ICreatesCommandBinding>>().ToList();

converters.ShouldNotBeNull();
converters.ShouldContain(x => x.GetType() == typeof(CreatesCommandBindingViaEvent));
converters.ShouldContain(x => x.GetType() == typeof(CreatesCommandBindingViaCommandParameter));
}
}
}
4 changes: 4 additions & 0 deletions src/Splat.Autofac/AutofacDependencyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Autofac;
using Autofac.Core;
Expand Down Expand Up @@ -70,6 +71,7 @@ public virtual IEnumerable<object> GetServices(Type serviceType, string contract
/// <param name="factory">The factory function which generates our object.</param>
/// <param name="serviceType">The type which is used for the registration.</param>
/// <param name="contract">A optional contract value which will indicates to only generate the value if this contract is specified.</param>
[SuppressMessage("Design", "CS0168: Obsolete method call", Justification = "Needed for container generation")]
public virtual void Register(Func<object> factory, Type serviceType, string contract = null)
{
var builder = new ContainerBuilder();
Expand All @@ -81,6 +83,8 @@ public virtual void Register(Func<object> factory, Type serviceType, string cont
{
builder.Register(x => factory()).Named(contract, serviceType).AsImplementedInterfaces();
}

builder.Update(_container);
}

/// <summary>
Expand Down