Reputation: 1388
I have a WPF application using Caliburn.Micro and AutoFac
In the Bootstrapper.Configure() I register my Views & ViewModels as :
protected override void Configure()
{
var builder = new ContainerBuilder();
// register view models
builder.RegisterAssemblyTypes(AssemblySource.Instance.ToArray())
.Where(type => type.Name.EndsWith("ViewModel"))
.AsSelf()
.InstancePerDependency();
// register views
builder.RegisterAssemblyTypes(AssemblySource.Instance.ToArray())
.Where(type => type.Name.EndsWith("View"))
.AsSelf()
.InstancePerDependency();
builder.Register<IWindowManager>(c => new WindowManager()).InstancePerLifetimeScope();
_container = builder.Build();
}
protected override IEnumerable<Assembly> SelectAssemblies()
{
return new[]
{
typeof (MainViewModel).Assembly, // assembly that holds all the ViewModels
typeof (MainView).Assembly // assembly that holds all the Views
};
}
This works nice, the views and viewmodels are nicely mapped by this convention, eg. when instantiating a RandomViewModel, the RandomView is shown.
Furthermore, I have a lot of edit/list viewmodels that derive from generic base classes, where the generic types are the entities in my solution.
For instance, when I have entities UserEntity, OrderEntity, AccountEntity, etc... Then I would create classes:
//base classes
abstract class EditViewModelBase<T> : IEditViewModelBase where T: IEntity
abstract class ListViewModelBase<T> : IListViewModelBase where T: IEntity
//implementations
class UserEditViewModel : EditViewModelBase<UserEntity> {...}
class OrderEditViewModel : EditViewModelBase<OrderEntity> {...}
class AccountEditViewModel : EditViewModelBase<AccountEntity> {...}
...
class UserListViewModel : ListViewModelBase<UserEntity> {...}
class OrderListViewModel : ListViewModelBase<OrderEntity> {...}
class UserListViewModel : ListViewModelBase<UserEntity> {...}
...
Now I want to create a ViewModelFactory to locate viewmodels in a generic way:
class ViewModelFactory
{
public IEditViewModelBase CreateEditViewModel<T>()
{
//this method should do :
// if typeof(T) == typeof(UserEntity) return new UserEditViewModel();
// if typeof(T) == typeof(OrderEntity) return new OrderEditViewModel();
// if typeof(T) == typeof(UserEntity) return new UserEditViewModel();
...
}
public IListViewModelBase CreateListViewModel<T>()
{
//this method should do :
// if typeof(T) == typeof(OrderEntity) return new OrderListViewModel();
...
}
}
How would I solve this so I don't have to change the ViewModelFactory everytime I add a new Entity & ViewModels to my solution ? I was thinking about looping over the container, using reflection tricks like IsInstanceOf and Activator.CreateInstance, but I can't get it to work ...
Upvotes: 1
Views: 1324
Reputation: 63
I tink
public class Bootstrapper
{
public IContainer Bootstrap()
{
var builder = new ContainerBuilder();
builder.RegisterType<EventAggregator>().As<IEventAggregator>().SingleInstance();
builder.RegisterType<MessageDialogService>().As<IMessageDialogService>();
builder.RegisterType<FileDataService>().As<IDataService>();
builder.RegisterType<fLookupProvider>().As<ILookupProvider<f>>();
builder.RegisterType<fGroupLookupProvider>().As<ILookupProvider<fGroup>>();
builder.RegisterType<fDataProvider>().As<IfDataProvider>();
builder.RegisterType<fEditViewModel>().As<IfEditViewModel>();
builder.RegisterType<NavigationViewModel>().As<INavigationViewModel>();
builder.RegisterType<MainViewModel>().AsSelf();
return builder.Build();
}
}
Upvotes: 0
Reputation: 13124
I think the easiest way to do it is by using the Auto Factory library that internally uses AutoFac. You can resolve it like this:
using AutoFactory;
class ViewModelFactory
{
private IAutoFactory<IEditViewModelBase> _editFactory = Factory.Create<IEditViewModelBase>();
private IAutoFactory<IListViewModelBase> _listFactory = Factory.Create<IListViewModelBase>();
public IEditViewModelBase CreateEditViewModel<T>()
{
return _editFactory.SeekPart(t => t.BaseType.GetGenericArguments()[0].Name == typeof(T).Name);
}
public IListViewModelBase CreateListViewModel<T>()
{
return _listFactory.SeekPart(t => t.BaseType.GetGenericArguments()[0].Name == typeof(T).Name);
}
}
Or if you want to do it directly with AutoFac, you can do for example:
using Autofac.Builder;
using Autofac.Features.Metadata
class ViewModelFactory
{
private IContainer _container;
private IEnumerable<Meta<Lazy<IEditViewModelBase>>> _editParts;
public ViewModelFactory()
{
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(this.GetType().Assembly)
.Where(t => typeof(IEditViewModelBase).IsAssignableFrom(t))
.As<IEditViewModelBase>()
.WithMetadata("type", t => t.BaseType.GetGenericArguments()[0]);
_container = builder.Build();
_editParts = _container.Resolve<IEnumerable<Meta<Lazy<IEditViewModelBase>>>>();
}
public IEditViewModelBase CreateEditViewModel<T>()
{
return _editParts.FirstOrDefault(p => p.Metadata["type"] as Type == typeof(T)).Value.Value;
}
...
}
Upvotes: 1