Skip to content

Commit

Permalink
added MongoDB.DataContext
Browse files Browse the repository at this point in the history
  • Loading branch information
craiggwilson committed May 9, 2010
1 parent bdc8135 commit 3420e3d
Show file tree
Hide file tree
Showing 5 changed files with 365 additions and 0 deletions.
6 changes: 6 additions & 0 deletions MongoDB-CSharp-2010.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDB.Tests", "source\Mon
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benchmark", "tools\Benchmark\Benchmark.csproj", "{5ACD68A0-0F2E-452A-90E3-3D1CB82C055B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDB.DataContext", "source\MongoDB.DataContext\MongoDB.DataContext.csproj", "{5E413F58-FA04-4C9A-96B9-12AD43731C24}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -53,6 +55,10 @@ Global
{5ACD68A0-0F2E-452A-90E3-3D1CB82C055B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5ACD68A0-0F2E-452A-90E3-3D1CB82C055B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5ACD68A0-0F2E-452A-90E3-3D1CB82C055B}.Release|Any CPU.Build.0 = Release|Any CPU
{5E413F58-FA04-4C9A-96B9-12AD43731C24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E413F58-FA04-4C9A-96B9-12AD43731C24}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E413F58-FA04-4C9A-96B9-12AD43731C24}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E413F58-FA04-4C9A-96B9-12AD43731C24}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
91 changes: 91 additions & 0 deletions source/MongoDB.DataContext/IMongoDataContext.cs
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;
}
}
64 changes: 64 additions & 0 deletions source/MongoDB.DataContext/MongoDB.DataContext.csproj
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>
188 changes: 188 additions & 0 deletions source/MongoDB.DataContext/MongoDataContext.cs
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;
}

}
}
16 changes: 16 additions & 0 deletions source/MongoDB.DataContext/Properties/AssemblyInfo.cs
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)]

0 comments on commit 3420e3d

Please sign in to comment.