microservice.toolkit.entitystoremanager 1.1.1

dotnet add package microservice.toolkit.entitystoremanager --version 1.1.1                
NuGet\Install-Package microservice.toolkit.entitystoremanager -Version 1.1.1                
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="microservice.toolkit.entitystoremanager" Version="1.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add microservice.toolkit.entitystoremanager --version 1.1.1                
#r "nuget: microservice.toolkit.entitystoremanager, 1.1.1"                
#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 microservice.toolkit.entitystoremanager as a Cake Addin
#addin nuget:?package=microservice.toolkit.entitystoremanager&version=1.1.1

// Install microservice.toolkit.entitystoremanager as a Cake Tool
#tool nuget:?package=microservice.toolkit.entitystoremanager&version=1.1.1                

Entity Store Manager

Build Release License: MIT Nuget Nuget

The Entity Store Manager allows you to have the flexibility of a no-sql database with the power of a relational database.
You will no longer have to worry about keeping your database updated with complex queries.
Save, delete, and search any entity (data models) in a database without writing a query. Leveraging the flexibility of the microservice pattern with asynchronous and decoupled communication.

How to install

Package Manager

Install-Package microservice.toolkit.entitystoremanager -Version 1.1.1

.NET CLI

dotnet add package microservice.toolkit.entitystoremanager --version 1.1.1

Package Reference

<PackageReference Include="microservice.toolkit.entitystoremanager" Version="1.1.1" />

Supported databases

  • Microsoft SQL Server

Before start

Migrate DB

How to use

Upsert

The SqlServerItemUpsert service provides a way to either insert a new item into the database or update it if it already exists. This document outlines a minimal example of how to use this service.
Ensure your project is set up with a connection to a SQL Server database and that you have the necessary models and services configured. The SqlServerItemUpsert service requires an instance of IDbConnection to interact with the database. \


class MyEntity : IItem
{
    public Guid Id { get; set; }
    public int IntValue { get; set; }
    public string StringValue { get; set; }
    public bool Enabled { get; set; }
    public long Inserted { get; set; }
    public long Updated { get; set; }
    public string Updater { get; set; }
}

var entity = new MyEntity
{
    Id = Guid.NewGuid(),
    IntValue = 1,
    StringValue = "Entity Description",
    Enabled = true,
    Updater = "ExampleUpdater",
    Inserted = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
    Updated = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
};

var myUpsertService = SqlServerItemUpsert<MyEntity>();
var result = await myUpsertService.Run(new ItemUpsertRequest<MyEntity>
{
    Item = entity
});

The SqlServerItemSearch service allows for advanced searches on stored items using various filters and conditions. Below is a minimal example of using this service to search for items based on specific criteria.
Ensure you have a database connection configured and accessible from your service. The SqlServerItemSearch service requires an instance of IDbConnection to access the database.

Code Example

using microservice.toolkit.connectionmanager;
using microservice.toolkit.entitystoremanager.entity;
using microservice.toolkit.entitystoremanager.service.sqlserver;
using System;
using System.Threading.Tasks;

[...]

// Creating an instance of the SqlServerItemSearch service
var itemSearchService = new SqlServerItemSearch<MyItem>(yourDbConnection);

// Defining the search request
var searchRequest = new ItemSearchRequest
{
    Filters = new AndWhere
    {
        Conditions = new List<Where>
        {
            new Where { Key = nameof(MyItem.IntValue), Value = 1 },
            new Where { Key = nameof(MyItem.StringValue), Value = "test" }
        }
    }
};

// Executing the search
var searchResponse = await itemSearchService.Run(searchRequest);

// Handling the response
if (!searchResponse.Error.HasValue)
{
    foreach (var item in searchResponse.Payload.Items)
    {
        Console.WriteLine($"Item ID: {item.Id}");
    }
}
else
{
    Console.WriteLine("Error during the search for items.");
}

Count

The SqlServerItemCount service enables you to count items in the database based on specific criteria, such as filters on item properties. This guide provides a minimal example of how to use this service.
Ensure your project is configured with a connection to a SQL Server database. The SqlServerItemCount service requires an instance of DbConnection to interact with the database.

