Evospike.MicroservicesCommon 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Evospike.MicroservicesCommon --version 1.0.0                
NuGet\Install-Package Evospike.MicroservicesCommon -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Evospike.MicroservicesCommon" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Evospike.MicroservicesCommon --version 1.0.0                
#r "nuget: Evospike.MicroservicesCommon, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Evospike.MicroservicesCommon as a Cake Addin
#addin nuget:?package=Evospike.MicroservicesCommon&version=1.0.0

// Install Evospike.MicroservicesCommon as a Cake Tool
#tool nuget:?package=Evospike.MicroservicesCommon&version=1.0.0                

MicroservicesCommon

This repository contains the code to build microservices with common codes in the shortest possible time.

Allows you to configure MongoDB and a generic repository to manage the database in MontoDB

Allows you to configure MassTransit to manage RabbitMQ, which is the event mailbox to communicate microservices asynchronously

Allows you to add an authentication layer using JwtBearer to manage access and identification tokens

Build Status

appsettings.json configuration

The file path and other settings can be read from JSON configuration if desired.

In appsettings.json add a "MongoDbSettings", "ServiceSettings", "RabbitMQSettings" properties:

{
   "MongoDbSettings": {
    "Host": "localhost",
    "Port": "27017"
  },
  "ServiceSettings": {
    "ServiceName": "Catalog",
    "Authority": "https://localhost:5003"
  },
  "RabbitMQSettings": {
    "Host": "localhost"
  }
}

And then pass the configuration section to the next methods:

services.AddMongo()
        .AddMongoRepository<Item>("YourTableName")
        .AddMassTransitWithRabbitMQ()
        .AddJwtBearerAuthetication();

Your model must inherit from IEntity class

public class Item : IEntity
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public DateTimeOffset CreatedDate { get; set; }
}

Example of a controller using dependency injection services

[Route("[controller]")]
[ApiController]
public class ItemsController : ControllerBase
{
    private const string AdminRole = "Admin";
    private readonly IRepository<Item> _itemsRepository;
    private readonly IPublishEndpoint _publishEndpoint;

    public ItemsController(IRepository<Item> itemsRepository, IPublishEndpoint publishEndpoint)
    {
        _itemsRepository = itemsRepository;
        _publishEndpoint = publishEndpoint;
    }

    [HttpGet]
    [Authorize(Policies.Read)]
    public async Task<IEnumerable<ItemDto>> GetAsync()
    {
        var items = (await _itemsRepository.GetAllAsync()).Select(item => item.AsDto());
        return items;
    }

    [HttpGet("{id}")]
    [Authorize(Policies.Read)]
    public async Task<ActionResult<ItemDto>> GetByIdAsync(Guid id)
    {
        var item = await _itemsRepository.GetAsync(id);

        if(item == null)
        {
            return NotFound();
        }

        return item.AsDto();
    }

    [HttpPost]
    [Authorize(Policies.Write)]
    public async Task<ActionResult<ItemDto>> PostAsync(CreateItemDto createItemDto)
    {
        var item =  new Item
        {
            Name = createItemDto.Name,
            Description = createItemDto.Description,
            Price = createItemDto.Price,
            CreatedDate = DateTimeOffset.UtcNow
        };

        await _itemsRepository.CreateAsync(item);
        await _publishEndpoint.Publish(new CatalogItemCreated(item.Id, item.Name, item.Description));

        return CreatedAtAction(nameof(GetByIdAsync), new { id = item.Id}, item);
    }

    [HttpPut("{id}")]
    [Authorize(Policies.Write)]
    public async Task<IActionResult> PutAsync(Guid id, UpdateItemDto updateItemDto)
    {
        var existingItem = await _itemsRepository.GetAsync(id);

        if(existingItem == null)
        {
            NotFound();
        }

        existingItem.Name = updateItemDto.Name;
        existingItem.Description = updateItemDto.Description;
        existingItem.Price = updateItemDto.Price;

        await _itemsRepository.UpdateAsync(existingItem);
        await _publishEndpoint.Publish(new CatalogItemUpdated(existingItem.Id, existingItem.Name, existingItem.Description));

        return NoContent();
    }

    [HttpDelete("{id}")]
    [Authorize(Policies.Write)]
    public async Task<IActionResult> DeleteAsync(Guid id)
    {
        var existingItem = await _itemsRepository.GetAsync(id);

        if (existingItem == null)
        {
            NotFound();
        }

        await _itemsRepository.RemoveAsync(existingItem.Id);
        await _publishEndpoint.Publish(new CatalogItemDeleted(existingItem.Id));

        return NoContent();
    }
}

Example of a consumer used by RabbitMQ to consume posts from other microservices

public class CatalogItemCreatedConsumers : IConsumer<CatalogItemCreated>
{
    private readonly IRepository<CatalogItem> _catalogItemsRepository;

    public CatalogItemCreatedConsumers(IRepository<CatalogItem> catalogItemsRepository)
    {
        _catalogItemsRepository = catalogItemsRepository;
    }

    public async Task Consume(ConsumeContext<CatalogItemCreated> context)
    {
        var message = context.Message;
        var item = await _catalogItemsRepository.GetAsync(message.ItemId);

        if(item != null)
        {
            return;
        }

        item = new CatalogItem
        {
            Id = message.ItemId,
            Name = message.Name,
            Description = message.Description
        };

        await _catalogItemsRepository.CreateAsync(item);
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 1,022 7/21/2021
1.0.0 948 5/14/2021