Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,22 @@ public ObjectConverterFactory(IEnumerable<Type> entityTypes)
if (knownTypes.Count <= 1)
continue;

// Skip base types, it has KnownType attributes and is handled correctly
if (item.Key.BaseType == typeof(Entity))
continue;

converters.Add(item.Key, (MessagePackConverter)Activator.CreateInstance(typeof(ObjectConverter<>).MakeGenericType(item.Key), [knownTypes])!);
}
converters.Add(typeof(object), new ObjectConverter<object>(allTypes));

_converters = converters.ToFrozenDictionary();
}

/// <summary>
/// Generate DerivedTypeUnion required for the ObjectConverterFactory to work.
/// </summary>
/// <remarks>
/// In order to prevent Nerdabank.MessagePack from creating additional discriminators we must disable the built in inheritance support for the types.
/// </remarks>
public IEnumerable<DerivedTypeUnion> GetDerivedTypeUnions()
=> _converters.Keys.Select(DerivedTypeUnion.CreateDisabled);

MessagePackConverter? IMessagePackConverterFactory.CreateConverter(Type type, ITypeShape? shape, in ConverterContext context)
{
// Only return type shapes for specified converter, this allows nerdbank default converters to be generated for other providers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ internal MessagePackSerializer GetSerializer(Type service, IEnumerable<Type> kno
{
return _serializerCache.GetOrAdd(service, static (_, args) =>
{
var converterFactory = new ObjectConverterFactory(args.knownTypes);
return args.Item1.BaseSerializerSerializer with
{
ConverterFactories = [new ObjectConverterFactory(args.knownTypes), .. args.Item1.BaseSerializerSerializer.ConverterFactories]
ConverterFactories = [converterFactory, .. args.Item1.BaseSerializerSerializer.ConverterFactories],
DerivedTypeUnions = [..converterFactory.GetDerivedTypeUnions(), .. args.Item1.BaseSerializerSerializer.DerivedTypeUnions]
Comment thread
Daniel-Svensson marked this conversation as resolved.
};

}, (this, knownTypes));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Cities;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenRiaServices.Silverlight.Testing;
using TestDomainServices;
using TestDomainServices.NamedUpdates;

namespace OpenRiaServices.Client.Test
{
Expand Down Expand Up @@ -394,6 +392,27 @@ public void Inherit_Run_Call_Derived_Custom_Method_On_Abstract_Base_()
EnqueueTestComplete();
}

[TestMethod]
[Description("Ensure querying works for 3 levels of inheritance")]
public async Task Inherit_Queries()
{
// Inheritance is City <-- CityWithEditHistory <-- CityWithInfo
// Ensure we can methods retrieve entities at all levels of the hierarchy
Comment thread
Daniel-Svensson marked this conversation as resolved.
CityDomainContext baseContext = new CityDomainContext();
CityDomainContext middleContext = new CityDomainContext();
CityDomainContext mostDerivedContext = new CityDomainContext();

var baseResult = await baseContext.LoadAsync(baseContext.GetCitiesQuery());
var editResult = await middleContext.LoadAsync(middleContext.GetCitiesWithEditHistoryQuery());
var mostDerivedResult = await mostDerivedContext.LoadAsync(mostDerivedContext.GetCitiesWithInfoQuery());

// CityWithEditHistory is abstract so should be return exactly the same number of entities as CityWithInfo
Assert.HasCount(2, mostDerivedResult.Entities);
Assert.HasCount(2, editResult.Entities);
Assert.HasCount(2, baseResult.Entities.OfType<CityWithInfo>());
Assert.HasCount(11, baseResult.Entities);
}

#endregion //Custom methods

#region CUD
Expand Down
Loading