Skip to content

Commit

Permalink
Expose stream properties type to simplify overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
yevhen committed Jun 6, 2015
1 parent fbd46fd commit 8a806ab
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 61 deletions.
8 changes: 4 additions & 4 deletions Source/Example/Scenarios/S07_Custom_stream_metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void SpecifyingForExistingStream()
{"Active", new EntityProperty(true)}
};

Stream.Provision(partition, properties);
Stream.Provision(partition, StreamProperties.From(properties));

Console.WriteLine("Stream metadata specified during provisioning in partition '{0}'",
partition);
Expand All @@ -45,7 +45,7 @@ void SpecifyingDuringWritingToNewStream()
{"Active", new EntityProperty(true)}
};

var stream = new Stream(partition, properties);
var stream = new Stream(partition, StreamProperties.From(properties));
Stream.Write(stream, new[]{new EventData("42")});

Console.WriteLine("Stream metadata specified during writing to new stream in partition '{0}'",
Expand All @@ -65,7 +65,7 @@ void UpdatingForExistingStream()
{"Active", new EntityProperty(true)}
};

Stream.Provision(partition, properties);
Stream.Provision(partition, StreamProperties.From(properties));

Console.WriteLine("Stream metadata specified for stream in partition '{0}'",
partition);
Expand All @@ -74,7 +74,7 @@ void UpdatingForExistingStream()
Print(stream.Properties);

properties["Active"] = new EntityProperty(false);
Stream.SetProperties(stream, properties);
Stream.SetProperties(stream, StreamProperties.From(properties));

Console.WriteLine("Updated stream metadata in partition '{0}'", partition);

Expand Down
6 changes: 2 additions & 4 deletions Source/Example/Scenarios/S08_Concurrency_conflicts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,11 @@ void SimultaneousSettingOfStreamMetadata()
var a = Stream.Open(Partition);
var b = Stream.Open(Partition);

Stream.SetProperties(a,
new Dictionary<string, EntityProperty>{{"A", new EntityProperty("42")}});
Stream.SetProperties(a, StreamProperties.From(new {A = 42}));

try
{
Stream.SetProperties(b,
new Dictionary<string, EntityProperty> {{"A", new EntityProperty("56")}});
Stream.SetProperties(b, StreamProperties.From(new {A = 56}));
}
catch (ConcurrencyConflictException)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Streamstone.Tests/Scenarios/Provisioning_stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public async void When_provisioning_along_with_custom_properties()
{"Active", new EntityProperty(true)}
};

var stream = await Stream.ProvisionAsync(partition, properties);
var stream = await Stream.ProvisionAsync(partition, StreamProperties.From(properties));
var entity = partition.RetrieveStreamEntity();

var expectedStream = new Stream
Expand Down
10 changes: 5 additions & 5 deletions Source/Streamstone.Tests/Scenarios/Setting_stream_properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async void When_property_map_is_empty()
var properties = new Dictionary<string, EntityProperty>();

var previous = await Stream.ProvisionAsync(partition);
var current = await Stream.SetPropertiesAsync(previous, properties);
var current = await Stream.SetPropertiesAsync(previous, StreamProperties.From(properties));

Assert.That(current.ETag, Is.Not.EqualTo(previous.ETag));
StreamProperties.From(properties).ToExpectedObject().ShouldEqual(current.Properties);
Expand All @@ -41,7 +41,7 @@ public async void When_concurrency_conflict()
partition.UpdateStreamEntity();

Assert.Throws<ConcurrencyConflictException>(
async ()=> await Stream.SetPropertiesAsync(stream, new Dictionary<string, EntityProperty>()));
async ()=> await Stream.SetPropertiesAsync(stream, StreamProperties.None));
}

[Test]
Expand All @@ -53,15 +53,15 @@ public async void When_set_successfully()
{"P2", new EntityProperty("42")}
};

var stream = await Stream.ProvisionAsync(partition, properties);
var stream = await Stream.ProvisionAsync(partition, StreamProperties.From(properties));

var newProperties = new Dictionary<string, EntityProperty>
{
{"P1", new EntityProperty(56)},
{"P2", new EntityProperty("56")}
};

