Skip to content

Commit

Permalink
Added entity writer based on new azure sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
dei79 committed Aug 29, 2022
1 parent ae041f0 commit 7ff9466
Show file tree
Hide file tree
Showing 11 changed files with 635 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ public ITS001StoreWithStaticEntityMapper(ITestEnvironment env)
}

[Fact]
public async Task VerifyTableExists()
{
using (var scp = new StorageContext(env.ConnectionString))
{
// set the tablename context
scp.SetTableContext();

// configure the entity mapper
scp.AddEntityMapper(typeof(UserModel), new DynamicTableEntityMapper() { TableName = "UserProfiles", PartitionKeyFormat = "Contact", RowKeyFormat = "Contact" });

Assert.False(await scp.ExistsTableAsync<UserModel>());

await scp.CreateTableAsync<UserModel>();
Assert.True(await scp.ExistsTableAsync<UserModel>());
}
}

[Fact]
public async Task VerifyStaticEntityMapperOperations()
{
using (var scp = new StorageContext(env.ConnectionString))
Expand All @@ -41,9 +59,13 @@ public async Task VerifyStaticEntityMapperOperations()
{
// ensure the table exists
await sc.CreateTableAsync<UserModel>();

// inser the model
await sc.MergeOrInsertAsync<UserModel>(user);

// ensure we are empty
var resultEmpty = await scp.QueryAsync<UserModel>();
Assert.Empty(resultEmpty);

// inser the model
await sc.MergeOrInsertAsync<UserModel>(user);
}

// verify if the model was created
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using CoreHelpers.WindowsAzure.Storage.Table.Serialization;
using System.Reflection;

namespace CoreHelpers.WindowsAzure.Storage.Table.Attributes
{
public interface IVirtualTypeAttribute
{
void WriteProperty<T>(PropertyInfo propertyInfo, T obj, TableEntityBuilder builder);
}
}

Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System;
using System.Collections;
using System.Reflection;
using CoreHelpers.WindowsAzure.Storage.Table.Serialization;
using Microsoft.WindowsAzure.Storage.Table;
using Newtonsoft.Json;

namespace CoreHelpers.WindowsAzure.Storage.Table.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
public class StoreAsJsonObjectAttribute : StoreAsAttribute
{
public class StoreAsJsonObjectAttribute : StoreAsAttribute, IVirtualTypeAttribute
{
protected Type ObjectType { get; set; }

public StoreAsJsonObjectAttribute()
Expand Down Expand Up @@ -54,5 +55,19 @@ public override Object ConvertFromEntityProperty(PropertyInfo property, EntityPr
return JsonConvert.DeserializeObject(entityProperty.StringValue, property.PropertyType);
}
}
}

public void WriteProperty<T>(PropertyInfo propertyInfo, T obj, TableEntityBuilder builder)
{
// get the value
var element = propertyInfo.GetValue(obj);
if (element == null)
return;

// convert to strong
var stringifiedElement = JsonConvert.SerializeObject(element);

// add the property
builder.AddProperty(propertyInfo.Name, stringifiedElement);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
using HandlebarsDotNet;
using Microsoft.WindowsAzure.Storage.Table;
using CoreHelpers.WindowsAzure.Storage.Table.Extensions;
using CoreHelpers.WindowsAzure.Storage.Table.Serialization;

namespace CoreHelpers.WindowsAzure.Storage.Table.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
public class VirtualListAttribute : VirtualTypeAttribute
{
public class VirtualListAttribute : VirtualTypeAttribute, IVirtualTypeAttribute
{
private HandlebarsTemplate<object, string> TemplateFunction { get; set; }
private string DigitFormat { get; set; }

Expand Down Expand Up @@ -73,5 +74,28 @@ public override void ReadProperty(PropertyInfo propertyInfo, object obj, IDictio

}
}
}

public void WriteProperty<T>(PropertyInfo propertyInfo, T obj, TableEntityBuilder builder)
{
// get the value
var arrayValue = propertyInfo.GetValue(obj);

// check if enumerable
if ((arrayValue as IList) == null)
return;

// visit every element
for (int idx = 0; idx < (arrayValue as IList).Count; idx++)
{
// get the element
var element = (arrayValue as IList)[idx];

// generate the property name
var propertyName = TemplateFunction(new { index = idx.ToString(DigitFormat) });

// write the property
builder.AddProperty(propertyName, element);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using CoreHelpers.WindowsAzure.Storage.Table.Serialization;
using Microsoft.WindowsAzure.Storage.Table;

namespace CoreHelpers.WindowsAzure.Storage.Table.Attributes
Expand All @@ -9,6 +10,6 @@ public abstract class VirtualTypeAttribute : Attribute
{
public abstract void WriteProperty(PropertyInfo propertyInfo, Object obj, Dictionary<string, EntityProperty> targetList);

public abstract void ReadProperty(PropertyInfo propertyInfo, Object obj, IDictionary<string, EntityProperty> entityProperties);
public abstract void ReadProperty(PropertyInfo propertyInfo, Object obj, IDictionary<string, EntityProperty> entityProperties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,27 @@
</PropertyGroup>

<ItemGroup>
<None Include="..\LICENSE" Pack="true" PackagePath=""/>
<None Remove="Azure.Data.Tables" />
<None Remove="Serialization\" />
</ItemGroup>
<ItemGroup>
<None Include="..\LICENSE" Pack="true" PackagePath="" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
<PackageReference Include="System.Linq.Queryable" Version="4.3.0" />
<PackageReference Include="Handlebars.Net" Version="2.1.2" />
<PackageReference Include="System.Linq.Parallel" Version="4.3.0" />
<PackageReference Include="Azure.Data.Tables" Version="12.6.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="Attributes\" />
<Folder Include="Extensions\" />
<Folder Include="Delegates\" />
<Folder Include="Services\" />
<Folder Include="Internal\" />
<Folder Include="Serialization\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CoreHelpers.WindowsAzure.Storage.Table.Abstractions\CoreHelpers.WindowsAzure.Storage.Table.Abstractions.csproj" />
Expand Down
Loading

0 comments on commit 7ff9466

Please sign in to comment.