Skip to content

Instantly share code, notes, and snippets.

@JakeGinnivan
Created July 22, 2011 02:28
Show Gist options
  • Save JakeGinnivan/1098765 to your computer and use it in GitHub Desktop.
Save JakeGinnivan/1098765 to your computer and use it in GitHub Desktop.
//In app.xaml.cs
public void AppStartup()
{
var containerBuilder = new ContainerBuilder();
containerBuilder
.RegisterAssembly(typeof(App).Assembly)
.AssignableTo<IView>()
.AsImplementedInterfaces();
var container = containerBuilder.Build();
container.Resolve<IUIService>().ShowDialog(container.Resolve<IMainView>());
//this should be the only place the container is ever referenced
}
public interface IView {}
public interface IMyView {}
public partial class MyView : IMyView
{
public MyView(MyViewModel viewModel)
{
DataContext = viewModel;
InitaliseComponent();
}
}
public class MyViewModel : ViewModelBase
{
public MyViewModel()
{
//Could throw here if not in design time
}
public MyViewModel(ISomeDependency someDependency)
{
_someDependency = someDependency;
}
}
//The only place we use ServiceLocator.Current is in our EventAggregator to resolve handlers for a particular event. i.e
Type handlerInterface = typeof(IHandleDomainEvent<>).MakeGenericType(domainEvent.GetType());
IEnumerable<object> allhandlers = ServiceLocator.Current.GetAllInstances(handlerInterface);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment