Evospike.Repository
1.0.2
dotnet add package Evospike.Repository --version 1.0.2
NuGet\Install-Package Evospike.Repository -Version 1.0.2
<PackageReference Include="Evospike.Repository" Version="1.0.2" />
paket add Evospike.Repository --version 1.0.2
#r "nuget: Evospike.Repository, 1.0.2"
// Install Evospike.Repository as a Cake Addin #addin nuget:?package=Evospike.Repository&version=1.0.2 // Install Evospike.Repository as a Cake Tool #tool nuget:?package=Evospike.Repository&version=1.0.2
Repository
This repository contains the code to build services 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 SqlServer and a generic repository to manage the database in SqlServer
appsettings.json
configuration
The file path and other settings can be read from JSON configuration if desired.
In appsettings.json
add a "MongoDbSettings", "ServiceSettings"
properties:
{
"MongoDbSettings": {
"Host": "localhost",
"Port": "27017"
},
"ServiceSettings": {
"ServiceName": "Catalog",
"Authority": "https://localhost:5003"
}
}
And then pass the configuration section to the next methods:
services.AddMongo()
.AddMongoRepository<Item>("{{YourTableName}}");
Or
services.AddSqlRepository<Item, {{YourDbContext}}>();
Your model must inherit from Entity class
public class Item : Entity
{
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;
public ItemsController(IRepository<Item> itemsRepository)
{
_itemsRepository = itemsRepository;
}
[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);
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);
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);
return NoContent();
}
}
Product | Versions 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. |
-
net5.0
- Microsoft.EntityFrameworkCore (>= 5.0.9)
- Microsoft.Extensions.Configuration (>= 5.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 5.0.0)
- Microsoft.Extensions.DependencyInjection (>= 5.0.2)
- MongoDB.Driver (>= 2.13.1)
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.2 | 322 | 8/25/2021 |