var newStream = await Stream.SetPropertiesAsync(stream, newProperties);
var newStream = await Stream.SetPropertiesAsync(stream, StreamProperties.From(newProperties));
StreamProperties.From(newProperties).ToExpectedObject().ShouldEqual(newStream.Properties);

var storedEntity = partition.RetrieveStreamEntity();
Expand All @@ -78,7 +78,7 @@ public void When_trying_to_set_properties_on_transient_stream()
partition.CaptureContents(contents =>
{
Assert.Throws<ArgumentException>(
async ()=> await Stream.SetPropertiesAsync(stream, new Dictionary<string, EntityProperty>()));
async ()=> await Stream.SetPropertiesAsync(stream, StreamProperties.None));

contents.AssertNothingChanged();
});
Expand Down
18 changes: 10 additions & 8 deletions Source/Streamstone.Tests/Scenarios/Writing_to_stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,16 @@ public async void When_writing_with_duplicate_event_detection_enabled()
[Test]
public async void When_writing_to_nonexisting_stream_along_with_stream_properties()
{
var properties = new Dictionary<string, EntityProperty>
var properties = new
{
{"Created", new EntityProperty(DateTimeOffset.Now)},
{"Active", new EntityProperty(true)}
Created = DateTimeOffset.Now,
Active = true
};


var stream = new Stream(partition, StreamProperties.From(properties));

EventData[] events = {CreateEvent("e1"), CreateEvent("e2")};
var result = await Stream.WriteAsync(new Stream(partition, properties), events);
var result = await Stream.WriteAsync(stream, events);

AssertNewStream(result, 2, properties);
AssertStreamEntity(2, properties);
Expand All @@ -203,7 +205,7 @@ public async void When_writing_to_nonexisting_stream_along_with_stream_propertie
Is.EqualTo(eventEntities.Length + eventIdEntities.Length + 1));
}

