|
| 1 | +/* Copyright 2010-2012 10gen Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Linq; |
| 19 | +using System.Text; |
| 20 | + |
| 21 | +namespace MongoDB.Driver |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Represents a client to MongoDB. |
| 25 | + /// </summary> |
| 26 | + public class MongoClient |
| 27 | + { |
| 28 | + // private fields |
| 29 | + private readonly MongoClientSettings _settings; |
| 30 | + |
| 31 | + // constructors |
| 32 | + /// <summary> |
| 33 | + /// Initializes a new instance of the MongoClient class. |
| 34 | + /// </summary> |
| 35 | + public MongoClient() |
| 36 | + : this(new MongoClientSettings()) |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Initializes a new instance of the MongoClient class. |
| 42 | + /// </summary> |
| 43 | + /// <param name="settings">The settings.</param> |
| 44 | + public MongoClient(MongoClientSettings settings) |
| 45 | + { |
| 46 | + _settings = settings.FrozenCopy(); |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Initializes a new instance of the MongoClient class. |
| 51 | + /// </summary> |
| 52 | + /// <param name="url">The URL.</param> |
| 53 | + public MongoClient(MongoUrl url) |
| 54 | + : this(MongoClientSettings.FromUrl(url)) |
| 55 | + { |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Initializes a new instance of the MongoClient class. |
| 60 | + /// </summary> |
| 61 | + /// <param name="connectionString">The connection string.</param> |
| 62 | + public MongoClient(string connectionString) |
| 63 | + : this(ParseConnectionString(connectionString)) |
| 64 | + { |
| 65 | + } |
| 66 | + |
| 67 | + // public properties |
| 68 | + /// <summary> |
| 69 | + /// Gets the client settings. |
| 70 | + /// </summary> |
| 71 | + public MongoClientSettings Settings |
| 72 | + { |
| 73 | + get { return _settings; } |
| 74 | + } |
| 75 | + |
| 76 | + // private static methods |
| 77 | + private static MongoClientSettings ParseConnectionString(string connectionString) |
| 78 | + { |
| 79 | + if (connectionString.StartsWith("mongodb://")) |
| 80 | + { |
| 81 | + var url = new MongoUrl(connectionString); |
| 82 | + return MongoClientSettings.FromUrl(url); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + var builder = new MongoConnectionStringBuilder(connectionString); |
| 87 | + return MongoClientSettings.FromConnectionStringBuilder(builder); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // public methods |
| 92 | + /// <summary> |
| 93 | + /// Gets a MongoServer object using this client's settings. |
| 94 | + /// </summary> |
| 95 | + /// <returns>A MongoServer.</returns> |
| 96 | + public MongoServer GetServer() |
| 97 | + { |
| 98 | + var serverSettings = MongoServerSettings.FromClientSettings(_settings); |
| 99 | +#pragma warning disable 618 |
| 100 | + return MongoServer.Create(serverSettings); |
| 101 | +#pragma warning restore |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments