I have to create a generic viewmodel passing an entity with a one to many relationship. I'll explain: My WIndows:
<Window x:Class="Invoice_Example_Brux.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:invoiceExampleBrux="clr-namespace:Invoice_Example_Brux"
Title="MainWindow" Height="350" Width="525"><Window.DataContext><invoiceExampleBrux:MainWindowViewModel/></Window.DataContext><Grid><TextBox HorizontalAlignment="Left" Height="23" Margin="174,78,0,0" TextWrapping="Wrap" Text="{Binding MyModel.Name}" VerticalAlignment="Top" Width="120"/><Label Content="Id" HorizontalAlignment="Left" Margin="10,53,0,0" VerticalAlignment="Top"/><TextBox HorizontalAlignment="Left" Height="23" Margin="10,78,0,0" TextWrapping="Wrap" Text="{Binding MyModel.Id}" VerticalAlignment="Top" Width="120" IsReadOnly="True"/><Label Content="Number" HorizontalAlignment="Left" Margin="322,52,0,0" VerticalAlignment="Top"/><TextBox HorizontalAlignment="Left" Height="23" Margin="322,78,0,0" TextWrapping="Wrap" Text="{Binding MyModel.Number}" VerticalAlignment="Top" Width="120"/><Label Content="Name" HorizontalAlignment="Left" Margin="174,53,0,0" VerticalAlignment="Top"/><Button Content="Save" HorizontalAlignment="Left" Margin="211,288,0,0" VerticalAlignment="Top" Width="75" Command="{Binding SaveCommand}"/><ComboBox
SelectionChanged="Selector_OnSelectionChanged"
HorizontalAlignment="Left" Margin="180,38,0,0" VerticalAlignment="Top" Width="120"
DisplayMemberPath="Name"
ItemsSource="{Binding DocumentType,UpdateSourceTrigger=PropertyChanged}"/><Label Content="Type Document" HorizontalAlignment="Left" Margin="192,12,0,0" VerticalAlignment="Top"/></Grid>myWindows codebheind:
namespace Invoice_Example_Brux
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var cmb = sender as ComboBox;
var selectedItem = cmb.SelectedValue as DocumentType;
if (selectedItem == null) return;
var code = selectedItem.Code;
switch (code)
{
case "A":
DataContext = new ViewModelGeneric<DocumentA>();
break;
case "B":
DataContext = new ViewModelGeneric<DocumentB>();
break;
case "C":
break;
}
}
}
}My Entity DocumentA and DocumentB:
public class DocumentA : DocumentGeneral
{
public ObservableCollection<DetailDocumentA> DetailDocumentA { get; set; }
}
public class DetailDocumentA : DetailDocumentGeneral
{
}
public class DocumentB : DocumentGeneral
{
public ObservableCollection<DetailDocumentB> DetailDocumentB { get; set; }
}
public class DetailDocumentB : DetailDocumentGeneral
{
}
public class DocumentGeneral
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Number { get; set; }
public string TypeDocument { get; set; }
}
public class DetailDocumentGeneral
{
public Guid Id { get; set; }
public string Quantity { get; set; }
public string Price { get; set; }
public string Total { get; set; }
}My ViewModelGeneric:
public class ViewModelGeneric<T> : ViewModelBase
where T : DocumentGeneral, new()
{
public T MyModel { get; set; }
public RelayCommand SaveCommand { get; set; }
public ViewModelGeneric()
{
MyModel = new T();
SaveCommand = new RelayCommand(Save);
}
private void Save(object obj)
{
if (MyModel.Id == Guid.Empty)
{
MyModel.Id = Guid.NewGuid();
}
//the problme is here. how can I do to fill in the detail of my entity
/* MyModel.Detail.Add(new DetailDocumentGeneral
{
Price = "50",Quantity = "100",Total = "5000"
});*/
using (var ctx = new DocumentContext())
{
var document = ctx.Set<T>();
document.Add(MyModel);
ctx.SaveChanges();
}
}
}the problem is that depending on the choice of my combobox I have to go a different entity. But I can not access the respective detailDocument = (
the different documents have the same properties.
I depending on the choice of the combobox I can save different tables in my db.
I had solved using automapper.
I did the mapping table (with relevant details table) attached to the window with the one that I chose from combobx. But if I have 10 documents means doing the mapping of all ten (performance decreases).
I do not know if this is the right way. and for months I'm looking for a more suitable solution but still nothing to do.