Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement string enum storage via attributes #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
58 changes: 58 additions & 0 deletions CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS025StoreEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Contracts;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Models;
using Xunit.DependencyInjection;

namespace CoreHelpers.WindowsAzure.Storage.Table.Tests
{
[Startup(typeof(Startup))]
[Collection("Sequential")]
public class ITS025StoreEnum
{
private readonly IStorageContext _rootContext;

public ITS025StoreEnum(IStorageContext context)
{
_rootContext = context;
}

[Fact]
public async Task VerifyAttributeMapper()
{
using (var storageContext = _rootContext.CreateChildContext())
{
// set the tablename context
storageContext.SetTableContext();

// create a new user
var user = new UserModel4() { FirstName = "Egon", LastName = "Mueller", Contact = "[email protected]", UserType = UserTypeEnum.Pro };

// ensure we are using the attributes
storageContext.AddAttributeMapper();

// ensure the table exists
await storageContext.CreateTableAsync<UserModel4>();

// inser the model
await storageContext.MergeOrInsertAsync<UserModel4>(user);

// query all
var result = await storageContext.QueryAsync<UserModel4>();
Assert.Single(result);
Assert.Equal("Egon", result.First().FirstName);
Assert.Equal("Mueller", result.First().LastName);
Assert.Equal("[email protected]", result.First().Contact);
Assert.Equal(UserTypeEnum.Pro, result.First().UserType);

// Clean up
await storageContext.DeleteAsync<UserModel4>(result);
result = await storageContext.QueryAsync<UserModel4>();
Assert.NotNull(result);
Assert.Empty(result);

await storageContext.DropTableAsync<UserModel4>();
}
}
}
}
29 changes: 29 additions & 0 deletions CoreHelpers.WindowsAzure.Storage.Table.Tests/Models/UserModel4.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using CoreHelpers.WindowsAzure.Storage.Table.Attributes;

namespace CoreHelpers.WindowsAzure.Storage.Table.Tests.Models
{

public enum UserTypeEnum
{
Free,
Pro
}

[Storable()]
public class UserModel4
{
[PartitionKey]
public string P { get; set; } = "Partition01";

[RowKey]
public string Contact { get; set; } = String.Empty;

public string FirstName { get; set; } = String.Empty;
public string LastName { get; set; } = String.Empty;

[StoreEnumAsString]
public UserTypeEnum UserType { get; set; }

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

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

public StoreEnumAsStringAttribute()
{}

public StoreEnumAsStringAttribute(Type objectType)
{
ObjectType = objectType;
}

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 = (element as Enum)?.ToString("F");

// add the property
builder.AddProperty(propertyInfo.Name, stringifiedElement);
}

public void ReadProperty<T>(Azure.Data.Tables.TableEntity dataObject, PropertyInfo propertyInfo, T obj)
{
// check if we have the property in our entity othetwise move forward
if (!dataObject.ContainsKey(propertyInfo.Name))
return;

// get the string value
var stringValue = Convert.ToString(dataObject[propertyInfo.Name]);

// prepare the value
var resultValue = Enum.Parse(propertyInfo.PropertyType, stringValue);


// set the value
propertyInfo.SetValue(obj, resultValue);
}
}
}
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,29 @@ public class VDictionaryModel
}
```

## Store Enum as String Attribute
The store enum as string attribute allows to store enum values as string in Azure Table Store with the following code:

```csharp
[Storable(Tablename: "EnumModel")]
public class EnumtModel
{

public enum TestEnum
{
First,
Second
}

[PartitionKey]
[RowKey]
public string UUID { get; set; }

[StoreEnumAsString]
public TestEnum Data { get; set; };
}
```

## Store as JSON Object Attribute
The store as JSON attribute allows to store refenrenced objects as json payload for a specific property

Expand Down
Loading