I'm investigating a change in our application to use Prism. At the moment I'm struggling with the initialization of hierarchical structures.
Basically I have a base class of which other classes inherit, simplified like this:
public class NodeViewModel : INodeViewModel
{
Node Node { get; private set; }
public ObservableCollection<INodeViewModel> ChildNodeViewModels { get; private set; }
public NodeViewModel(IUnityContainer container, Node node)
{
Node = node;
ChildNodeViewModels = new ObservableCollection<INodeViewModel>();
foreach (Node childNode in Node.ChildNodes)
{
// Some initialization code
}
}
}
Up to now I didn't use the container so we only had the Node argument. Each inherited class had an attribute which we used for model to viewmodel mapping.
[ModelToViewModelMapping(typeof(InheritedNode ))]
public class InheritedViewModel: NodeViewModel
{
public InheritedViewModel(InheritedNode inheritedNode)
: base(inheritedNode)
{
}
}
I then had an initialization line like this:
ChildNodeViewModels.Add((INodeViewModel)System.Activator.CreateInstance(ModelToViewModelMappingDictionary.GetMappedViewModelType(childNode.GetType()), System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null, new object[] { childNode }, null));
My question is twofold.
- I don't know if my old trick with the attribute is a common solution for the mapping. I suppose not but it keeps things together and manageable in an easy way. I'm not sure the viewmodellocator is a good solution and I'm completely new with it, I haven't tried.
- How can the initialization be done. The Resolve method uses a type that I can resolve in one way or another (my attribute solution, viewmodellocator, ...) but Unity's
ParameterOverrideneeds astring parameterName. As shown in the code above that fails because the parameterName is not constant, every inheritted class has its own parameterName.
Thanks in advance!