MVVM and bundle, service level implementation

I am creating an MVVM application. I am trying to structure my application as follows:

enter image description here

I do not know if this approach is common in MVVM. In either case, the ViewModel uses a service level, for example, to populate the model or ObservableCollection that it wraps. To use its services, ViewModel has a field containing a service abstraction, for example:

IService service; 

Since I use Linq to query the database, I have entities that have the same names as my domain names. To let ViewModel not know about Layer / Database objects, I need a service level to return a domain model instead of the Linq database object created. I do this by doing the following (an example of something I'm working on):

 ObservableCollection<ItemTypeViewModel> GetItemTypes() { DataContextLocalDB dc = new DataContextLocalDB(); ObservableCollection<ItemTypeViewModel> itemTypes = new ObservableCollection<ItemTypeViewModel>(); foreach (ItemType itemType in dc.ItemTypes) { Models.ItemType type = new Models.ItemType(); type.Name = itemType.Name; type.Description = itemType.Description; ItemTypeViewModel itemTypeViewModel = new ItemTypeViewModel(type); itemTypes.Add(itemTypeViewModel); } } 

There are a few things that I'm unhappy / unsure about:

  • Is this a good way to structure in conjunction with MVVM?
  • I am forced to use Model.ItemType so that it is different from ItemType coming from the database. It's unavoidable?
  • I return an ObservableCollection - maybe something is even better to give away, and then somewhere to do what I returned the ObservableCollection?
  • As a rule, what can be improved or what could be the error of the judgment that you see, I made?

Thanks: -)

+6
architecture wpf mvvm
source share
2 answers

There is no reason to recreate Linq data objects for you. Just pass them along with the ViewModel and everything will be fine. It might seem that you should create a decoupling between the Domain and the ViewModel, but since these objects contain only properties, not logic, it is easy for them to pass them on, and it would also be very simple to program.

everything else is very relevant. The only thing I did not use LinqToSql, but EntityFramework instead. It looks about the same, only L2SQL is a rejection of MS.

+2
source share
 public partial class ItemType : EntityObject //this is your Entity Model { public string Name{get;set;} public string Description{get;set;} } 

You cannot edit the section above.

If you want to extend the model using viewModel. Create another class

 public partial class ItemType : EntityObject // this is your ViewModel class,this place on another file {// Important: in same namespace public void SomeMethod(){} public ICommand CustomCommand {get{...}} public string CustomProperty{ get{ return localVar;} set{ localVar=value; onPropertyChanged("CustomProperty"); }} } 

Finally:

 public IQueryable<ItemType> GetItemTypes{ get{ DataContextLocalDB dc = new DataContextLocalDB(); return dc.ItemTypes; } } 
0
source share

All Articles