Add support for ObjectId type for the Id of the document.
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
using CoreIntegrationTests.Infrastructure;
|
||||
using MongoDB.Bson;
|
||||
using System;
|
||||
namespace CoreIntegrationTests
|
||||
{
|
||||
public class CoreObjectIdTestDocument : TestDoc<ObjectId>
|
||||
{
|
||||
}
|
||||
|
||||
public class CRUDObjectIdTests : MongoDbTKeyDocumentTestBase<CoreObjectIdTestDocument, ObjectId>
|
||||
{
|
||||
public CRUDObjectIdTests(MongoDbTestFixture<CoreObjectIdTestDocument, ObjectId> fixture) : base(fixture)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override string GetClassName()
|
||||
{
|
||||
return "CRUDObjectIdTests";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,13 +9,16 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.0.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.5.0" />
|
||||
<PackageReference Include="MongoDbGenericRepository" Version="1.3.5" />
|
||||
<PackageReference Include="xunit" Version="2.3.1" />
|
||||
<PackageReference Include="xunit.runner.console" Version="2.3.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
|
||||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta4-build3742" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MongoDbGenericRepository\MongoDbGenericRepository.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="System.Configuration">
|
||||
<HintPath>..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Configuration.dll</HintPath>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using MongoDbGenericRepository.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
@@ -82,22 +83,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
|
||||
public TId Init<TId>()
|
||||
{
|
||||
var idTypeName = typeof(TKey).Name;
|
||||
switch (idTypeName)
|
||||
{
|
||||
case "Guid":
|
||||
return (TId)(object)Guid.NewGuid();
|
||||
case "Int16":
|
||||
return (TId)(object)GlobalVariables.Random.Next(1, short.MaxValue);
|
||||
case "Int32":
|
||||
return (TId)(object)GlobalVariables.Random.Next(1, int.MaxValue);
|
||||
case "Int64":
|
||||
return (TId)(object)(GlobalVariables.Random.NextLong(1, long.MaxValue));
|
||||
case "String":
|
||||
return (TId)(object)Guid.NewGuid().ToString();
|
||||
default:
|
||||
throw new NotSupportedException($"{idTypeName} is not supported.");
|
||||
}
|
||||
return IdGenerator.GetId<TId>();
|
||||
}
|
||||
|
||||
private void InitializeFields()
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDB.Bson;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class ObjectIdTestDocument : TestDoc<ObjectId>
|
||||
{
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class CRUDObjectIdTests : MongoDbTKeyDocumentTestBase<ObjectIdTestDocument, ObjectId>
|
||||
{
|
||||
public override string GetClassName()
|
||||
{
|
||||
return "CRUDObjectIdTests";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using MongoDbGenericRepository.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
@@ -82,22 +83,7 @@ namespace IntegrationTests.Infrastructure
|
||||
|
||||
public TId Init<TId>()
|
||||
{
|
||||
var idTypeName = typeof(TKey).Name;
|
||||
switch (idTypeName)
|
||||
{
|
||||
case "Guid":
|
||||
return (TId)(object)Guid.NewGuid();
|
||||
case "Int16":
|
||||
return (TId)(object)GlobalVariables.Random.Next(1, short.MaxValue);
|
||||
case "Int32":
|
||||
return (TId)(object)GlobalVariables.Random.Next(1, int.MaxValue);
|
||||
case "Int64":
|
||||
return (TId)(object)(GlobalVariables.Random.NextLong(1, long.MaxValue));
|
||||
case "String":
|
||||
return (TId)(object)Guid.NewGuid().ToString();
|
||||
default:
|
||||
throw new NotSupportedException($"{idTypeName} is not supported.");
|
||||
}
|
||||
return IdGenerator.GetId<TId>();
|
||||
}
|
||||
|
||||
private void InitializeFields()
|
||||
|
||||
@@ -30,37 +30,25 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="DnsClient, Version=1.0.7.0, Culture=neutral, PublicKeyToken=4574bb5573c51424, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.5\lib\net45\DnsClient.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="MongoDB.Bson, Version=2.5.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.5\lib\net45\MongoDB.Bson.dll</HintPath>
|
||||
<HintPath>..\packages\MongoDB.Bson.2.5.0\lib\net45\MongoDB.Bson.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="MongoDB.Driver, Version=2.5.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.5\lib\net45\MongoDB.Driver.dll</HintPath>
|
||||
<HintPath>..\packages\MongoDB.Driver.2.5.0\lib\net45\MongoDB.Driver.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="MongoDB.Driver.Core, Version=2.5.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.5\lib\net45\MongoDB.Driver.Core.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="MongoDbGenericRepository, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.5\lib\net45\MongoDbGenericRepository.dll</HintPath>
|
||||
<HintPath>..\packages\MongoDB.Driver.Core.2.5.0\lib\net45\MongoDB.Driver.Core.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="nunit.framework, Version=3.9.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\NUnit.3.9.0\lib\net45\nunit.framework.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Buffers, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.5\lib\net45\System.Buffers.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.5\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="CRUDObjectIdTests.cs" />
|
||||
<Compile Include="CRUDPartitionedTests.cs" />
|
||||
<Compile Include="CRUDTKeyPartitionedCollectionNameAttributeTests.cs" />
|
||||
<Compile Include="CRUDTests.cs" />
|
||||
@@ -86,5 +74,11 @@
|
||||
<ItemGroup>
|
||||
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MongoDbGenericRepository\MongoDbGenericRepository.csproj">
|
||||
<Project>{efc776c4-2af3-440c-be80-3fbe335817a5}</Project>
|
||||
<Name>MongoDbGenericRepository</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
@@ -4,7 +4,6 @@
|
||||
<package id="MongoDB.Bson" version="2.5.0" targetFramework="net461" />
|
||||
<package id="MongoDB.Driver" version="2.5.0" targetFramework="net461" />
|
||||
<package id="MongoDB.Driver.Core" version="2.5.0" targetFramework="net461" />
|
||||
<package id="MongoDbGenericRepository" version="1.3.5" targetFramework="net461" />
|
||||
<package id="NUnit" version="3.9.0" targetFramework="net461" />
|
||||
<package id="NUnit.ConsoleRunner" version="3.7.0" targetFramework="net461" />
|
||||
<package id="System.Buffers" version="4.3.0" targetFramework="net461" />
|
||||
|
||||
@@ -15,7 +15,6 @@ namespace MongoDbGenericRepository
|
||||
/// </summary>
|
||||
public abstract class BaseMongoRepository : ReadOnlyMongoRepository, IBaseMongoRepository
|
||||
{
|
||||
private static readonly Random Random = new Random();
|
||||
|
||||
/// <summary>
|
||||
/// The constructor taking a connection string and a database name.
|
||||
@@ -1022,26 +1021,6 @@ namespace MongoDbGenericRepository
|
||||
|
||||
#endregion Find And Update
|
||||
|
||||
|
||||
private TKey SetIdField<TKey>()
|
||||
{
|
||||
var idTypeName = typeof(TKey).Name;
|
||||
switch (idTypeName)
|
||||
{
|
||||
case "Guid":
|
||||
return (TKey)(object)Guid.NewGuid();
|
||||
case "Int16":
|
||||
return (TKey)(object)Random.Next(1, short.MaxValue);
|
||||
case "Int32":
|
||||
return (TKey)(object)Random.Next(1, int.MaxValue);
|
||||
case "Int64":
|
||||
return (TKey)(object)(Random.NextLong(1, long.MaxValue));
|
||||
case "String":
|
||||
return (TKey)(object)Guid.NewGuid().ToString();
|
||||
}
|
||||
throw new ArgumentException($"{idTypeName} is not a supported Id type, the Id of the document cannot be set.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the value of the document Id if it is not set already.
|
||||
/// </summary>
|
||||
@@ -1061,7 +1040,7 @@ namespace MongoDbGenericRepository
|
||||
|| (defaultTKey != null
|
||||
&& defaultTKey.Equals(document.Id)))
|
||||
{
|
||||
document.Id = SetIdField<TKey>();
|
||||
document.Id = IdGenerator.GetId<TKey>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
using MongoDB.Bson;
|
||||
using System;
|
||||
|
||||
namespace MongoDbGenericRepository.Utils
|
||||
{
|
||||
public static class IdGenerator
|
||||
{
|
||||
private static readonly Random Random = new Random();
|
||||
|
||||
public static TKey GetId<TKey>()
|
||||
{
|
||||
var idTypeName = typeof(TKey).Name;
|
||||
switch (idTypeName)
|
||||
{
|
||||
case "Guid":
|
||||
return (TKey)(object)Guid.NewGuid();
|
||||
case "Int16":
|
||||
return (TKey)(object)Random.Next(1, short.MaxValue);
|
||||
case "Int32":
|
||||
return (TKey)(object)Random.Next(1, int.MaxValue);
|
||||
case "Int64":
|
||||
return (TKey)(object)(Random.NextLong(1, long.MaxValue));
|
||||
case "String":
|
||||
return (TKey)(object)Guid.NewGuid().ToString();
|
||||
case "ObjectId":
|
||||
return (TKey)(object)ObjectId.GenerateNewId();
|
||||
}
|
||||
throw new ArgumentException($"{idTypeName} is not a supported Id type, the Id of the document cannot be set.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user