TStack.MongoDB 1.0.1

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.

Install-Package TStack.MongoDB -Version 1.0.1
dotnet add package TStack.MongoDB --version 1.0.1
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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

  • Mail : candasferhat61@gmail.com
  • LinkedIn : https://www.linkedin.com/in/ferhatcandas

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

  • Mail : candasferhat61@gmail.com
  • LinkedIn : https://www.linkedin.com/in/ferhatcandas

Release Notes

replicaset name added on connection

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.1 61 7/15/2019
1.0.0 1,080 7/8/2019