void AssertNewStream(StreamWriteResult actual, int version, Dictionary<string, EntityProperty> properties = null)
void AssertNewStream(StreamWriteResult actual, int version, object properties = null)
{
var newStream = actual.Stream;
var newStreamEntity = partition.RetrieveStreamEntity();
Expand All @@ -223,7 +225,7 @@ void AssertModifiedStream(Stream previous, StreamWriteResult actual, int version
actualStream.ShouldEqual(expectedStream.ToExpectedObject());
}

Stream CreateStream(int version, string etag, IDictionary<string, EntityProperty> properties = null)
Stream CreateStream(int version, string etag, object properties = null)
{
var props = properties != null
? StreamProperties.From(properties)
Expand All @@ -232,7 +234,7 @@ Stream CreateStream(int version, string etag, IDictionary<string, EntityProperty
return new Stream(partition, etag, version, props);
}

void AssertStreamEntity(int version = 0, Dictionary<string, EntityProperty> properties = null)
void AssertStreamEntity(int version = 0, object properties = null)
{
var newStreamEntity = partition.RetrieveStreamEntity();

Expand Down
15 changes: 6 additions & 9 deletions Source/Streamstone/Stream.Api.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Microsoft.WindowsAzure.Storage.Table;

namespace Streamstone
{
public sealed partial class Stream
Expand All @@ -14,7 +11,7 @@ public static Stream Provision(Partition partition)
return Provision(new Stream(partition));
}

public static Stream Provision(Partition partition, IDictionary<string, EntityProperty> properties)
public static Stream Provision(Partition partition, StreamProperties properties)
{
return Provision(new Stream(partition, properties));
}
Expand All @@ -29,7 +26,7 @@ public static Task<Stream> ProvisionAsync(Partition partition)
return ProvisionAsync(new Stream(partition));
}

public static Task<Stream> ProvisionAsync(Partition partition, IDictionary<string, EntityProperty> properties)
public static Task<Stream> ProvisionAsync(Partition partition, StreamProperties properties)
{
return ProvisionAsync(new Stream(partition, properties));
}
Expand All @@ -49,14 +46,14 @@ public static Task<StreamWriteResult> WriteAsync(Stream stream, EventData[] even
return new WriteOperation(stream, events, ded).ExecuteAsync();
}

public static Stream SetProperties(Stream stream, IDictionary<string, EntityProperty> properties)
public static Stream SetProperties(Stream stream, StreamProperties properties)
{
return new SetPropertiesOperation(stream, StreamProperties.From(properties)).Execute();
return new SetPropertiesOperation(stream, properties).Execute();
}

public static Task<Stream> SetPropertiesAsync(Stream stream, IDictionary<string, EntityProperty> properties)
public static Task<Stream> SetPropertiesAsync(Stream stream, StreamProperties properties)
{
return new SetPropertiesOperation(stream, StreamProperties.From(properties)).ExecuteAsync();
return new SetPropertiesOperation(stream, properties).ExecuteAsync();
}

public static Stream Open(Partition partition)
Expand Down
26 changes: 5 additions & 21 deletions Source/Streamstone/Stream.Header.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.WindowsAzure.Storage.Table;

namespace Streamstone
{
public sealed partial class Stream
{
public readonly StreamProperties Properties;
public readonly Partition Partition;
public readonly string ETag;
public readonly int Version;

readonly StreamProperties properties;

/// <summary>
/// The readonly map of additional properties which this stream has.
/// </summary>
public IEnumerable<KeyValuePair<string, EntityProperty>> Properties
{
get { return properties; }
}

/// <summary>
/// Constructs a new <see cref="Stream"/> instance which doesn't have any additional properties.
/// </summary>
Expand All @@ -41,25 +29,21 @@ public Stream(Partition partition)
/// <param name="properties">
/// The additional properties for this stream.
/// </param>
public Stream(Partition partition, IDictionary<string, EntityProperty> properties)
: this(partition, StreamProperties.From(properties))
{}

Stream(Partition partition, StreamProperties properties)
public Stream(Partition partition, StreamProperties properties)
{
Requires.NotNull(partition, "partition");
Requires.NotNull(properties, "properties");

Partition = partition;
this.properties = properties;
Properties = properties;
}

internal Stream(Partition partition, string etag, int version, StreamProperties properties)
{
Partition = partition;
ETag = etag;
Version = version;
this.properties = properties;
Properties = properties;
}

bool IsTransient
Expand All @@ -74,7 +58,7 @@ static Stream From(Partition partition, StreamEntity entity)

StreamEntity Entity()
{
return new StreamEntity(Partition, ETag, Version, properties);
return new StreamEntity(Partition, ETag, Version, Properties);
}
}
}
23 changes: 14 additions & 9 deletions Source/Streamstone/StreamProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
using System.Collections.Generic;
using System.Linq;

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;

namespace Streamstone
{
sealed class StreamProperties : PropertyMap
public sealed class StreamProperties : PropertyMap
{
internal static readonly StreamProperties None = new StreamProperties();
public static readonly StreamProperties None = new StreamProperties();

StreamProperties()
{}
Expand All @@ -18,22 +17,28 @@ sealed class StreamProperties : PropertyMap
: base(properties)
{}

internal static StreamProperties ReadEntity(IDictionary<string, EntityProperty> properties)
public static StreamProperties ReadEntity(IDictionary<string, EntityProperty> properties)
{
Requires.NotNull(properties, "properties");
return Build(properties);
}

internal static StreamProperties From(ITableEntity entity)
public static StreamProperties From(IDictionary<string, EntityProperty> properties)
{
Requires.NotNull(entity, "entity");
return Build(ToDictionary(entity));
Requires.NotNull(properties, "properties");
return Build(Clone(properties));
}

internal static StreamProperties From(IDictionary<string, EntityProperty> properties)
public static StreamProperties From(object properties)
{
Requires.NotNull(properties, "properties");
return Build(Clone(properties));
return Build(ToDictionary(properties));
}

public static StreamProperties From(ITableEntity entity)
{
Requires.NotNull(entity, "entity");
return Build(ToDictionary(entity));
}

static StreamProperties Build(IEnumerable<KeyValuePair<string, EntityProperty>> properties)
Expand Down

0 comments on commit 8a806ab

Please sign in to comment.