Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Andrés Díaz
Andrés Díaz

Posted on

     

How to migrate your WebAPI to MediatRV12.

In a previous post, we explain how to build a web API with CQRS and MediatR.How to Build a Clean WebAPI Net 6.

Why migrate to MediatR V12

MediatR v12 is the latest version of MediatR, and it comes with several new features and improvements. If you're currently using an older version of MediatR and want to upgrade to v12, here's how to do it:

Step 1: Update the Nuget packages.

We first need to remove the current MediatR Nugets and add the latest version since the newest version is all-in, assuming we are using MediatR with dependency injection.

*MediatR
*MediatR.Extensions.Microsoft.DependencyInjection

we can use this command from PowerShell or terminal using dotnet cli to remove the current nugets:

dotnetremovepackageMediatRdotnetremovepackageMediatR.Extensions.Microsoft.DependencyInjection
Enter fullscreen modeExit fullscreen mode

Then the current nugets are removed, and the next step is to add the latest version.

dotnetaddpackageMediatR--version12.0.0
Enter fullscreen modeExit fullscreen mode

Step 2 : Update the mediator registration

The way of registration has changed; we have to call thebuilder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly())); in theProgram.cs file instead ofbuilder.Services.AddMediatR(Assembly.GetExecutingAssembly());.

Step 3 : Update your handlers

The last step is to change the void requests returning Unit to only a return.In this case, the class handler is updated like this:

Previous code

publicsealedclassEditExistingCustomerHandler:IRequestHandler<EditExistingCustomerCommand,Unit>{...publicasyncTask<Unit>Handle(EditExistingCustomerCommandrequest,CancellationTokencancellationToken){varcustomerToUpdate=_mapper.Map<Customer>(request.customerToUpdate);await_customerRepository.UpdateAsync(customerToUpdate);returnUnit.Value;;}}
Enter fullscreen modeExit fullscreen mode

MediatR version V12 code

publicsealedclassEditExistingCustomerHandler:IRequestHandler<EditExistingCustomerCommand>{...publicasyncTaskHandle(EditExistingCustomerCommandrequest,CancellationTokencancellationToken){varcustomerToUpdate=_mapper.Map<Customer>(request.customerToUpdate);await_customerRepository.UpdateAsync(customerToUpdate);return;}}
Enter fullscreen modeExit fullscreen mode

Important Note : if you method is not async, the return is
return Task.CompletedTask.

We can get more details about How to migrate to this versionhere

Recaps

On this topic, we learned that Upgrading to MediatR v12 is a straightforward process that requires updating your NuGet packages, mediator registration, handlers, requests, and responses. By following these steps, you can take advantage of the new features and improvements in v12 and keep your application up-to-date with the latest technologies.
If you enjoyed this article, please subscribe and follow me.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
abflett profile image
Adam Flett
  • Joined

Thank you!

"we have to call the builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly())); in the Program.cs file instead of builder.Services.AddMediatR(Assembly.GetExecutingAssembly());"

almost gave up ;)

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I am a .NET Backend Developer for more than 10 years, mainly focusing on the application architecture, design patterns, and Azure.
  • Location
    Santo Domingo
  • Work
    Software Developer
  • Joined

More fromAndrés Díaz

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp