diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-08-06 10:41:16 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-08-06 10:41:16 +0300 |
| commit | e86fe2e8cf34343cf3ebbf4640b2be5e85899615 (patch) | |
| tree | dee59a2c43b69b6abe20be24059d42ecabcdfffc /Software/Visual_Studio_v2/Tango.DAL.Mongo | |
| parent | c1b00ec58a47e504e0247c21c96b55a89e7a95eb (diff) | |
| download | Tango-e86fe2e8cf34343cf3ebbf4640b2be5e85899615.tar.gz Tango-e86fe2e8cf34343cf3ebbf4640b2be5e85899615.zip | |
v2 Basic structure.
Diffstat (limited to 'Software/Visual_Studio_v2/Tango.DAL.Mongo')
3 files changed, 103 insertions, 0 deletions
diff --git a/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoDataBaseSettings.cs b/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoDataBaseSettings.cs new file mode 100644 index 000000000..5c44f2022 --- /dev/null +++ b/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoDataBaseSettings.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tango.DAL.Mongo +{ + public class MongoDataBaseSettings + { + public String Address { get; set; } + public String DatabaseName { get; set; } + } +} diff --git a/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoRepository.cs b/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoRepository.cs new file mode 100644 index 000000000..d268f434d --- /dev/null +++ b/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoRepository.cs @@ -0,0 +1,76 @@ +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Reflection; +using Tango.DAL.Attributes; +using System.Linq.Expressions; +using MongoDB.Driver.Linq; + +namespace Tango.DAL.Mongo +{ + public class MongoRepository<T> : IRepository<T> where T : Entity + { + private IMongoDatabase _database; + private MongoClient _client; + private IMongoCollection<T> _collection; + + public MongoRepository(MongoDataBaseSettings settings) + { + _client = new MongoClient(settings.Address); + _database = _client.GetDatabase(settings.DatabaseName); + } + + private IMongoCollection<T> GetCollection() + { + if (_collection == null) + { + _collection = _database.GetCollection<T>(typeof(T).GetCustomAttribute<MongoCollectionAttribute>().Name); + } + + return _collection; + } + + public async Task<List<T>> GetAllAsync() + { + return await (await GetCollection().FindAsync(x => true)).ToListAsync(); + } + + public async Task<List<T>> GetAsync(Expression<Func<T, bool>> filter) + { + return await (await GetCollection().FindAsync(filter)).ToListAsync(); + } + + public async Task<T> ReplaceAsync(T entity) + { + await GetCollection().ReplaceOneAsync(x => x.ID == entity.ID, entity); + return entity; + } + + public async Task<long> DeleteAsync(Expression<Func<T, bool>> filter) + { + return (await GetCollection().DeleteOneAsync(filter)).DeletedCount; + } + + public async Task<T> Insert(T entity) + { + await GetCollection().InsertOneAsync(entity); + return entity; + } + + public Task<long> Count() + { + return GetCollection().CountDocumentsAsync(x => true); + } + + public Task<long> Count(Expression<Func<T, bool>> filter) + { + return GetCollection().CountDocumentsAsync(filter); + } + + public void Dispose() + { + + } + } +} diff --git a/Software/Visual_Studio_v2/Tango.DAL.Mongo/Tango.DAL.Mongo.csproj b/Software/Visual_Studio_v2/Tango.DAL.Mongo/Tango.DAL.Mongo.csproj new file mode 100644 index 000000000..7f0e7a2f0 --- /dev/null +++ b/Software/Visual_Studio_v2/Tango.DAL.Mongo/Tango.DAL.Mongo.csproj @@ -0,0 +1,15 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>netstandard2.0</TargetFramework> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="MongoDB.Driver" Version="2.8.1" /> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\Tango.DAL\Tango.DAL.csproj" /> + </ItemGroup> + +</Project> |
