example also in framework

alphons <alphons@heijden.com> 22 Oct 2024, 10:59
106c17ddf2b60cbf5fc956c8268b5712126e248f
7 files changed
  • src/MongoExampleFramework/App.config
  • src/MongoExampleFramework/CrmContext.cs
  • src/MongoExampleFramework/MongoExampleFramework.csproj
  • src/MongoExampleFramework/MongoExtensions.cs
  • src/MongoExampleFramework/Program.cs
  • src/MongoExampleFramework/Properties/AssemblyInfo.cs
  • src/MongoExtensions.sln
diff --git a/src/MongoExampleFramework/App.config b/src/MongoExampleFramework/App.config
new file mode 100644
index 0000000..cc8e9d5
--- /dev/null
+++ b/src/MongoExampleFramework/App.config
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<configuration>
+ <startup>
+ <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
+ </startup>
+ <connectionStrings>
+ <add connectionString="mongodb://127.0.0.1" name="mongo" />
+ </connectionStrings>
+ <runtime>
+ </runtime>
+</configuration>
\ No newline at end of file
diff --git a/src/MongoExampleFramework/CrmContext.cs b/src/MongoExampleFramework/CrmContext.cs
new file mode 100644
index 0000000..5effdd4
--- /dev/null
+++ b/src/MongoExampleFramework/CrmContext.cs
@@ -0,0 +1,34 @@
+using MongoDB.Bson;
+using MongoDB.Driver;
+using MongoDb.Extensions;
+
+
+namespace MongoExampleFramework
+{
+
+ public class CrmContext : MongoDbContext
+ {
+ public CrmContext() : base("dbCrm")
+ {
+ }
+
+ public IMongoCollection<Klant> KlantTable => Table<Klant>();
+ public IMongoCollection<Hypotheek> HypotheekTable => Table<Hypotheek>();
+ }
+
+ public class Hypotheek
+ {
+ public ObjectId Id { get; set; }
+ public string Name { get; set; }
+ public string Rente { get; set; }
+ }
+
+ public class Klant
+ {
+ public ObjectId Id { get; set; }
+ public string Name { get; set; }
+ public string Address { get; set; }
+ public int Age { get; set; }
+ }
+
+}
diff --git a/src/MongoExampleFramework/MongoExampleFramework.csproj b/src/MongoExampleFramework/MongoExampleFramework.csproj
new file mode 100644
index 0000000..cf46e5e
--- /dev/null
+++ b/src/MongoExampleFramework/MongoExampleFramework.csproj
@@ -0,0 +1,159 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{1AB4C45B-BDD8-4232-8944-69144E9BC809}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <RootNamespace>MongoExampleFramework</RootNamespace>
+ <AssemblyName>MongoExampleFramework</AssemblyName>
+ <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
+ <Deterministic>true</Deterministic>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <PlatformTarget>AnyCPU</PlatformTarget>
+ <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' ">
+ <PlatformTarget>AnyCPU</PlatformTarget>
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="DnsClient, Version=1.8.0.0, Culture=neutral, PublicKeyToken=4574bb5573c51424, processorArchitecture=MSIL">
+ <HintPath>..\packages\DnsClient.1.8.0\lib\net472\DnsClient.dll</HintPath>
+ </Reference>
+ <Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
+ <HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.8.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
+ </Reference>
+ <Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions, Version=8.0.0.2, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
+ <HintPath>..\packages\Microsoft.Extensions.DependencyInjection.Abstractions.8.0.2\lib\net462\Microsoft.Extensions.DependencyInjection.Abstractions.dll</HintPath>
+ </Reference>
+ <Reference Include="Microsoft.Extensions.Logging.Abstractions, Version=8.0.0.2, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
+ <HintPath>..\packages\Microsoft.Extensions.Logging.Abstractions.8.0.2\lib\net462\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
+ </Reference>
+ <Reference Include="Microsoft.Win32.Registry, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <HintPath>..\packages\Microsoft.Win32.Registry.5.0.0\lib\net461\Microsoft.Win32.Registry.dll</HintPath>
+ </Reference>
+ <Reference Include="MongoDB.Bson, Version=3.0.0.0, Culture=neutral, PublicKeyToken=94992a530f44e321, processorArchitecture=MSIL">
+ <HintPath>..\packages\MongoDB.Bson.3.0.0\lib\net472\MongoDB.Bson.dll</HintPath>
+ </Reference>
+ <Reference Include="MongoDB.Driver, Version=3.0.0.0, Culture=neutral, PublicKeyToken=94992a530f44e321, processorArchitecture=MSIL">
+ <HintPath>..\packages\MongoDB.Driver.3.0.0\lib\net472\MongoDB.Driver.dll</HintPath>
+ </Reference>
+ <Reference Include="SharpCompress, Version=0.38.0.0, Culture=neutral, PublicKeyToken=afb0a02973931d96, processorArchitecture=MSIL">
+ <HintPath>..\packages\SharpCompress.0.38.0\lib\net462\SharpCompress.dll</HintPath>
+ </Reference>
+ <Reference Include="Snappier, Version=1.1.6.0, Culture=neutral, PublicKeyToken=a1b25124e6e13a3a, processorArchitecture=MSIL">
+ <HintPath>..\packages\Snappier.1.1.6\lib\netstandard2.0\Snappier.dll</HintPath>
+ </Reference>
+ <Reference Include="System" />
+ <Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
+ <HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
+ </Reference>
+ <Reference Include="System.ComponentModel.Composition" />
+ <Reference Include="System.Configuration" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Diagnostics.DiagnosticSource, Version=8.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
+ <HintPath>..\packages\System.Diagnostics.DiagnosticSource.8.0.1\lib\net462\System.Diagnostics.DiagnosticSource.dll</HintPath>
+ </Reference>
+ <Reference Include="System.IO, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <HintPath>..\packages\System.IO.4.3.0\lib\net462\System.IO.dll</HintPath>
+ <Private>True</Private>
+ <Private>True</Private>
+ </Reference>
+ <Reference Include="System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
+ <HintPath>..\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Net.Http, Version=4.1.1.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <HintPath>..\packages\System.Net.Http.4.3.4\lib\net46\System.Net.Http.dll</HintPath>
+ <Private>True</Private>
+ <Private>True</Private>
+ </Reference>
+ <Reference Include="System.Numerics" />
+ <Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Runtime, Version=4.1.1.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <HintPath>..\packages\System.Runtime.4.3.1\lib\net462\System.Runtime.dll</HintPath>
+ <Private>True</Private>
+ <Private>True</Private>
+ </Reference>
+ <Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <HintPath>..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
+ <Private>True</Private>
+ <Private>True</Private>
+ </Reference>
+ <Reference Include="System.Security.AccessControl, Version=6.0.0.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <HintPath>..\packages\System.Security.AccessControl.6.0.1\lib\net461\System.Security.AccessControl.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Security.Cryptography.Algorithms, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <HintPath>..\packages\System.Security.Cryptography.Algorithms.4.3.1\lib\net463\System.Security.Cryptography.Algorithms.dll</HintPath>
+ <Private>True</Private>
+ <Private>True</Private>
+ </Reference>
+ <Reference Include="System.Security.Cryptography.Encoding, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <HintPath>..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll</HintPath>
+ <Private>True</Private>
+ <Private>True</Private>
+ </Reference>
+ <Reference Include="System.Security.Cryptography.Primitives, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <HintPath>..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll</HintPath>
+ <Private>True</Private>
+ <Private>True</Private>
+ </Reference>
+ <Reference Include="System.Security.Cryptography.X509Certificates, Version=4.1.1.2, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <HintPath>..\packages\System.Security.Cryptography.X509Certificates.4.3.2\lib\net461\System.Security.Cryptography.X509Certificates.dll</HintPath>
+ <Private>True</Private>
+ <Private>True</Private>
+ </Reference>
+ <Reference Include="System.Security.Principal.Windows, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <HintPath>..\packages\System.Security.Principal.Windows.5.0.0\lib\net461\System.Security.Principal.Windows.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Text.Encoding.CodePages, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <HintPath>..\packages\System.Text.Encoding.CodePages.8.0.0\lib\net462\System.Text.Encoding.CodePages.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
+ <HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ <Reference Include="ZstdSharp, Version=0.8.1.0, Culture=neutral, PublicKeyToken=8d151af33a4ad5cf, processorArchitecture=MSIL">
+ <HintPath>..\packages\ZstdSharp.Port.0.8.1\lib\net462\ZstdSharp.dll</HintPath>
+ </Reference>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="CrmContext.cs" />
+ <Compile Include="MongoExtensions.cs" />
+ <Compile Include="Program.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="App.config" />
+ </ItemGroup>
+ <ItemGroup>
+ <PackageReference Include="MongoDB.Driver">
+ <Version>3.0.0</Version>
+ </PackageReference>
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+</Project>
\ No newline at end of file
diff --git a/src/MongoExampleFramework/MongoExtensions.cs b/src/MongoExampleFramework/MongoExtensions.cs
new file mode 100644
index 0000000..45cd50e
--- /dev/null
+++ b/src/MongoExampleFramework/MongoExtensions.cs
@@ -0,0 +1,63 @@
+using MongoDB.Driver;
+using System;
+using System.Collections.Generic;
+using System.Configuration;
+using System.Linq.Expressions;
+using System.Threading.Tasks;
+
+
+namespace MongoDb.Extensions
+{
+
+ public static class LinqExtensions
+ {
+ public static IFindFluent<T, T> Where<T>(this IMongoCollection<T> collection, Expression<Func<T, bool>> filter)
+ {
+ return collection.Find(filter);
+ }
+
+ public static T FirstOrDefault<T>(this IMongoCollection<T> collection, Expression<Func<T, bool>> filter)
+ {
+ var results = collection.Find(filter);
+
+ return results.FirstOrDefault();
+ }
+
+ public async static Task<T> FirstOrDefaultAsync<T>(this IMongoCollection<T> collection, Expression<Func<T, bool>> filter)
+ {
+ var results = await collection.FindAsync(filter);
+
+ if (results == null)
+ return default;
+ else
+ return await results.FirstOrDefaultAsync();
+ }
+
+ public async static Task<List<T>> ToListAsync<T>(this IMongoCollection<T> collection, Expression<Func<T, bool>> filter)
+ {
+ var results = await collection.FindAsync(filter);
+
+ if (results == null)
+ return new List<T>();
+ else
+ return await results.ToListAsync();
+ }
+ }
+
+ public class MongoDbContext
+ {
+ public readonly MongoClient Client;
+
+ private readonly IMongoDatabase db;
+
+ public MongoDbContext(string name)
+ {
+ Client = new MongoClient(ConfigurationManager.ConnectionStrings["mongo"].ConnectionString);
+
+ db = Client.GetDatabase(name);
+ }
+
+ public IMongoCollection<T> Table<T>() => db.GetCollection<T>($"{typeof(T).Name}Table");
+ }
+
+}
\ No newline at end of file
diff --git a/src/MongoExampleFramework/Program.cs b/src/MongoExampleFramework/Program.cs
new file mode 100644
index 0000000..641de5d
--- /dev/null
+++ b/src/MongoExampleFramework/Program.cs
@@ -0,0 +1,33 @@
+using MongoDb.Extensions;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MongoExampleFramework
+{
+ internal class Program
+ {
+ static void Main(string[] args)
+ {
+ var db = new CrmContext();
+
+ var klant = db.KlantTable.FirstOrDefault(x => x.Name == "Peter");
+
+ if (klant == null)
+ {
+ klant = new Klant()
+ {
+ Name = "Peter",
+ Address = "Street 11",
+ Age = 35
+ };
+
+ db.KlantTable.InsertOne(klant);
+ }
+
+ Console.WriteLine($"Het {klant.Address}");
+ }
+ }
+}
diff --git a/src/MongoExampleFramework/Properties/AssemblyInfo.cs b/src/MongoExampleFramework/Properties/AssemblyInfo.cs
new file mode 100644
index 0000000..b6d727a
--- /dev/null
+++ b/src/MongoExampleFramework/Properties/AssemblyInfo.cs
@@ -0,0 +1,33 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("MongoExampleFramework")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("MongoExampleFramework")]
+[assembly: AssemblyCopyright("Copyright © 2024")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("1ab4c45b-bdd8-4232-8944-69144e9bc809")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
diff --git a/src/MongoExtensions.sln b/src/MongoExtensions.sln
index 252ad79..ec96640 100644
--- a/src/MongoExtensions.sln
+++ b/src/MongoExtensions.sln
@@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongoGui", "MongoGui\MongoG
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongoExample", "MongoExample\MongoExample.csproj", "{B398A402-4D22-47AE-AD79-D5C2224B160B}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoExampleFramework", "MongoExampleFramework\MongoExampleFramework.csproj", "{1AB4C45B-BDD8-4232-8944-69144E9BC809}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -51,6 +53,10 @@ Global
{B398A402-4D22-47AE-AD79-D5C2224B160B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B398A402-4D22-47AE-AD79-D5C2224B160B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B398A402-4D22-47AE-AD79-D5C2224B160B}.Release|Any CPU.Build.0 = Release|Any CPU
+ {1AB4C45B-BDD8-4232-8944-69144E9BC809}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {1AB4C45B-BDD8-4232-8944-69144E9BC809}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {1AB4C45B-BDD8-4232-8944-69144E9BC809}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {1AB4C45B-BDD8-4232-8944-69144E9BC809}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE