TStack.MongoDB
1.0.1
dotnet add package TStack.MongoDB --version 1.0.1
NuGet\Install-Package TStack.MongoDB -Version 1.0.1
<PackageReference Include="TStack.MongoDB" Version="1.0.1" />
<PackageVersion Include="TStack.MongoDB" Version="1.0.1" />
<PackageReference Include="TStack.MongoDB" />
paket add TStack.MongoDB --version 1.0.1
#r "nuget: TStack.MongoDB, 1.0.1"
#addin nuget:?package=TStack.MongoDB&version=1.0.1
#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
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
Product | Versions 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.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. |
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection (>= 2.0.0)
- MongoDB.Driver (>= 2.8.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
replicaset name added on connection