Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM and layering, implementing the Service Layer

I am building an MVVM application. I'm trying to structure my application like this:

enter image description here

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:

  • Is this a good way of structuring in combination with MVVM?
  • I am forced to use Models.ItemType to make it different from the ItemType coming from the database. Is this unavoidable?
  • I'm giving back a ObservableCollection - maybe something else would be better to give back and then somewhere make what I returned an ObservableCollection?
  • Just in general, what could be improved or what could be a mistake of judgement you see I made?

Thanks :-)

like image 932
TheDude Avatar asked Apr 03 '11 08:04

TheDude


People also ask

Is MVVM a layered architecture?

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.

What is a service in MVVM?

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.

What is the MVVM design pattern?

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.

What is domain layer in MVVM?

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.


1 Answers

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.

like image 130
Elad Katz Avatar answered Nov 15 '22 08:11

Elad Katz