vitorm04 / VMNotification

Library to improve the use of notifications.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

VMNotification

VMNotification is a solution to improve the notifications handling. We have two kinds of messages:

  • Errors
  • Notifications

Add in Startup.cs file, the follow dependency injection:

 services.AddScoped<Notificator>();

Examples

[Route("[controller]")]
public class TestController : Controller
    
    private readonly Notificator _notificator;
    private readonly TestService _service;

    public TestController(Notificator notificator, TestService service) {
        _notificator = notificator;
        _service = service;
    }
    
    [HttpPost]
    public void Post(Customer customer){
    
       _service.AddCustomer(customer);
       
       if(_notificator.IsValid){
           return Ok(new 
           {
               notifications: _notificator.GetNotifications();
           });
       }
       
       return BadRequest(new 
       {
          errors: _notificator.GetErrors();
       });
    }
}


public TestService {
    
    private readonly Notificator _notificator;

    public TestService(Notificator notificator) {
        _notificator = notificator;
    }
    
    public void AddCustomer(Customer customer){
    
        //this action add a new error in the list
        if(string.IsNullOrEmpty(customer.Name)
            _notificator.AddError("Name is invalid");
        
        //this action add a new notification in the list
        _notificator.AddNotification("The confirmation link was sent to the email.!");
    }
}

Add Errors from FluentValidation:

 Customer customer = new Customer();
 CustomerValidator validator = new CustomerValidator();

 _notificator.AddError(validator.Validate(customer));

To get all errors:

 _notificator.GetErrors();

To get all notifications:

 _notificator.GetNotifications();

To remove all errors:

 _notificator.ClearErrors();

To remove all notifications:

 _notificator.ClearNotifications();

About

Library to improve the use of notifications.


Languages

Language:C# 100.0%