using System.Data.Common;
using microservice.toolkit.connectionmanager;
using microservice.toolkit.entitystoremanager.entity;
using microservice.toolkit.entitystoremanager.service.sqlserver;
using System.Threading.Tasks;

[...]

var itemCountService = new SqlServerItemCount<MyItem>(dbConnection);

var countRequest = new ItemCountRequest
{
    Filters = new List<Where>
    {
        new Where { Key = nameof(MyItem.Enabled), Value = true }
    }
};

var response = await itemCountService.Run(countRequest);

if (!response.Error.HasValue)
{
    return response.Payload.Counter;
}
else
{
    Console.WriteLine($"Error counting items: {response.Error.Value}");
    return -1;
}

Search by id

The SqlServerItemById service is designed to retrieve a single item from the database by its ID. This section provides a minimal example of how to use this service in your project.
Ensure your project is configured with a connection to a SQL Server database. The SqlServerItemById service requires an instance of DbConnection to interact with the database.

using System.Data.Common;
using microservice.toolkit.connectionmanager;
using microservice.toolkit.entitystoremanager.entity;
using microservice.toolkit.entitystoremanager.service.sqlserver;
using System.Threading.Tasks;

[...]

var itemByIdService = new SqlServerItemById<MyItem>(dbConnection);

var response = await itemByIdService.Run(new ItemByIdRequest
{
    ItemId = itemId
});

if (!response.Error.HasValue)
{
    return response.Payload.Item;
}
else
{
    Console.WriteLine($"Error retrieving item: {response.Error.Value}");
    return null;
}

Bulk Upsert

The SqlServerItemBulkUpsert service allows for the bulk insertion or update of items in a SQL Server database. This can significantly improve performance when dealing with large datasets. Below is a minimal example of how to use this service.
Ensure your project is configured with a connection to a SQL Server database. The SqlServerItemBulkUpsert service requires an instance of DbConnection to interact with the database.

using System.Data.Common;
using microservice.toolkit.connectionmanager;
using microservice.toolkit.entitystoremanager.entity;
using microservice.toolkit.entitystoremanager.service.sqlserver;
using System.Collections.Generic;
using System.Threading.Tasks;

[...]

var customService = new SqlServerItemBulkUpsert<MyCustomItem>(dbConnection);

var itemsToUpsert = new List<MyCustomItem>
{
    new MyCustomItem { Id = "1", Enabled = true },
    new MyCustomItem { Id = "2", Enabled = false }
    // Add more items as needed
};

await customService.Run(new ItemBulkUpsertRequest<MyCustomItem> { Items = itemsToUpsert });

Console.WriteLine("Bulk upsert completed.");

Delete

Attributes

ItemAttribute

The ItemAttribute is a custom attribute in C# that can be used to annotate classes representing items in a database. It allows you to specify a name for the item that can be different from the class name. This is particularly useful in scenarios where your entity names do not directly match your C# class names.

[Item("Product")]
public class MyItem : IItem
{
    [...]
}

The entity will be saved into database with type column as Product.

ItemPropertyAttribute

The ItemPropertyAttribute is a custom attribute that can be used to map class properties using a custom name.

using microservice.toolkit.entitystoremanager.attribute;

public class User : IItem
{
    public string Id { get; set; }
    
    [ItemProperty("user_name")]
    public string Name { get; set; }
    
    [ItemProperty("user_email")]
    public string Email { get; set; }
    
    [...]
}

The ItemProperty attribute will be ignored for properties of the IItem interface (Id, Enabled, Inserted, Updated, Updater).

ItemPropertyIgnoreAttribute

ItemTableAttribute

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  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 is compatible.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.1.1 128 7/19/2024 1.1.1 is deprecated because it is no longer maintained and has critical bugs.
1.1.0 95 7/19/2024
1.0.0 110 6/10/2024
0.11.2 138 4/6/2024
0.11.1 118 3/30/2024