.NET Core API endpoint model validation

Example of simple method in an API controller for showing invalid posted model state.

[HttpPost("create")]
public ActionResult<MyModel> Create([FromBody] MyModel model)
{
    ValidateIncomingModel();
    return myBusiness.Create(model);
}

private void ValidateIncomingModel()
{
    if (!ControllerContext.ModelState.IsValid)
    {
        var errors = ControllerContext.ModelState.Keys
              .SelectMany(key => ControllerContext.ModelState[key].Errors
              .Select(x => key + ": " + x.ErrorMessage));
        var message = string.Join("\n", errors);
        throw new Exception($"Not valid incoming model\n {message}");
    }
}

 

What is NgRx and why is it used in Angular apps?

Good intro to NgRx:

NgRx implements the Flux-Pattern. At a high level, it helps you unify all events and derive a common state in your Angular app. With NgRx, you store a single state and use actions to express state changes. It is ideal for apps with many user interactions and multiple data sources.

What problems does NgRx solve?

At a high-level, it mainly solves mainly the two main scenarios:

  • Sharing data between different components
  • A global state for the reuse of data

Sharing Data between different components

In a complex web application, you have different sections. Imagine the following scenario: In a web shop, you have an item list and your shopping cart. These two sections of the web shop are different component trees, probably in different Angular modules. In the item list, the user clicks on a particular item “Add to my cart”. After the click, the item appears in the shopping cart.

Read more:
https://www.workingsoftware.dev/what-is-ngrx-and-why-is-it-used-in-angular/

How to set memory limit for ElasticSearch in Windows

Running elasticsearch on my local development machine takes up half of system memory by default. Here is instructions on how to change max memory size.

Prerequisites:
* Windows 10
* This instruction is for Elasticsearch version 7.16.2 but probably works on other versions as well
* Running elasticsearch.bat (in development mode not as a service)

Goto folder:
“C:\elasticsearch\elasticsearch-7.16.2\config\jvm.options.d\”
Create file:
“jvm.options” (normal text file, utf-8)
Setting for maximum of 4GB memory allocation:

-Xms4g
-Xmx4g

Change both “4” values into other value if desired.
Restart elasticservice.bat

Instructions if running elasticsearch as a service on Windows:

You can set the memory limit for Elastic Search on Windows Server by following command:

[Elasticsearch Path]\bin>elasticsearch-service.bat manager

Note: Run command prompt as administrator

It will open manager as shown here: Image: ElasticSearch service properties

Now go to the ‘Java’ tab and change settings based on your requirement.

Note: Make sure, you changed it under ‘JavaOptions’ textbox and also for separate parameters. For example, to set 1GB initial memory pool and maximum memory pool, you can set ‘1024’ MB for both.

From: https://stackoverflow.com/questions/28798845/how-to-set-memory-limit-to-elasticsearch-in-windows

ClosedXML – .NET library for Excel files

ClosedXML is a .NET library for reading, manipulating and writing Excel 2007+ (.xlsx, .xlsm) files. It aims to provide an intuitive and user-friendly interface to dealing with the underlying OpenXML API.

Source: ClosedXML
License: MIT / Open source project
Doc: https://closedxml.readthedocs.io/en/latest/index.html
Wiki: https://github.com/closedxml/closedxml/wiki

ClosedXML is a wrapper of the offical .NET Open XML SDK:
https://github.com/dotnet/Open-XML-SDK