Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wegel committed Sep 20, 2013
1 parent 4c13b0e commit 0d0c97b
Show file tree
Hide file tree
Showing 16 changed files with 173 additions and 167 deletions.
29 changes: 18 additions & 11 deletions FluentProtobufNet.Tests/DevelopmentTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using FluentProtobufNet.Mapping;
using NUnit.Framework;
using System.Linq;
using ProtoBuf.Meta;
Expand All @@ -8,37 +8,44 @@ namespace FluentProtobufNet.Tests
[TestFixture]
public class DevelopmentTests
{
[Test]
public Configuration CanBuildConfiguration()
private Configuration _config;

[SetUp]
public void Setup()
{
return Fluently.Configure()
.Mappings(m =>
_config = Fluently.Configure()
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<CategoryMap>())
.BuildConfiguration();
}

[Test]
public void CorrectlyMapsSingleLevelSubclasses()
public void CanBuildConfiguration()
{
var config = CanBuildConfiguration();
Assert.IsNotNull(_config.RuntimeTypeModel);
Assert.Greater(_config.RuntimeTypeModel.GetTypes().Cast<object>().Count(), 0);
}

var types = config.RuntimeTypeModel.GetTypes().Cast<MetaType>();
[Test]
public void CorrectlyMapsSingleLevelSubclasses()
{
var types = _config.RuntimeTypeModel.GetTypes().Cast<MetaType>();
var category =
types.SingleOrDefault(t => t.Type == typeof (Category));

Assert.IsNotNull(category);
Assert.IsTrue(category.HasSubtypes);
Assert.IsTrue(category.GetSubtypes()[0].DerivedType.Type == typeof(CategoryWithDescription));
}

[Test]
public void CorrectlyMapsUpToThirdLevelSubclass()
{
var config = CanBuildConfiguration();

var types = config.RuntimeTypeModel.GetTypes().Cast<MetaType>();
var types = _config.RuntimeTypeModel.GetTypes().Cast<MetaType>();
var categoryWithDescription =
types.SingleOrDefault(t => t.Type == typeof(CategoryWithDescription));

Assert.IsNotNull(categoryWithDescription);
Assert.IsTrue(categoryWithDescription.HasSubtypes);
Assert.IsTrue(categoryWithDescription.GetSubtypes()[0].DerivedType.Type == typeof(CategoryThirdLevel));
}
Expand Down
22 changes: 5 additions & 17 deletions FluentProtobufNet/AssemblyTypeSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ namespace FluentProtobufNet
{
public class AssemblyTypeSource : ITypeSource
{
readonly Assembly source;
readonly Assembly _source;

public AssemblyTypeSource(Assembly source)
{
if (source == null) throw new ArgumentNullException("source");

this.source = source;
_source = source;
}

#region ITypeSource Members

public IEnumerable<Type> GetTypes()
{
return source.GetTypes().OrderBy(x => x.FullName);
return _source.GetTypes().OrderBy(x => x.FullName);
}

public void LogSource(IDiagnosticLogger logger)
Expand All @@ -32,14 +32,14 @@ public void LogSource(IDiagnosticLogger logger)

public string GetIdentifier()
{
return source.GetName().FullName;
return _source.GetName().FullName;
}

#endregion

public override int GetHashCode()
{
return source.GetHashCode();
return _source.GetHashCode();
}
}

Expand All @@ -49,16 +49,4 @@ public interface ITypeSource
//void LogSource(IDiagnosticLogger logger);
string GetIdentifier();
}

public interface IDiagnosticLogger
{
void Flush();
void FluentMappingDiscovered(Type type);
void ConventionDiscovered(Type type);
void LoadedFluentMappingsFromSource(ITypeSource source);
void LoadedConventionsFromSource(ITypeSource source);
void AutomappingSkippedType(Type type, string reason);
void AutomappingCandidateTypes(IEnumerable<Type> types);
void BeginAutomappingType(Type type);
}
}
12 changes: 12 additions & 0 deletions FluentProtobufNet/Exceptions/FieldIdAlreadyUsedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using ProtoBuf.Meta;

