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

feature: splat init checks for pre registrations #360

Merged
merged 16 commits into from
Jul 27, 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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ There are 2 parts to the locator design:
* **Locator.Current** The property to use to **retrieve** services. Locator.Current is a static variable that can be set on startup, to adapt Splat to other DI/IoC frameworks. We're currently working from v7 onward to make it easier to use your DI/IoC framework of choice. (see below)
* **Locator.CurrentMutable** The property to use to **register** services

**Note:** Currently these properties point to the same object and you can use CurrentMutable to also GetServices, but this is not the intention and the interfaces may be adjusted in future to lock this down (and make it more obvious what the use cases are).

To get a service:

```cs
Expand Down Expand Up @@ -132,6 +130,10 @@ Locator.CurrentMutable.RegisterLazySingleton(() => new LazyToaster(), typeof(ITo
### Dependency Resolver Packages
For each of the provided dependency resolver adapters, there is a specific package that allows the service locator to be implemented by another ioc container.

Please note: If you are adjusting behaviours of Splat by working with your custom container directly. Please read the relevant projects documentation on
REPLACING the registration. If the container supports appending\ multiple registrations you may get undesired behaviours, such as the wrong logger factory
being used.

| Container | NuGet | Read Me
|---------|-------|-------|
| [Splat.Autofac][SplatAutofacNuGet] | [![SplatAutofacBadge]][SplatAutofacNuGet] | [Setup Autofac][SplatAutofacReadme]
Expand Down
47 changes: 32 additions & 15 deletions src/Splat.Autofac.Tests/DependencyResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
using Autofac;
using Shouldly;
using Splat.Common.Test;
using Splat.Tests.ServiceLocation;
using Xunit;

namespace Splat.Autofac.Tests
{
/// <summary>
/// Tests to show the <see cref="AutofacDependencyResolver"/> works correctly.
/// </summary>
public class DependencyResolverTests
public class DependencyResolverTests : BaseDependencyResolverTests<AutofacDependencyResolver>
{
/// <summary>
/// Shoulds the resolve views.
Expand Down Expand Up @@ -93,45 +94,61 @@ public void AutofacDependencyResolver_Should_Resolve_Screen()
/// Should throw an exception if service registration call back called.
/// </summary>
[Fact]
public void AutofacDependencyResolver_Should_Throw_If_UnregisterCurrent_Called()
public void AutofacDependencyResolver_Should_Throw_If_ServiceRegistionCallback_Called()
{
var container = new ContainerBuilder();
container.UseAutofacDependencyResolver();

var result = Record.Exception(() =>
Locator.CurrentMutable.UnregisterCurrent(typeof(IScreen)));
Locator.CurrentMutable.ServiceRegistrationCallback(typeof(IScreen), disposable => { }));

result.ShouldBeOfType<NotImplementedException>();
}

/// <summary>
/// Should unregister all.
/// Check to ensure the correct logger is returned.
/// </summary>
/// <remarks>
/// Introduced for Splat #331.
/// </remarks>
[Fact]
public void AutofacDependencyResolver_Should_UnregisterAll_Called()
public void AutofacDependencyResolver_Should_ReturnRegisteredLogger()
{
var container = new ContainerBuilder();
container.UseAutofacDependencyResolver();

var result = Record.Exception(() =>
Locator.CurrentMutable.UnregisterCurrent(typeof(IScreen)));
Locator.CurrentMutable.RegisterConstant(
new FuncLogManager(type => new WrappingFullLogger(new ConsoleLogger())),
typeof(ILogManager));

result.ShouldBeOfType<NotImplementedException>();
var d = Splat.Locator.Current.GetService<ILogManager>();
Assert.IsType<FuncLogManager>(d);
}

/// <summary>
/// Should throw an exception if service registration call back called.
/// Test that a pre-init logger isn't overriden.
/// </summary>
/// <remarks>
/// Introduced for Splat #331.
/// </remarks>
[Fact]
public void AutofacDependencyResolver_Should_Throw_If_ServiceRegistionCallback_Called()
public void AutofacDependencyResolver_PreInit_Should_ReturnRegisteredLogger()
{
var container = new ContainerBuilder();
container.UseAutofacDependencyResolver();
var builder = new ContainerBuilder();
builder.Register(_ => new FuncLogManager(type => new WrappingFullLogger(new ConsoleLogger()))).As(typeof(ILogManager))
.AsImplementedInterfaces();

var result = Record.Exception(() =>
Locator.CurrentMutable.ServiceRegistrationCallback(typeof(IScreen), disposable => { }));
builder.UseAutofacDependencyResolver();

result.ShouldBeOfType<NotImplementedException>();
var d = Splat.Locator.Current.GetService<ILogManager>();
Assert.IsType<FuncLogManager>(d);
}

/// <inheritdoc />
protected override AutofacDependencyResolver GetDependencyResolver()
{
var container = new ContainerBuilder();
return new AutofacDependencyResolver(container.Build());
}
}
}
1 change: 1 addition & 0 deletions src/Splat.Autofac.Tests/Splat.Autofac.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<ItemGroup>
<ProjectReference Include="..\Splat.Autofac\Splat.Autofac.csproj" />
<ProjectReference Include="..\Splat.Common.Test\Splat.Common.Test.csproj" />
<ProjectReference Include="..\Splat.Tests\Splat.Tests.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading