Last active
November 11, 2016 15:36
-
-
Save asmorger/11f232167d4fb57257a2 to your computer and use it in GitHub Desktop.
Caliburn.Micro Application Bootstrapper to replace the default DI container with Autofac
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<customBootstrapper:CaliburnAutofacApplication | |
x:Class="MyApplication.App" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:local="using:MyApplication" | |
xmlns:customBootstrapper="using:MyApplication.CustomBootstrapper"> | |
</customBootstrapper:CaliburnAutofacApplication> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MyApplication.CustomBootstrapper | |
{ | |
public abstract class CaliburnAutofacApplication : CaliburnApplication | |
{ | |
protected IContainer Container; | |
private readonly ContainerBuilder _builder; | |
private FrameAdapter _rootFrame; | |
protected CaliburnAutofacApplication() | |
{ | |
_builder = new ContainerBuilder(); | |
} | |
protected override void Configure() | |
{ | |
HandleConfigure(_builder); | |
Container = _builder.Build(); | |
} | |
protected override object GetInstance(Type service, string key) | |
{ | |
object instance; | |
if (string.IsNullOrEmpty(key)) | |
{ | |
if (Container.TryResolve(service, out instance)) | |
{ | |
return instance; | |
} | |
} | |
else | |
{ | |
if (Container.TryResolveNamed(key, service, out instance)) | |
{ | |
return instance; | |
} | |
} | |
throw new Exception(string.Format("Could not locate any instances of service {0}.", service.Name)); | |
} | |
protected override IEnumerable<object> GetAllInstances(Type service) | |
{ | |
var result = Container.Resolve(typeof(IEnumerable<>).MakeGenericType(service)) as IEnumerable<object>; | |
return result; | |
} | |
protected override void BuildUp(object instance) | |
{ | |
Container.InjectProperties(instance); | |
} | |
protected override void PrepareViewFirst(Windows.UI.Xaml.Controls.Frame rootFrame) | |
{ | |
_rootFrame = new FrameAdapter(rootFrame); | |
} | |
public virtual void HandleConfigure(ContainerBuilder builder) | |
{ | |
builder.Register(x => _rootFrame).As<INavigationService>().SingleInstance(); | |
/* Register all types by default. This will register the view models. */ | |
builder.RegisterAssemblyTypes(AssemblySource.Instance.ToArray()).AsSelf().InstancePerDependency(); | |
/* Personal classes to separate & manage the Autofac & Automapper configurations*/ | |
// AutofacConfiguration.Configure(builder); | |
// AutomapperConfiguration.Configure(Container); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment