Another approach which doesn't break the open/closed principle is to create view models and views for each type of StockItem, and then have a type which collates all of the exposed sub types and their corresponding view models, and provides a factory method that takes a StockItem and returns the matching view model.
This would be easy to do with an IoC container or MEF for example.
Update
As a quick example using MEF:
public class StockItemEditViewModelFactory : IPartImportsSatisfiedNotification
{
private Dictionary<Type, IStockItemEditViewModelResolver> resolvers;
[ImportMany(IStockItemEditViewModelResolver)]
private IEnumerable<IStockItemEditViewModelResolver> importedResolvers;
public void OnImportsSatisfied()
{
// Build dictionary of StockItem -> StockItemEditViewModel
// Do error handling if no imported resolvers or duplicate types
resolvers = new Dictionary<Type, IStockItemEditViewModelResolver>
foreach(var importedResolver in importedResolvers)
{
resolvers.Add(importedResolver.StockItemType, importedResolver);
}
}
public IStockItemEditViewModel Create(StockItem stockItem)
{
// Find the appropriate resolver based on stockItem.GetType(), handle errors
var type = stockItem.GetType();
var entry = this.resolvers.FirstOrDefault(kv => kv.Key == type);
var resolver = entry.Value;
return resolver.CreateEditViewModel(stockItem);
}
}
[InheritedExport]
public interface IStockItemEditViewModelResolver
{
Type StockItemType { get; }
IStockItemEditViewModel CreateEditViewModel(StockItem stockItem);
}
public class LotItemEditViewModelResolver : IStockItemEditViewModelResolver
{
Type StockItemType { get { return typeof(LotItem); } }
IStockItemEditViewModel CreateEditViewModel(StockItem stockItem)
{
return new LotItemEditViewModel(stockItem);
}
}
public class MainViewModel
{
public IStockItemEditViewModel ActiveItem { get; private set; }
public MainViewModel(StockItemEditViewModelFactory editViewModelfactory)
{
StockItem stockItem = new LotItem();
this.ActiveItem = editViewModelFactory.Create(myStockItem);
}
}
This is untested, but it shows you the general approach. You could use generics to make this tidier.
If you want to use Unity instead of MEF, then the concept would be the same, but your would need to register each implementation of IStockItemEditViewModelResolver (or use a Unity extension and do it with conventions), and then your factory would need a reference to the container so that it could do a ResolveAll (see Unity Resolve Multiple Classes).