original text

You want to keep your controller Simple enough .

Yours controller More and more overstaffed , You heard that command The pattern is one for controller Slimming solutions .

But you don't know command Whether the pattern is suitable for your application . How much should there be command?

characteristic

command The best thing about the model is that you can ignore the implementation of the business first , Focus on the user interface first .

Suppose you create a website , One function is to register and log in .

Hello MediatR

Realization command The pattern is very simple , Especially when you use Jimmy Bogard’s MediatR To send messages from MVC/API controller Of command.

Use MediatR You can start by creating a simple class to represent your commands .

public class RegisterUser : IRequest<bool>
{
public string EmailAddress { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}

bool Parameter indicates that your command will return bool type .

Then you can go to controller It uses MediatR To send this command .

public class UserController : Controller
{
private readonly IMediator _mediator; public UserController(IMediator mediator)
{
_mediator = mediator;
} [HttpPost]
public ActionResult Register(RegisterUser registerUser)
{
bool registered = _mediator.Send(registerUser);
return View();
}
}

The main points of

Separation of duties . You don't need to store 、 Application services and so on are put into your controller , All you need is Mediator.

Model binding auto fill command object (RegisterUser).

This ensures your controller It's simple. ,controller Focus on processing forwarding requests .

So how to deal with command Well ? It's simple , Create a handler that will do !

public class RegisterUserHandler : IRequestHandler<RegisterUser, bool>
{
public bool Handle(RegisterUser message)
{
// save to database
return true;
}
}

[ translate ] Use Command Patterns and MediatR More articles on simplifying your controller

  1. C++ Basics —— Simulate transaction (1)COMMAND Pattern

    ================================= Copyright notice ================================= Copyright notice : Original article Prohibited reproduced   Please click the “ Contact mail ...

  2. Command Pattern

    Command The pattern just encapsulates a function without any variables . interface Command{ void Excute();} It has a strong decomposition function . The task of function level is promoted to the level of class ( A class is just for ...

  3. Agile software development (3)---COMMAND Pattern &amp; Active Object Pattern

    COMMAND Pattern command The pattern is very simple , It's as simple as you can't imagine . public interface Command { void execute(); } So that's one command The pattern looks like ...

  4. Clause 19 command Patterns and Hollywood rules

    When a function object is used as a callback , It's just one. command Examples of patterns What is callback ? Callback is that the framework knows when to do something , But what exactly to do , Maybe the framework doesn't know ( Because he didn't design the callback function ), And the user knows that a particular ...

  5. Developing design patterns ( One )Command Pattern

    Command Definition Pass the request from the client into an object , You don't need to know what this request activates Action or processing details about accepting the request . It's a model of communication between two machines , Similar to traditional process language Verbal CallBack function . optimal ...

  6. Command Pattern

    Command The pattern encapsulates the request into an object (Command) in , And store the specific ConcreteCommand Class (Receiver) in , So as to realize the object of calling operation and the concrete implementation of operation Between ...

  7. 【 Behavior type 】Command Pattern

    Command mode is to encapsulate a user's request as ( command ) object , Thus, different requests of users can be parameterized . Sort or log these requests . And supports rollback recovery operations . I remember I just started using it before Photoshop when , It has a special operation history panel ...

  8. Java Design patterns (22) Command mode (Command Pattern )

    Command Pattern is the one that puzzles me most , After reading a lot of code , Just feel vaguely grasp its general principle , I think the most important thing to understand design patterns is to grasp the principles and structures , Only in this way can we guide our actual programming .Command The pattern is actually not very specific ...

  9. Xwork survey XWork It's a standard Command Pattern realization , And completely from web The layers come out .Xwork Provides a lot of core functions : Front end interceptors (interceptor), Runtime form property validation , Type conversion , Powerful expression language (OGNL – the Object Graph NavigationLanguage),IoC(Inversion of Control Inversion control ) Container, etc . ----------------

    Xwork survey XWork It's a standard Command Pattern realization , And completely from web The layers come out .Xwork Provides a lot of core functions : Front end interceptors (interceptor), Runtime form property validation , Type conversion , Powerful expressions ...

Random recommendation

  1. HDU5769 Substring( The suffix array )

    link :http://acm.hdu.edu.cn/showproblem.php?pid=5769 #include <iostream> #include <stdio.h> ...

  2. Use time to generate users Id

    Use the time of user registration , As a new user Uid: /** * Generate users id, Generate... In time * * @return */ public static String date2UserId() { SimpleDa ...

  3. c++ And c Something different 2

    1. General global variables are placed in cpp  In the document , instead of .h  Inside . Otherwise, it's easy to mess up , This mess will be fine a few times , First of all, remember such a coding specification . I think it's more troublesome for everyone to introduce it , But it can only be realized cpp It's inside . After all .h Is the file to be included ...

  4. High performance IO The design of the Reactor and Proactor Pattern ( turn )

    In high performance I/O In design , There are two well-known models Reactor and Proactor Pattern , among Reactor Patterns are used to synchronize I/O, and Proactor Applied to asynchrony I/O operation . Before comparing the two patterns , The first thing we need to understand is ...

  5. android Bluetooth( Official translator )

    Bluetooth Using the Bluetooth APIs, an Android application can perform the following: Use Bluetooth APIs, One And ...

  6. jQuery.merge Source code reading

    jQuery.merge(first,second) summary Merge two arrays The returned result modifies the contents of the first array —— Elements of the first array are followed by elements of the second array . Parameters first: The first array to be processed , It's going to change ...

  7. Swift - Add a filter effect to the image ( Tan old photo filter , Black and white filter )

    Core Image It's a powerful filter handling framework . It can directly add all kinds of built-in filters to images , It can also modify the brightness level precisely , Colour and lustre , Exposure, etc , Here are two examples to show how to give UIImage Add filters . 1, Tan filters  -  ...

  8. Nginx Configure the same domain name to support http And https Two ways to access

    Nginx Configure the same domain name http And https Both ways are accessible , The certificate is applied for free on Alibaba cloud server{listen 80;listen 443 ssl;ssl on;server_name domain name ;in ...

  9. C# Dirty word filtering algorithm

    public class DirtyWordOper    {        private static Dictionary<string, object> hash = new Di ...

  10. PHP7 Learning notes ( 3、 ... and ) About PHP7 How to install debugging tools Xdebug Expansion and Zephir The problem of

    Preface : 1. Find your own way to install 2. Fast installation This extension is installed because Zephir Compilation doesn't always pass , I have to , Use Zephir Write extensions , There are always the following errors : www@ubuntu1:~/phalcon-zephi ...