Skip to content

Commit

Permalink
MemoryMessageBodySerializerFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
ReubenBond committed Feb 24, 2017
1 parent 09e7925 commit 5a9d837
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/OrleansProviders/OrleansProviders.csproj
Expand Up @@ -83,6 +83,7 @@
<Compile Include="Streams\Memory\MemoryAdapterReceiver.cs" />
<Compile Include="Streams\Memory\MemoryBatchContainer.cs" />
<Compile Include="Streams\Memory\MemoryMessageBody.cs" />
<Compile Include="Streams\Memory\MemoryMessageBodySerializerFactory.cs" />
<Compile Include="Streams\Memory\MemoryMessageData.cs" />
<Compile Include="Streams\Memory\MemoryPooledCache.cs" />
<Compile Include="Streams\Memory\MemoryStreamProvider.cs" />
Expand Down
8 changes: 1 addition & 7 deletions src/OrleansProviders/Streams/Memory/MemoryAdapterFactory.cs
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Orleans.Providers.Streams.Common;
using Orleans.Runtime;
using Orleans.Streams;
Expand All @@ -19,10 +18,6 @@ namespace Orleans.Providers
public class MemoryAdapterFactory<TSerializer> : IQueueAdapterFactory, IQueueAdapter, IQueueAdapterCache
where TSerializer : class, IMemoryMessageBodySerializer
{
private static readonly Lazy<ObjectFactory> ObjectFactory = new Lazy<ObjectFactory>(
() => ActivatorUtilities.CreateFactory(
typeof(TSerializer),
null));

private TSerializer serializer;
private IStreamQueueMapper streamQueueMapper;
Expand Down Expand Up @@ -78,8 +73,7 @@ public void Init(IProviderConfiguration providerConfig, string name, Logger log,
// 10 meg buffer pool. 10 1 meg blocks
bufferPool = new FixedSizeObjectPool<FixedSizeBuffer>(adapterConfig.CacheSizeMb, () => new FixedSizeBuffer(1 << 20));

this.serializer = this.serviceProvider.GetService<TSerializer>() ??
(TSerializer)ObjectFactory.Value(this.serviceProvider, null);
this.serializer = MemoryMessageBodySerializerFactory<TSerializer>.GetOrCreateSerializer(svcProvider);
}

/// <summary>
Expand Down
9 changes: 1 addition & 8 deletions src/OrleansProviders/Streams/Memory/MemoryBatchContainer.cs
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Orleans.Providers.Streams.Common;
using Orleans.Runtime;
using Orleans.Serialization;
Expand All @@ -13,11 +12,6 @@ namespace Orleans.Providers
internal class MemoryBatchContainer<TSerializer> : IBatchContainer, IOnDeserialized
where TSerializer : class, IMemoryMessageBodySerializer
{
private static readonly Lazy<ObjectFactory> ObjectFactory = new Lazy<ObjectFactory>(
() => ActivatorUtilities.CreateFactory(
typeof(TSerializer),
null));

[NonSerialized]
private TSerializer serializer;

Expand Down Expand Up @@ -67,8 +61,7 @@ public bool ShouldDeliver(IStreamIdentity stream, object filterData, StreamFilte

void IOnDeserialized.OnDeserialized(ISerializerContext context)
{
this.serializer = context.ServiceProvider.GetService<TSerializer>() ??
(TSerializer) ObjectFactory.Value(context.ServiceProvider, null);
this.serializer = MemoryMessageBodySerializerFactory<TSerializer>.GetOrCreateSerializer(context.ServiceProvider);
}
}
}
@@ -0,0 +1,20 @@
using System;
using Microsoft.Extensions.DependencyInjection;

namespace Orleans.Providers
{
internal static class MemoryMessageBodySerializerFactory<TSerializer>
where TSerializer : class, IMemoryMessageBodySerializer
{
private static readonly Lazy<ObjectFactory> ObjectFactory = new Lazy<ObjectFactory>(
() => ActivatorUtilities.CreateFactory(
typeof(TSerializer),
Type.EmptyTypes));

public static TSerializer GetOrCreateSerializer(IServiceProvider serviceProvider)
{
return serviceProvider.GetService<TSerializer>() ??
(TSerializer) ObjectFactory.Value(serviceProvider, null);
}
}
}

0 comments on commit 5a9d837

Please sign in to comment.