TStack.MongoDB 1.0.1

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

// Install TStack.MongoDB as a Cake Tool
#tool nuget:?package=TStack.MongoDB&version=1.0.1

Overview

This library is designed for generic architecture based on mongodb. It can be used relationally because it has mapping feature. So, can make relational queries with defined rules.

Covarage

  • Generic Repository
  • CRUD
  • Relational Mapping
  • Cluster Mode

Installation

Nuget Package

Package Manager
Install-Package TStack.MongoDB -Version 1.0.0
.NET CLI
dotnet add package TStack.MongoDB --version 1.0.0
PackageReference
<PackageReference Include="TStack.MongoDB" Version="1.0.0" />
Paket CLI
paket add TStack.MongoDB --version 1.0.0

Usage

First must define connection to access mongodb engine.

For an example :

public class TestConnection : MongoConnection
{
    public TestConnection():base("localhost",27017,"testRepo")
    {

    }
}

Your enitity objects must be inherit from "MongoEntity" class.

For an example :

public class Person : MongoEntity
{
    public Person()
    {

    }
    public Person(string name, string surname, DateTime birthdate, double salary)
    {
        Name = name;
        Surname = surname;
        BirthDate = birthdate;
        Salary = salary;
    }
    public string Name { get; set; }
    public string Surname { get; set; }
  
    [BsonDateTimeOptions(Kind = DateTimeKind.Local)]//on insert save datetime on your local datetime otherwise universal
    public DateTime BirthDate { get; set; }
    public double Salary { get; set; }
    [BsonIgnore]//for relation must be add
    public PersonDetail PersonDetail { get; set; }
    [BsonIgnore]//for relation must be add
    public List<PersonAddress> Addresses { get; set; }
}

public class PersonAddress : MongoEntity
{
    public PersonAddress()
    {

    }
    public PersonAddress(string street, string apartment, string city)
    {
        Street = street;
        Apartment = apartment;
        City = city;
    }
    public string PersonId { get; set; }
    public string Street { get; set; }
    public string Apartment { get; set; }
    public string City { get; set; }
}

public class PersonDetail : MongoEntity
{
    public PersonDetail()
    {

    }
    public PersonDetail(string email, string phone)
    {
        Email = email;
        Phone = phone;
    }
    public string PersonId { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
}
// base class
public class MongoEntity : IMongoEntity
{
    /// <summary>
    /// id in string format
    /// </summary>
    [BsonElement(Order = 0)]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; } = ObjectId.GenerateNewId().ToString();

    /// <summary>
    /// id in objectId format
    /// </summary>
    public ObjectId ObjectId => ObjectId.Parse(Id);
}

Now, define repository to access methods to use.

### Without mapping

public class PersonRepository : MongoRepositoryBase<Person, TestConnection>
{

}

### With Mapping

Before define map class

public class PersonMapper : Mapper<Person>
{
    public PersonMapper() 
    {
        //example usage
        Rule("PersonDetail").Key(primaryKey => primaryKey.Id).WithOne(field => field.PersonDetail, relationKey => relationKey.PersonId);
        //example usage
        Rule("PersonAddresses").Key("Id").WithCollection(field => field.Addresses).RelationKey("PersonId");
    }
}

By the way you must fill RelationKey("") function, its required for init your rule.

Now define your repository like this.

public class PersonRepository : MongoRepositoryBase<Person, TestConnection, PersonMapper>
{

}

Fundamentals

Let's use mapped repository with rule.

For an example :

PersonRepository personRepository = new PersonRepository();

Person person = new Person("ferhat", "candas", DateTime.Now.AddYears(-15), 2000.52);

List<PersonAddress> personAddresses = new List<PersonAddress>()
{
    new PersonAddress("Fatih mah","besikduzu apt.","Trabzon"),
    new PersonAddress("Laik sokak","Çağrı apt.","Izmir"),
    new PersonAddress("Yilmaz sokak.","esenyurt apt.","Tokat"),
};

var personDetail = new PersonDetail("candasferhat61@gmail.com", "90537*******");

person.Addresses = personAddresses;

person.PersonDetail = personDetail;

personRepository.Insert(person, rule => rule.Name == "PersonDetail" || rule.Name == "PersonAddresses");

That's it you have create three collection on mongodb related with rules.

You can get data like this.

PersonRepository personRepository = new PersonRepository();
//example recordId
string recordId = "5d23a3036f4bca70448cf6de";
var personData = personRepository.First(person => person.Id == recordId, rule => rule.Name == "PersonDetail" || rule.Name == "PersonAddresses");

personData includes related objects on rule.

Author

Ferhat Candaş - Software Developer

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 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.0.1 587 7/15/2019
1.0.0 1,553 7/8/2019

replicaset name added on connection