I am building an MVVM application. I'm trying to structure my application like this:
I don't know if this approach is common in MVVM. Anyways, the ViewModel uses the Service Layer to e.g populate the Model or ObservableCollection it is wrapping. To make use of its services, the ViewModel has a field that holds an abstraction of the service, like so:
IService service;
Because I use Linq to query the database, I have entities that have the same names as my domain names. To let the ViewModel be unaware of the Service Layer/Database entities, I need the Service Layer to give back a Domain Model instead of a Linq generated database entity. I do that by doing the following (an example of something I am working on at work):
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 couple of things I am unhappy/unsure about:
Thanks :-)
MVVM is the same as the MVC framework. It is a 3-tier architecture plus one more layer. We can do loose coupling using MVVM. View: Write the UI in XAML.
Services provide a specific UI-aware functionality for Views in MVVM applications. Although services are defined within Views, their functionality can still be invoked from View Models that may not even include information about Views.
Model-View-ViewModel (MVVM) is a software design pattern that is structured to separate program logic and user interface controls. MVVM is also known as model-view-binder and was created by Microsoft architects Ken Cooper and John Gossman.
The domain layer is an optional layer that sits between the UI layer and the data layer. Figure 1. The domain layer's role in app architecture. The domain layer is responsible for encapsulating complex business logic, or simple business logic that is reused by multiple ViewModels.
There's just no reason to recreate the Data Objects that Linq creates for you. Just pass them along to the ViewModel an you'll be fine. It might seem like you HAVE to create a decoupling between the Domain and the ViewModel, but since these Entities only contain properties and not logic, it's ok to pass them along, and it would also be oh-so-much-easier to program.
everything else is very much current. one only thing is I wouldn't use LinqToSql, but instead EntityFramework. looks rather the same only L2SQL is an abandon thing by MS.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With