namespace FluentProtobufNet.Exceptions
{
public class FieldIdAlreadyUsedException : Exception
{
public FieldIdAlreadyUsedException(int fieldId, MetaType usedBy): base("The field ID " + fieldId + " has already been used by " + usedBy.Name)
{
}
}
}
18 changes: 18 additions & 0 deletions FluentProtobufNet/Exceptions/InstantiationException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace FluentProtobufNet
{
public class InstantiationException : Exception
{
public string Message { get; set; }
public Exception Exception { get; set; }
public Type Type { get; set; }

public InstantiationException(string message, Exception exception, Type type)
{
Message = message;
Exception = exception;
Type = type;
}
}
}
14 changes: 14 additions & 0 deletions FluentProtobufNet/Exceptions/MissingConstructorException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace FluentProtobufNet.Exceptions
{
public class MissingConstructorException : Exception
{
public Type Type { get; set; }

public MissingConstructorException(Type type)
{
Type = type;
}
}
}
10 changes: 4 additions & 6 deletions FluentProtobufNet/FluentConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ namespace FluentProtobufNet
public class FluentConfiguration
{
private readonly Configuration _cfg;
private IDiagnosticLogger _logger;
readonly List<Action<MappingConfiguration>> mappingsBuilders = new List<Action<MappingConfiguration>>();
private bool _mappingsSet;
private readonly IDiagnosticLogger _logger;
readonly List<Action<MappingConfiguration>> _mappingsBuilders = new List<Action<MappingConfiguration>>();

internal FluentConfiguration()
: this(new Configuration())
Expand All @@ -22,8 +21,7 @@ public FluentConfiguration(Configuration cfg)

public FluentConfiguration Mappings(Action<MappingConfiguration> mappings)
{
mappingsBuilders.Add(mappings);
_mappingsSet = true;
_mappingsBuilders.Add(mappings);
return this;
}

Expand All @@ -32,7 +30,7 @@ public Configuration BuildConfiguration()
{
var mappingCfg = new MappingConfiguration(_logger);

foreach (var builder in mappingsBuilders)
foreach (var builder in _mappingsBuilders)
builder(mappingCfg);

mappingCfg.Apply(Configuration);
Expand Down
14 changes: 9 additions & 5 deletions FluentProtobufNet/FluentProtobufNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,26 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyTypeSource.cs" />
<Compile Include="ClassMap.cs" />
<Compile Include="Exceptions\InstantiationException.cs" />
<Compile Include="Exceptions\FieldIdAlreadyUsedException.cs" />
<Compile Include="IDiagnosticLogger.cs" />
<Compile Include="Mapping\IMappingProvider.cs" />
<Compile Include="Mapping\ClassMap.cs" />
<Compile Include="Configuration.cs" />
<Compile Include="FluentConfiguration.cs" />
<Compile Include="Fluently.cs" />
<Compile Include="FluentMappingsContainer.cs" />
<Compile Include="Helpers\DummyPropertyInfo.cs" />
<Compile Include="Helpers\LinqExtensions.cs" />
<Compile Include="Helpers\Extensions.cs" />
<Compile Include="Helpers\ReflectionExtensions.cs" />
<Compile Include="MappingConfiguration.cs" />
<Compile Include="Mapping\ManyToOnePart.cs" />
<Compile Include="Mapping\Member.cs" />
<Compile Include="Mapping\PropertyPart.cs" />
<Compile Include="Exceptions\MissingConstructorException.cs" />
<Compile Include="NullDiagnosticsLogger.cs" />
<Compile Include="PersistenceModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SubclassMap.cs" />
<Compile Include="Mapping\SubclassMap.cs" />
<Compile Include="Helpers\ReflectHelper.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using FluentProtobufNet.Exceptions;

namespace FluentProtobufNet.Helpers
{
Expand Down
34 changes: 34 additions & 0 deletions FluentProtobufNet/Helpers/ReflectHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Reflection;

namespace FluentProtobufNet.Helpers
{
public class ReflectHelper
{
public const BindingFlags AnyVisibilityInstance = BindingFlags.Instance | BindingFlags.Public |
BindingFlags.NonPublic;
private static readonly Type[] NoClasses = Type.EmptyTypes;

public static ConstructorInfo GetDefaultConstructor(Type type)
{
if (IsAbstractClass(type))
return null;

try
{
ConstructorInfo constructor =
type.GetConstructor(AnyVisibilityInstance, null, CallingConventions.HasThis, NoClasses, null);
return constructor;
}
catch (Exception e)
{
throw new InstantiationException("A default (no-arg) constructor could not be found for: ", e, type);
}
}

public static bool IsAbstractClass(System.Type type)
{
return (type.IsAbstract || type.IsInterface);
}
}
}
17 changes: 17 additions & 0 deletions FluentProtobufNet/IDiagnosticLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;

namespace FluentProtobufNet
{
public interface IDiagnosticLogger
{
void Flush();
void FluentMappingDiscovered(Type type);
void ConventionDiscovered(Type type);
void LoadedFluentMappingsFromSource(ITypeSource source);
void LoadedConventionsFromSource(ITypeSource source);
void AutomappingSkippedType(Type type, string reason);
void AutomappingCandidateTypes(IEnumerable<Type> types);
void BeginAutomappingType(Type type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
using System.Linq;
using System.Linq.Expressions;
using FluentProtobufNet.Helpers;
using FluentProtobufNet.Mapping;
using ProtoBuf.Meta;

namespace FluentProtobufNet
namespace FluentProtobufNet.Mapping
{

public class ClassMap<T> : IMappingProvider
{
public IList<NameAndFieldNumber> Fields { get; set; }
public IList<PropertyMapping> Fields { get; set; }

public ClassMap()
{
Fields = new List<NameAndFieldNumber>();
Fields = new List<PropertyMapping>();
}


Expand All @@ -24,16 +23,16 @@ internal Type EntityType
get { return typeof(T); }
}

public NameAndFieldNumber Map(Expression<Func<T, object>> memberExpression, int fieldNumber)
public PropertyMapping Map(Expression<Func<T, object>> memberExpression, int fieldNumber)
{
return Map(memberExpression.ToMember(), fieldNumber);
}

NameAndFieldNumber Map(Member member, int fieldNumber)
PropertyMapping Map(Member member, int fieldNumber)
{
///­OnMemberMapped(member);
//­OnMemberMapped(member);

var field = new NameAndFieldNumber
var field = new PropertyMapping
{
Member = member,
FieldNumber = fieldNumber,
Expand All @@ -46,21 +45,21 @@ NameAndFieldNumber Map(Member member, int fieldNumber)
}


public NameAndFieldNumber References<TOther>(Expression<Func<T, TOther>> memberExpression, int fieldNumber)
public PropertyMapping References<TOther>(Expression<Func<T, TOther>> memberExpression, int fieldNumber)
{
return References<TOther>(memberExpression.ToMember(), fieldNumber);
}

public NameAndFieldNumber References<TOther>(Expression<Func<T, object>> memberExpression, int fieldNumber)
public PropertyMapping References<TOther>(Expression<Func<T, object>> memberExpression, int fieldNumber)
{
return References<TOther>(memberExpression.ToMember(), fieldNumber);
}

NameAndFieldNumber References<TOther>(Member member, int fieldNumber)
PropertyMapping References<TOther>(Member member, int fieldNumber)
{
//OnMemberMapped(member);

var field = new NameAndFieldNumber
var field = new PropertyMapping
{
Member = member,
FieldNumber = fieldNumber,
Expand Down Expand Up @@ -90,7 +89,7 @@ public virtual bool CanBeResolvedUsing(RuntimeTypeModel protobufModel)
}
}

public class NameAndFieldNumber
public class PropertyMapping
{
public Member Member { get; set; }
public int FieldNumber { get; set; }
Expand Down
13 changes: 13 additions & 0 deletions FluentProtobufNet/Mapping/IMappingProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using ProtoBuf.Meta;

namespace FluentProtobufNet
{
public interface IMappingProvider
{
RuntimeTypeModel GetRuntimeTypeModel(RuntimeTypeModel protobufModel);
// HACK: In place just to keep compatibility until verdict is made
//HibernateMapping GetHibernateMapping();
//IEnumerable<Member> GetIgnoredProperties();
bool CanBeResolvedUsing(RuntimeTypeModel protobufModel);
}
}
Loading

0 comments on commit 0d0c97b

Please sign in to comment.