forked from samus/mongodb-csharp
/
Mongo.cs
135 lines (120 loc) · 4.17 KB
/
Mongo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Configuration;
using MongoDB.Connections;
namespace MongoDB
{
/// <summary>
/// Description of Mongo.
/// </summary>
public class Mongo : IDisposable, IMongo
{
private readonly MongoConfiguration _configuration;
private readonly Connection _connection;
/// <summary>
/// Initializes a new instance of the <see cref = "Mongo" /> class.
/// </summary>
public Mongo()
: this(new MongoConfiguration())
{
}
/// <summary>
/// Initializes a new instance of the <see cref = "Mongo" /> class.
/// </summary>
/// <param name = "connectionString">The connection string.</param>
public Mongo(string connectionString)
: this(new MongoConfiguration {ConnectionString = connectionString})
{
}
/// <summary>
/// Initializes a new instance of the <see cref = "Mongo" /> class.
/// </summary>
/// <param name = "configuration">The mongo configuration.</param>
public Mongo(MongoConfiguration configuration){
if(configuration == null)
throw new ArgumentNullException("configuration");
configuration.ValidateAndSeal();
_configuration = configuration;
_connection = ConnectionFactoryFactory.GetConnection(configuration.ConnectionString);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
_connection.Dispose();
}
/// <summary>
/// Gets the connection string.
/// </summary>
/// <value>The connection string.</value>
public string ConnectionString
{
get { return _connection.ConnectionString; }
}
/// <summary>
/// Gets the named database.
/// </summary>
/// <param name = "name">The name.</param>
/// <returns></returns>
public IMongoDatabase GetDatabase(String name)
{
return new MongoDatabase(_configuration, _connection, name);
}
/// <summary>
/// Gets the <see cref = "MongoDatabase" /> with the specified name.
/// </summary>
/// <value></value>
public IMongoDatabase this[String name]
{
get { return GetDatabase(name); }
}
/// <summary>
/// Connects to server.
/// </summary>
/// <returns></returns>
/// <exception cref = "MongoDB.MongoConnectionException">Thrown when connection fails.</exception>
public void Connect()
{
_connection.Open();
}
/// <summary>
/// Tries to connect to server.
/// </summary>
/// <returns></returns>
public bool TryConnect()
{
try
{
_connection.Open();
return _connection.IsConnected;
}
catch(MongoException)
{
return _connection.IsConnected;
}
}
/// <summary>
/// Disconnects this instance.
/// </summary>
/// <returns></returns>
public bool Disconnect()
{
_connection.Close();
return _connection.IsConnected;
}
/// <summary>
/// Gets the databases.
/// </summary>
/// <returns></returns>
public IEnumerable<IMongoDatabase> GetDatabases()
{
var result = _connection.SendCommand(_configuration.SerializationFactory, "admin", typeof(Document), new Document("listDatabases", 1));
return ((IEnumerable<Document>)result["databases"])
.Select(database => (string)database["name"])
.Select(name => new MongoDatabase(_configuration, _connection, name))
.Cast<IMongoDatabase>();
}
}
}