Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lawrence_shaun_introducing_net_maui_build_and_deploy_crosspl.pdf
Скачиваний:
46
Добавлен:
26.06.2023
Размер:
5.15 Mб
Скачать

Chapter 4 An Architecture to Suit You

You want to modify the contents of the file to look as follows:

using System.Threading.Tasks;

public class Scheduler

{

public void ScheduleAction(TimeSpan timeSpan, Action action)

{

Task.Run(async () =>

{

await Task.Delay(timeSpan); action.Invoke();

});

}

}

In the following sections you will be looking at code examples rather than implementing them directly. This is aimed at providing some comparisons to allow you to find out what will be a good fit for you as you build your applications and grow as a cross-platform developer. At the end of the chapter, you will take your chosen approach and add it into your application so you can see the final result of your ClockWidget.

Model View ViewModel (MVVM)

Model View ViewModel is a software design pattern that focuses on separating the user interface (View) from the business logic (Model). It achieves this with the use of a layer in between (ViewModel). MVVM allows a clean separation of presentation and business logic. Figure 4-2 shows the clean separation between the components of the MVVM architecture.

77

Chapter 4 An Architecture to Suit You

Figure 4-2.  An overview of the MVVM pattern

The result of creating this separation between UI and business logic brings several benefits:

•\

Makes unit testing easier

•\

Allows for Views to be swapped out or even rewritten

 

without impacting the other parts

•\

Encourages code reuse

•\

Provides the ability to separate UI development from

 

the business logic development

A key part to any design pattern is knowing where to locate parts of your code to make it fit and abide by the rules. Let’s take a deeper look at each of the three key parts of this pattern.

Model

The Model is where you keep your business logic. It is typically loaded from a database/webservice among many other things.

For your business logic, you are going to rely on the Scheduler class that you created earlier in the “Prerequisites” section of this chapter.

78