wpf - How to create a generic view model -


i have create generic viewmodel passing entity 1 many relationship. i'll explain: 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 combobox;             var selecteditem = cmb.selectedvalue 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 , 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         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 here. how can fill in detail of 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 depending on choice of combobox have go different entity. can not access respective detaildocument = (

please stop doing... wpf verbose language. means you'll have write loads of code, duplicating large sections. please favour , accept fact. i've seen situation time , time again (and wasted own time on when first learning wpf), ends in tears.

in mvvm, customary provide 1 view model 1 view. view model responsible providing of data , functionality related view. unless of views identical, generic view model won't work. worse still, if did work, complicate hell out of project , make simple tasks more difficult.

if don't want rewrite view models each time, copy , paste view model , change differences. having said that, must code seems @ odd words... want generic view model, problems relates data access.

it far more usual have data access class(es) in separate project view models. in respect, seems though trying implement the repository pattern, in wrong place. making generic data access classes far more common , has far less go wrong generic view model.

therefore, recommend forget generic view models , declare new 1 each view , move data access code separate project (or @ least separate class) , optionally make generic instead (using linked page on msdn help). may first real problem have come across in endeavour, if continue along path, can guarantee won't last.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -