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

(wip) Update custom-dependency-inversion.md #255

Closed
wants to merge 5 commits into from
Closed
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public class AutofacDependencyResolver : IMutableDependencyResolver

builder.Update(_container);
}

public void UnregisterCurrent(Type serviceType, string contract = null)
{
throw new NotImplementedException();
}

public void UnregisterAll(Type serviceType, string contract = null)
{
throw new NotImplementedException();
}

public IDisposable ServiceRegistrationCallback(Type serviceType, string contract, Action<IDisposable> callback)
{
Expand All @@ -74,6 +84,8 @@ public class AutofacDependencyResolver : IMutableDependencyResolver
}
```

*Note*: Technically, Autofac doesn't provide unregister functionality, but there are ways of getting around it. If interested, refer to [the following SO thread](https://stackoverflow.com/questions/5091101/is-it-possible-to-remove-an-existing-registration-from-autofac-container-builder) for one possibility.

## Register services

```csharp
Expand All @@ -100,16 +112,42 @@ public class AutofacDependencyRegistrar
}
```


## Set the Locator.Current to your implementation

```csharp
var resolver = new AutofacDependencyResolver(container);
// These Initialize methods will add ReactiveUI platform registrations to your container
// They MUST be present if you override the default Locator
resolver.InitializeSplat();
resolver.InitializeReactiveUI();
// Assigning your custom resolver to Locator.Current will automatically take care of
// adding all default Splat and ReactiveUI registrations to your container.
Locator.Current = resolver;
```

From this point on calls `Locator.Current` will go against your custom implementation!

## Bonus
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of calling this a bonus, which implies that it is an extra added bit to the auto fac code above. We should either move this to another page, or explicitly call out using a header tag that this is a different container.


The following is a simple example of using a FuncDependencyResolver with NInject:

```csharp
var kernel = new StandardKernel();
var customResolver = new FuncDependencyResolver(
getAllServices: (service, contract) =>
{
if (contract != null)
cabauman marked this conversation as resolved.
Show resolved Hide resolved
{
return kernel.GetAll(service, contract);
}
var items = kernel.GetAll(service);
var list = items.ToList();
return list;
},
register: (factory, service, contract) =>
{
var binding = kernel.Bind(service).ToMethod(_ => factory());
if (contract != null)
cabauman marked this conversation as resolved.
Show resolved Hide resolved
{
binding.Named(contract);
}
},
unregisterCurrent: null,
unregisterAll: null);
```