Skip to content

Commit

Permalink
Merge pull request #789 from oculosdev/issue/779
Browse files Browse the repository at this point in the history
Refactored Bifrost.MongoDb for newest MongoDb driver.
  • Loading branch information
einari authored Mar 1, 2017
2 parents 2d97613 + a7bea14 commit f26c593
Show file tree
Hide file tree
Showing 17 changed files with 508 additions and 476 deletions.
3 changes: 2 additions & 1 deletion Source/Bifrost.MongoDb/Bifrost.MongoDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{63BBC1F3-E5E0-4902-BADF-1589F91C0BEA}</ProjectGuid>
<ProjectGuid>{6AA43B6E-8B56-4B00-AB6E-0F711C432BE8}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Bifrost.MongoDB</RootNamespace>
Expand Down Expand Up @@ -79,6 +79,7 @@
<ItemGroup>
<None Include="Bifrost.MongoDB.nuspec" />
<None Include="packages.config" />
<None Include="project.json" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
using Bifrost.Concepts;
using MongoDB.Bson.Serialization;

namespace Bifrost.MongoDB.Concepts
namespace Bifrost.MongoDb.Concepts
{
public class ConceptSerializationProvider : IBsonSerializationProvider
{
public IBsonSerializer GetSerializer(Type type)
{
if (type.IsConcept())
return new ConceptSerializer();
return new ConceptSerializer(type);

return null;
}
Expand Down
99 changes: 56 additions & 43 deletions Source/Bifrost.MongoDb/Concepts/ConceptSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,82 +10,95 @@
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Options;

namespace Bifrost.MongoDB.Concepts
namespace Bifrost.MongoDb.Concepts
{
public class ConceptSerializer : IBsonSerializer
{
public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
private Type _valueType;
public Type ValueType
{
get
{
return _valueType;
}
}

public ConceptSerializer(Type conceptType)
{
if (!conceptType.IsConcept())
throw new ArgumentException("Type is not a concept.", nameof(conceptType));

_valueType = conceptType;
}

public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var bsonReader = context.Reader;
var actualType = args.NominalType;

object value = null;

var valueType = actualType.GetConceptValueType();
if (valueType == typeof(Guid)) {
if (valueType == typeof(Guid))
{
var binaryData = bsonReader.ReadBinaryData();
value = binaryData.ToGuid();
} else if (valueType == typeof(double))
value = bsonReader.ReadDouble ();
}
else if (valueType == typeof(double))
value = bsonReader.ReadDouble();
else if (valueType == typeof(float))
value = (float)bsonReader.ReadDouble ();
value = (float)bsonReader.ReadDouble();
else if (valueType == typeof(Int32))
value = bsonReader.ReadInt32 ();
value = bsonReader.ReadInt32();
else if (valueType == typeof(Int64))
value = bsonReader.ReadInt64 ();
value = bsonReader.ReadInt64();
else if (valueType == typeof(bool))
value = bsonReader.ReadBoolean ();
value = bsonReader.ReadBoolean();
else if (valueType == typeof(string))
value = bsonReader.ReadString ();
value = bsonReader.ReadString();
else if (valueType == typeof(decimal))
value = decimal.Parse (bsonReader.ReadString ());
value = decimal.Parse(bsonReader.ReadString());

var concept = ConceptFactory.CreateConceptInstance(actualType, value);
return concept;
}

public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
{
return null;
}

public bool GetDocumentId(object document, out object id, out Type idNominalType, out IIdGenerator idGenerator)
{
id = null;
idGenerator = null;
idNominalType = null;
return false;
}

public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
{
var underlyingValue = value.GetType().GetProperty("Value").GetValue(value, null);
var underlyingValue = value?.GetType().GetProperty("Value").GetValue(value, null);
var nominalType = args.NominalType;
var underlyingValueType = nominalType.GetConceptValueType();
if (underlyingValueType == typeof(Guid)) {
var guid = (Guid)underlyingValue;
var guidAsBytes = guid.ToByteArray ();
bsonWriter.WriteBinaryData (guidAsBytes, BsonBinarySubType.UuidLegacy, GuidRepresentation.CSharpLegacy);
} else if (underlyingValueType == typeof(double))
bsonWriter.WriteDouble ((double)underlyingValue);

var bsonWriter = context.Writer;

if (underlyingValueType == typeof(Guid))
{
var guid = (Guid) (underlyingValue ?? default(Guid));
var guidAsBytes = guid.ToByteArray();
bsonWriter.WriteBinaryData(new BsonBinaryData(guidAsBytes, BsonBinarySubType.UuidLegacy, GuidRepresentation.CSharpLegacy));
}
else if (underlyingValueType == typeof(double))
bsonWriter.WriteDouble((double) (underlyingValue ?? default(double)));
else if (underlyingValueType == typeof(float))
bsonWriter.WriteDouble ((double)underlyingValue);
bsonWriter.WriteDouble((double) (underlyingValue ?? default(double)));
else if (underlyingValueType == typeof(Int32))
bsonWriter.WriteInt32 ((Int32)underlyingValue);
bsonWriter.WriteInt32((Int32) (underlyingValue ?? default(Int32)));
else if (underlyingValueType == typeof(Int64))
bsonWriter.WriteInt64 ((Int64)underlyingValue);
bsonWriter.WriteInt64((Int64) (underlyingValue ?? default(Int64)));
else if (underlyingValueType == typeof(bool))
bsonWriter.WriteBoolean ((bool)underlyingValue);
bsonWriter.WriteBoolean((bool) (underlyingValue ?? default(bool)));
else if (underlyingValueType == typeof(string))
bsonWriter.WriteString ((string)(underlyingValue ?? string.Empty));
bsonWriter.WriteString((string) (underlyingValue ?? string.Empty));
else if (underlyingValueType == typeof(decimal))
bsonWriter.WriteString (underlyingValue.ToString());
bsonWriter.WriteString(underlyingValue?.ToString() ?? default(decimal).ToString());
}

public void SetDocumentId(object document, object id)
{
}

public IBsonSerializationOptions GetDefaultSerializationOptions()
{
var options = new DocumentSerializationOptions();
return options;
}

}
}
16 changes: 14 additions & 2 deletions Source/Bifrost.MongoDb/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/
using System;
using Bifrost.MongoDB;
using Bifrost.MongoDB.Events;
using Bifrost.MongoDb;
using Bifrost.MongoDb.Events;

namespace Bifrost.Configuration
{
Expand All @@ -26,6 +26,12 @@ public static EventStorageConfiguration WithUrl(this EventStorageConfiguration c
return configuration;
}

public static EventStorageConfiguration WithSSL(this EventStorageConfiguration configuration)
{
configuration.UseSSL = true;
return configuration;
}


public static EventStorageConfiguration WithDefaultDatabase(this EventStorageConfiguration configuration, string defaultDatabase)
{
Expand All @@ -52,6 +58,12 @@ public static EntityContextConfiguration WithUrl(this EntityContextConfiguration
return configuration;
}

public static EntityContextConfiguration WithSSL(this EntityContextConfiguration configuration)
{
configuration.UseSSL = true;
return configuration;
}


public static EntityContextConfiguration WithDefaultDatabase(this EntityContextConfiguration configuration, string defaultDatabase)
{
Expand Down
164 changes: 85 additions & 79 deletions Source/Bifrost.MongoDb/EntityContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,85 +7,91 @@
using Bifrost.Concepts;
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Driver.Builders;
using System.Reflection;

namespace Bifrost.MongoDB
namespace Bifrost.MongoDb
{
public class EntityContext<T> : IEntityContext<T>
{
EntityContextConnection _connection;
string _collectionName;
MongoCollection<T> _collection;

public EntityContext(EntityContextConnection connection)
{
_connection = connection;
_collectionName = typeof(T).Name;
if( !_connection.Database.CollectionExists(_collectionName) )
_connection.Database.CreateCollection(_collectionName);

_collection = _connection.Database.GetCollection<T>(_collectionName);
}


public IQueryable<T> Entities
{
get { return _collection.FindAll().AsQueryable(); }
}

public void Attach(T entity)
{
}

public void Insert(T entity)
{
_collection.Insert(entity);
}

public void Update(T entity)
{
Save(entity);
}

public void Delete(T entity)
{
}

public void Save(T entity)
{
_collection.Save(entity);
}

public void Commit()
{
}

public void Dispose()
{
}


public T GetById<TProperty>(TProperty id)
{
var objectId = GetObjectId(id);
return _collection.FindOneById(objectId);
}

BsonValue GetObjectId<TProperty>(TProperty id)
{
object idValue = id;

if (id.IsConcept()) idValue = id.GetConceptValue();

var idAsValue = BsonValue.Create(idValue);
return idAsValue;
}


public void DeleteById<TProperty>(TProperty id)
{
var objectId = GetObjectId(id);
_collection.Remove(Query.EQ("_id", objectId));
}
}
public class EntityContext<T> : IEntityContext<T>
{
EntityContextConnection _connection;
string _collectionName;
IMongoCollection<T> _collection;

public EntityContext(EntityContextConnection connection)
{
_connection = connection;
_collectionName = typeof(T).Name;

_collection = _connection.Database.GetCollection<T>(_collectionName);
}


public IQueryable<T> Entities
{
get { return _collection.AsQueryable<T>(); }
}

public void Attach(T entity)
{
}

public void Insert(T entity)
{
_collection.InsertOne(entity);
}

public void Update(T entity)
{
Save(entity);
}

public void Delete(T entity)
{
}

public void Save(T entity)
{
var idProperty = GetIdProperty(entity);

var filter = Builders<T>.Filter.Eq("_id", idProperty.GetValue(entity));
_collection.ReplaceOne(filter, entity, new UpdateOptions() { IsUpsert = true });
}

public void Commit()
{
}

public void Dispose()
{
}

PropertyInfo GetIdProperty(T entity)
{
return typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.Name.ToLowerInvariant() == "id").First();
}


public T GetById<TProperty>(TProperty id)
{
var objectId = GetObjectId(id);
return _collection.Find(Builders<T>.Filter.Eq("_id", objectId)).FirstOrDefault();
}

BsonValue GetObjectId<TProperty>(TProperty id)
{
object idValue = id;

if (id.IsConcept()) idValue = id.GetConceptValue();

var idAsValue = BsonValue.Create(idValue);
return idAsValue;
}


public void DeleteById<TProperty>(TProperty id)
{
var objectId = GetObjectId(id);
_collection.DeleteOne(Builders<T>.Filter.Eq("id", objectId));
}
}
}
3 changes: 2 additions & 1 deletion Source/Bifrost.MongoDb/EntityContextConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
using Bifrost.Configuration;
using Bifrost.Entities;

namespace Bifrost.MongoDB
namespace Bifrost.MongoDb
{
public class EntityContextConfiguration : IEntityContextConfiguration
{
public string Url { get; set; }
public bool UseSSL { get; set; }
public string DefaultDatabase { get; set; }

public Type EntityContextType { get { return typeof(EntityContext<>); } }
Expand Down
Loading

0 comments on commit f26c593

Please sign in to comment.