JHRS WPF framework reference library introduction

JHRS WPF framework reference library introduction, This series of articles aims to record the use of WPF in the development of new medical projects at work. I feel that some of the norms must be unified and improve the efficiency of team development.

So I tossed such a half-hearted framework, the title WPF Enterprise Class Development framework building guide, 2020 may frighten some people from getting started to giving up, but friends who see these bits and pieces of text will come together and watch it. If it can help you, it would be an honor.

JHRS WPF framework reference library introduction

When I first started to build this framework, I didn’t have any WPF development experience before, and I didn’t know whether the third-party components were reasonable, but after a few days of trial and actual combat, I felt that it was quite convenient to do some business functions. Save a lot of code; in this framework, the three main frameworks are used, namely Prism, AspectInjector, and Refit.

JHRS WPF framework reference library introduction
JHRS WPF framework reference library introduction

Application of Prism Class Library

Prism can be used to build loosely coupled, maintainable and testable XAML application frameworks in WPF and Xamarin.Forms . Each platform has a separate version. Prism provides the implementation of a set of design patterns that help write well-structured and maintainable XAML applications, including MVVM, dependency injection, commands, EventAggregator, etc.

Therefore, you can use this Prism framework when developing WPF programs and developing App programs based on Xamarin.Forms . This framework was originally from Microsoft and has now been handed over to the open source community for maintenance. It was used by many companies before, and now you can feel more assured Use it to build MVVM programs.

Before you start using Prism, it’s best to spend two or three hours reading the Prism series of related articles. If you are familiar with the development model of MVVM before, learning and application are very fast.

Prism open source address: https://github.com/PrismLibrary/Prism

Prism series related articles

  1. Prism data binding of .NET 5 WPF MVVM framework
  2. .NET 5 WPF MVVM framework Prism command
  3. .NET 5 WPF MVVM framework Prism modularization
  4. .NET 5 WPF MVVM framework Prism event aggregator
  5. .NET 5 WPF MVVM framework Prism area manager
  6. .NET 5 WPF MVVM framework Prism navigation system
  7. .NET 5 WPF MVVM framework Prism dialog service

Application of AspectInjector framework

AspectInjector is an aspect-oriented (AOP) attribute-based framework for creating and injecting aspects (injecting aspects) into .net assemblies. We know that using AOP can refactor the original multi-line and repetitive process-oriented code, so that the core code only focuses on its own function or business , which improves the [readability] and [maintainability] of the code. Thereby reducing repetitive code.

AspectInjector open source address: https://github.com/pamidur/aspect-injector

When developing a WPF client, there are many buttons that need to call web api to get data, or when doing a save operation, you need to call the web api interface to submit the data to the remote server, and the event code that executes this button may contain some other For example, after clicking a button, a circle effect (or Loading effect) needs to be displayed on the interface, as shown in the figure below.

JHRS WPF framework reference library introduction
JHRS WPF framework reference library introduction

When the operation is completed (data loading is completed), the effect of this circle disappears and the result is presented to the user; it seems a simple function, if it is not encapsulated, if the entire system has 2000 places where this effect needs to be applied , The amount of code needs to be copied 2000 times during development. After encapsulation, you can make the relevant processing logic only focus on what you should do, as shown in the following sample code:

         /// <summary>
         /// General changing state method
         /// </summary>
         /// <typeparam name="TEntity">Pending change status entity</typeparam>
         /// <param name="entity">current object</param>
         protected override async Task<object> UpdateDataStatus<TEntity>(TEntity entity)
             if (!IsDevelopment)
                 var model = entity as ReservationOutputDto;
                 var response = await RestService.For<IReservationApi>(AuthClient).ChangeStatus(model.Id, model.Status);
                 AlertPopup(response.Message, response.Succeeded? MessageType.Success: MessageType.Error, async (d) =>
                     if (response.Succeeded) await BindPagingData();
                 return response.Succeeded;
             return null;

JHRS WPF framework reference library introduction

The code is used in the same way as above, just mark a [WaitComplete] feature on the corresponding method. In actual projects, you can also write more AOP classes as needed to reduce duplication of code.

How to use it in the framework can be found here.

What does the Refit library do

Refit is a type-safe REST open source library. It is a set of .NET client implementations based on RESTful architecture. It is internally encapsulated by the HttpClient class. It is easier and more secure to access the Web API interface through Refit. To use the Refit framework, you only need to Just install it through the NuGet package installer.

Simply put, Refit is an open source library used to request web APIs more elegantly.

Refit Github address: https://github.com/reactiveui/refit

Refit depends on the interface

Refit ultimately calls the related interface through the RestService class, and this interface corresponds to the web api. The sample interface from github is as follows:

public interface IGitHubApi
        Task<User> GetUser(string user);

JHRS WPF framework reference library introduction

In the framework, JHRS WPF framework reference library introduction. a tool for generating the interface based on swagger analysis is provided. The generated interface is as follows:

   /// <summary>
   /// Login interface
   /// </summary>
   [Headers("User-Agent: JHRS-WPF-Client")]
   public interface ILoginApi
     /// <summary>
     /// User login
     /// </summary>
     /// <param name="dto"></param>
     /// <returns></returns>
     Task<OperationResult<UserContext>> Login(LoginDto dto);

     /// <summary>
     /// drop out
     /// </summary>
     /// <returns></returns>
     Task<OperationResult> Logout();

JHRS WPF framework reference library introduction

How is it used in the framework

When you refer to Refit in the project to request the web api interface, you need to define the interface corresponding to the web api in the local project, and the interface can be generated using the tools provided by the framework. JHRS WPF framework reference library introduction.

Game Programming for Beginners

Each method in the interface provided above is marked with a [Post] feature. If you look at the source code, you can see that the marked features are consistent with REST, [Post], [Get], [Put], [Delete ], [Patch], etc.

JHRS WPF framework reference library introduction, The calling method is as follows:

var response = await RestService.For<IReservationApi>(AuthClient).GetPageingData(request) ;

JHRS WPF framework reference library introduction

In the framework, use the static generic method For of class RestService to call; in addition, AuthClient is an HttpClient object after the token is obtained after logging in, because in the subsequent interface, access to the web api requires identity authentication . The AuthClient object is the HttpClient object that sets the AuthenticationHeaderValue object after the login is successful. For details, please refer to here.

Write at the end

After introducing the three major frameworks or libraries used in building the WPF development framework, the next article will introduce how WPF calls the Web API encapsulation step by step to reconstruct the current state.

JHRS WPF framework reference library introduction

Related reading in this series

  1. WPF enterprise development framework building guide, 2020 from entry to give up
  2. Introduction JHRS WPF development framework basic library
  3. JHRS WPF framework reference library introduction
  4. WPF call Web API package of JHRS development framework
  5. Client portal project of JHRS development framework
  6. How to integrate the various subsystems of the JHRS development framework
  7. How to design a reasonable ViewModel base class in JHRS development framework
  8. Encapsulation of public component user control of JHRS development framework
  9. Some principles of cataloging documents followed by the recommendations of the JHRS development framework
  10. WPF data verification of JHRS development framework
  11. The solution of JHRS development framework’s ViewModel mutual parameter transfer and pop-up frame return parameter
  12. JHRS development framework of stepping on the pit (final chapter)

Leave a Comment