diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-08-08 15:50:21 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-08-08 15:50:21 +0300 |
| commit | 9240614f1bc024801789054092a961c6193695fb (patch) | |
| tree | b4373828029ff9ea12251bc572d36ab519bf40ed /Software/Visual_Studio_v2/Tango.DAL.Mongo | |
| parent | 97411eb2550c6b483181b823da12f127cf903170 (diff) | |
| download | Tango-9240614f1bc024801789054092a961c6193695fb.tar.gz Tango-9240614f1bc024801789054092a961c6193695fb.zip | |
v2 Working on unit of work.
Diffstat (limited to 'Software/Visual_Studio_v2/Tango.DAL.Mongo')
| -rw-r--r-- | Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoRepository.cs | 5 | ||||
| -rw-r--r-- | Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoUnitOfWork.cs | 56 |
2 files changed, 61 insertions, 0 deletions
diff --git a/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoRepository.cs b/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoRepository.cs index 864bb17cc..7ffdc505a 100644 --- a/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoRepository.cs +++ b/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoRepository.cs @@ -23,6 +23,11 @@ namespace Tango.DAL.Mongo _database = _client.GetDatabase(settings.DatabaseName); } + public MongoRepository(IMongoDatabase database) + { + _database = database; + } + private IMongoCollection<T> GetCollection() { if (_collection == null) diff --git a/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoUnitOfWork.cs b/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoUnitOfWork.cs new file mode 100644 index 000000000..b85dab1da --- /dev/null +++ b/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoUnitOfWork.cs @@ -0,0 +1,56 @@ +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Text; +using Tango.DAL.Entities; + +namespace Tango.DAL.Mongo +{ + public class MongoUnitOfWork : IUnitOfWork + { + private IMongoDatabase _database; + private MongoClient _client; + private IClientSessionHandle _sessionHandler; + private bool _commited; + + private IRepository<OrganizationEntity> _organizations; + private IRepository<MachineEntity> _machines; + + public MongoUnitOfWork(MongoDataBaseSettings settings) + { + _client = new MongoClient(settings.Address); + _database = _client.GetDatabase(settings.DatabaseName); + + _sessionHandler = _client.StartSession(); + } + + public IRepository<OrganizationEntity> Organizations + { + get + { + return _organizations ?? new MongoRepository<OrganizationEntity>(_database); + } + } + + public IRepository<MachineEntity> Machines + { + get + { + return _machines ?? new MongoRepository<MachineEntity>(_database); + } + } + + public void Commit() + { + _sessionHandler.CommitTransaction(); + } + + public void Dispose() + { + if (!_commited) + { + _sessionHandler.AbortTransaction(); + } + } + } +} |
