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

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: -)
architecture wpf mvvm
Thedude
source share