forked from samus/mongodb-csharp
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdc8135
commit 3420e3d
Showing
5 changed files
with
365 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Text; | ||
|
||
using MongoDB.Configuration; | ||
|
||
namespace MongoDB.DataContext | ||
{ | ||
public interface IMongoDataContext : IDisposable | ||
{ | ||
/// <summary> | ||
/// Gets the configuration. | ||
/// </summary> | ||
/// <value>The configuration.</value> | ||
IMongoConfiguration Configuration { get; } | ||
|
||
/// <summary> | ||
/// Gets the database. | ||
/// </summary> | ||
/// <value>The database.</value> | ||
IMongoDatabase Database { get; } | ||
|
||
/// <summary> | ||
/// Begins a queryable find. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <returns></returns> | ||
IQueryable<T> Find<T>() where T : class; | ||
|
||
/// <summary> | ||
/// Finds all the documents matching the predicate. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="predicate">The predicate.</param> | ||
/// <returns></returns> | ||
IEnumerable<T> Find<T>(Expression<Func<T, bool>> predicate) where T : class; | ||
|
||
/// <summary> | ||
/// Finds all the documents matching the predicate. | ||
/// </summary> | ||
/// <param name="predicate">The predicate.</param> | ||
/// <returns></returns> | ||
IEnumerable<T> Find<T>(object predicate) where T : class; | ||
|
||
/// <summary> | ||
/// Removes the specified document. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="document">The document.</param> | ||
void Remove<T>(T document) where T : class; | ||
|
||
/// <summary> | ||
/// Removes all the entities matching the predicate. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="predicate">The predicate.</param> | ||
void Remove<T>(Expression<Func<T, bool>> predicate) where T : class; | ||
|
||
/// <summary> | ||
/// Removes all the entities matching the predicate. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="predicate">The predicate.</param> | ||
void Remove<T>(object predicate) where T : class; | ||
|
||
/// <summary> | ||
/// Saves the specified document. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="document">The document.</param> | ||
void Save<T>(T document) where T : class; | ||
|
||
/// <summary> | ||
/// Updates all the documents that match the predicate with the specified values. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="values">The values.</param> | ||
/// <param name="predicate">The predicate.</param> | ||
void Update<T>(object values, object predicate) where T : class; | ||
|
||
/// <summary> | ||
/// Updates all the documents that match the predicate with the specified values. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="values">The values.</param> | ||
/// <param name="predicate">The predicate.</param> | ||
void Update<T>(object values, Expression<Func<T, bool>> predicate) where T : class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProductVersion>8.0.30703</ProductVersion> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
<ProjectGuid>{5E413F58-FA04-4C9A-96B9-12AD43731C24}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>MongoDB.DataContext</RootNamespace> | ||
<AssemblyName>MongoDB.DataContext</AssemblyName> | ||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="..\..\AssemblyInfoGlobal.cs"> | ||
<Link>Properties\AssemblyInfoGlobal.cs</Link> | ||
</Compile> | ||
<Compile Include="IMongoDataContext.cs" /> | ||
<Compile Include="MongoDataContext.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\MongoDB\MongoDB.csproj"> | ||
<Project>{B125BBA6-BFFD-44FA-9254-9B1754CD8AF3}</Project> | ||
<Name>MongoDB</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
Other similar extension points exist, see Microsoft.Common.targets. | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
--> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Text; | ||
|
||
using MongoDB.Configuration; | ||
using MongoDB.Linq; | ||
|
||
namespace MongoDB.DataContext | ||
{ | ||
public class MongoDataContext : IMongoDataContext | ||
{ | ||
private readonly IMongoConfiguration _configuration; | ||
private readonly IMongoDatabase _database; | ||
private IMongo _mongo; | ||
|
||
/// <summary> | ||
/// Gets the configuration. | ||
/// </summary> | ||
/// <value>The configuration.</value> | ||
public IMongoConfiguration Configuration | ||
{ | ||
get { return _configuration; } | ||
} | ||
|
||
/// <summary> | ||
/// Gets the database. | ||
/// </summary> | ||
/// <value>The database.</value> | ||
public IMongoDatabase Database | ||
{ | ||
get { return _database; } | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MongoDataContext"/> class. | ||
/// </summary> | ||
/// <param name="configuration">The configuration.</param> | ||
public MongoDataContext(IMongoConfiguration configuration) | ||
{ | ||
if (configuration == null) | ||
throw new ArgumentNullException("configuration"); | ||
configuration.Validate(); | ||
|
||
_configuration = configuration; | ||
|
||
var databaseName = new MongoConnectionStringBuilder(configuration.ConnectionString).Database; | ||
if (string.IsNullOrEmpty(databaseName)) | ||
databaseName = MongoConnectionStringBuilder.DefaultDatabase; | ||
|
||
_mongo = new Mongo(_configuration); | ||
_mongo.Connect(); | ||
_database = _mongo.GetDatabase(databaseName); | ||
} | ||
|
||
/// <summary> | ||
/// Releases unmanaged resources and performs other cleanup operations before the | ||
/// <see cref="MongoDataContext"/> is reclaimed by garbage collection. | ||
/// </summary> | ||
~MongoDataContext() | ||
{ | ||
Dispose(false); | ||
} | ||
|
||
/// <summary> | ||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
/// <summary> | ||
/// Begins a queryable find. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <returns></returns> | ||
public IQueryable<T> Find<T>() where T : class | ||
{ | ||
return _database.GetCollection<T>().Linq(); | ||
} | ||
|
||
/// <summary> | ||
/// Finds all the documents matching the predicate. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="predicate">The predicate.</param> | ||
/// <returns></returns> | ||
public IEnumerable<T> Find<T>(Expression<Func<T, bool>> predicate) where T : class | ||
{ | ||
return _database.GetCollection<T>().Find(predicate).Documents; | ||
} | ||
|
||
/// <summary> | ||
/// Finds all the documents matching the predicate. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="predicate">The predicate.</param> | ||
/// <returns></returns> | ||
public IEnumerable<T> Find<T>(object predicate) where T : class | ||
{ | ||
return _database.GetCollection<T>().Find(predicate).Documents; | ||
} | ||
|
||
/// <summary> | ||
/// Removes the specified document. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="document">The document.</param> | ||
public void Remove<T>(T document) where T : class | ||
{ | ||
_database.GetCollection<T>().Remove(document); | ||
} | ||
|
||
/// <summary> | ||
/// Removes all the entities matching the predicate. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="predicate">The predicate.</param> | ||
public void Remove<T>(Expression<Func<T, bool>> predicate) where T : class | ||
{ | ||
_database.GetCollection<T>().Remove(predicate); | ||
} | ||
|
||
/// <summary> | ||
/// Removes all the entities matching the predicate. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="predicate">The predicate.</param> | ||
public void Remove<T>(object predicate) where T : class | ||
{ | ||
_database.GetCollection<T>().Remove(predicate); | ||
} | ||
|
||
/// <summary> | ||
/// Saves the specified document. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="document">The document.</param> | ||
public void Save<T>(T document) where T : class | ||
{ | ||
_database.GetCollection<T>().Save(document); | ||
} | ||
|
||
/// <summary> | ||
/// Updates all the documents that match the predicate with the specified values. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="values">The values.</param> | ||
/// <param name="predicate">The predicate.</param> | ||
public void Update<T>(object values, object predicate) where T : class | ||
{ | ||
_database.GetCollection<T>().Update(values, predicate); | ||
} | ||
|
||
/// <summary> | ||
/// Updates all the documents that match the predicate with the specified values. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="values">The values.</param> | ||
/// <param name="predicate">The predicate.</param> | ||
public void Update<T>(object values, Expression<Func<T, bool>> predicate) where T : class | ||
{ | ||
_database.GetCollection<T>().Update(values, predicate); | ||
} | ||
|
||
/// <summary> | ||
/// Releases unmanaged and - optionally - managed resources | ||
/// </summary> | ||
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> | ||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!disposing) | ||
return; | ||
|
||
var disposable = _mongo as IDisposable; | ||
if (disposable != null) | ||
disposable.Dispose(); | ||
else | ||
_mongo.Disconnect(); | ||
|
||
_mongo = null; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Security.Permissions; | ||
|
||
[assembly: AssemblyTitle("MongoDB.DataContext")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyProduct("MongoDB.DataContext")] | ||
|
||
[assembly: System.Runtime.InteropServices.ComVisible(false)] | ||
[assembly: CLSCompliantAttribute(true)] | ||
|
||
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Bson")] | ||
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Bson")] | ||
[assembly: SecurityPermission(SecurityAction.RequestMinimum, Execution = true)] |