Canducci.MongoDB.Repository 2.0.0

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

// Install Canducci.MongoDB.Repository as a Cake Tool
#tool nuget:?package=Canducci.MongoDB.Repository&version=2.0.0                

Canducci MongoDB Repository

NuGet NuGet Build Status Github Workflows

Install Package (NUGET)

To install Canducci MongoDB Repository run the following command in the Package Manager Console

PM> Install-Package Canducci.MongoDB.Repository
How to use?

Create in your appsettings.json key MongoDB:

{
  ...
  "MongoDB": {
    "Database": "dbnew",
    "ConnectionStrings": "mongodb://localhost:27017"
  }
}

Make a class that represents your Collection in MongoDB

using Canducci.MongoDB.Repository.Attributes;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.IdGenerators;
using System.ComponentModel.DataAnnotations;

namespace WebApp.Models
{
    [BsonCollectionName("person")]
    public class Person
    {
        [BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
        public string Id { get; set; }

        [BsonElement("name")]
        [BsonRequired()]
        [Required()]
        public string Name { get; set; }
    }
}

Note: It has a BsonCollectionName attribute that has the configuration of the name of your collection in mongo , if by chance not pass he takes the class name.

Next step will be the creation of Repository.

Create a class and declare these two namespace:

Codification:
using Canducci.MongoDB.Repository;
namespace WebApp.Models
{
    //class abstract
    public abstract class RepositoryPersonImplementation : Repository<Person>
    {
        protected RepositoryPersonImplementation(IConnect connect) : base(connect)
        {
        }
    }
}

namespace WebApp.Models
{
    //class Concret
    public sealed class RepositoryPerson : RepositoryPersonImplementation
    {
        public RepositoryPerson(IConnect connect) : base(connect)
        {
        }
    }
}

Configure Startup.cs in method ConfigureServices:

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<IConnect, Connect>();
    services.AddScoped<RepositoryPersonImplementation, RepositoryPerson>();

    services.AddControllersWithViews();
}

In controller:

public class PersonController : Controller
{
    public RepositoryPersonImplementation Repository { get; }

    public PersonController(RepositoryPersonImplementation repository)
    {
        Repository = repository;
    }
            
    public ActionResult Index()
    {
        var data = Repository.All();
        return View(data);
    }

    public ActionResult Details(string id)
    {
        var data = Repository.Find(x => x.Id == id);
        return View(data);
    }

    public ActionResult Create()
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Person person)
    {
        try
        {
            if (ModelState.IsValid)
            {
                Repository.Add(person);
            }
            return RedirectToAction(nameof(Index));
        }
        catch
        {
            return View();
        }
    }

    public ActionResult Edit(string id)
    {
        var data = Repository.Find(x => x.Id == id);
        return View(data);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(string id, Person person)
    {
        try
        {
            if (ModelState.IsValid)
            {
                Repository.Edit(x => x.Id == id, person);
            }
            return RedirectToAction(nameof(Index));
        }
        catch
        {
            return View();
        }
    }

    public ActionResult Delete(string id)
    {
        var data = Repository.Find(x => x.Id == id);
        return View(data);
    }
            
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Delete(string id, IFormCollection collection)
    {
        try
        {
            Repository.Delete(x => x.Id == id);
            return RedirectToAction(nameof(Index));
        }
        catch
        {
            return View();
        }
    }
}
Paginated:

In controller:

public async Task<IActionResult> Index(int? page)
{
    int total = 5;
    IPagedList<Person> data = await Repository
        .PagedListAsync(page ?? 1, total);
    return View(data);
}

In View

@model Canducci.MongoDB.Repository.Paged.IPagedList<WebApp.Models.Person>

@{
  ViewData["Title"] = "Index";
}

<h1>Count Register Person @ViewBag.Count</h1>

<p>
  <a asp-action="Create">Create New</a>
</p>
<table class="table">
  <thead>
    <tr>
      <th class="text-center">
        Id
      </th>
      <th class="text-center">
        Name
      </th>
      <th class="text-center">...</th>
    </tr>
  </thead>
  <tbody>
    @foreach (var item in Model)
    {
      <tr>
        <td style="width:23%;text-align:center">
          @Html.DisplayFor(modelItem => item.Id)
        </td>
        <td>
          @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td style="width:20%;text-align:center">
          @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
          @Html.ActionLink("Details", "Details", new { id = item.Id }) |
          @Html.ActionLink("Delete", "Delete", new { id = item.Id })
        </td>
      </tr>
}
  </tbody>
</table>
<div class="row">
  <div class="col-md-5 text-right">
    @if (Model.HasPreviousPage)
    {
      <a href="/Person?page=@(Model.PageNumber - 1)">Previous</a>
    }
  </div>
  <div class="col-md-5 text-left">
    @if (Model.HasNextPage)
    {
      <a href="/Person?page=@(Model.PageNumber + 1)">Next</a>
    }
  </div>
</div>

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 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. 
.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 is compatible.  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
2.0.0 292 3/14/2023
1.1.1 1,749 12/21/2020
1.1.0 622 8/21/2020
1.0.0 583 6/3/2020

Short methods