Skip to content

Commit

Permalink
update to .NET + Orleans 7.0.0 GA, update example to Orleans.Results …
Browse files Browse the repository at this point in the history
…1.0.0
  • Loading branch information
VincentH-Net committed Nov 10, 2022
1 parent a85bfe3 commit c04aa9a
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 59 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Secure, flexible tenant separation for Microsoft Orleans 7
> (install in silo client and grain implementation projects)
## Summary
[Microsoft Orleans 7](https://github.com/dotnet/orleans/releases/tag/v7.0.0-rc2) is a great technology for building distributed, cloud-native applications. It was designed to reduce the complexity of building this type of applications for C# developers.
[Microsoft Orleans 7](https://github.com/dotnet/orleans/releases/tag/v7.0.0) is a great technology for building distributed, cloud-native applications. It was designed to reduce the complexity of building this type of applications for C# developers.

However, creating multi tenant applications with Orleans out of the box requires careful design, complex coding and significant testing to prevent unintentional leakage of communication or stored data across tenants. Orleans.Multitenant adds this capability to Orleans for free, as an uncomplicated, flexible and extensible API that lets developers:

Expand Down
10 changes: 5 additions & 5 deletions src/Example/Apis/Apis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Orleans.Persistence.AzureStorage" Version="7.0.0-rc2" />
<PackageReference Include="Microsoft.Orleans.Persistence.Memory" Version="7.0.0-rc2" />
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.0.0-rc2" />
<PackageReference Include="Orleans.Multitenant" Version="1.0.0-rc2" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0-rc.2.22476.2" />
<PackageReference Include="Microsoft.Orleans.Persistence.AzureStorage" Version="7.0.0" />
<PackageReference Include="Microsoft.Orleans.Persistence.Memory" Version="7.0.0" />
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.0.0" />
<PackageReference Include="Orleans.Multitenant" Version="1.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

Expand Down
3 changes: 1 addition & 2 deletions src/Example/Apis/Foundation/ControllerBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Orleans;
using Orleans.Multitenant;
using Orleans.Multitenant;
using Orleans4Multitenant.Contracts.TenantContract;

namespace Orleans4Multitenant.Apis;
Expand Down
1 change: 0 additions & 1 deletion src/Example/Apis/Foundation/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.OpenApi.Models;
using Orleans.Configuration;
using Orleans.Hosting;
using Orleans.Multitenant;
using Orleans.Storage;
using Orleans4Multitenant.Apis;
Expand Down
22 changes: 11 additions & 11 deletions src/Example/Apis/TenantController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class TenantController : ControllerBase
const string Users = "users";
const string UserId = "users/{id}";

public TenantController(Orleans.IClusterClient orleans) : base(orleans) { }
public TenantController(IClusterClient orleans) : base(orleans) { }

/// <response code="200">The tenant has been updated</response>
[HttpPut(Tenant)]
Expand All @@ -33,7 +33,7 @@ public async Task<Tenant> Get()
public async Task<ActionResult<User>> CreateUser(User user)
{
var result = await RequestTenant.CreateUser(user);
return result.TryAsValidationErrors(ErrorCode.ValidationError, out var validationErrors)
return result.TryAsValidationErrors(ErrorNr.ValidationError, out var validationErrors)
? ValidationProblem(new ValidationProblemDetails(validationErrors))
: result switch
{
Expand All @@ -56,9 +56,9 @@ public async Task<ActionResult<IEnumerable<User>>> GetUsers()
public async Task<ActionResult<User>> GetUser(Guid id)
=> await RequestTenant.GetUser(id) switch
{
{ IsSuccess: true } r => Ok(r.Value),
{ ErrorCode: ErrorCode.UserNotFound } r => NotFound(r.ErrorsText),
{ } r => throw r.UnhandledErrorException()
{ IsSuccess: true } r => Ok(r.Value),
{ ErrorNr: ErrorNr.UserNotFound } r => NotFound(r.ErrorsText),
{ } r => throw r.UnhandledErrorException()
};

/// <param name="id">must be equal to id in <paramref name="user"/></param>
Expand All @@ -72,9 +72,9 @@ public async Task<ActionResult> UpdateUser(Guid id, User user)
=> id != user.Id ? BadRequest($"url id {id} != user id {user?.Id}") :
await RequestTenant.UpdateUser(user) switch
{
{ IsSuccess: true } r => Ok(),
{ ErrorCode: ErrorCode.UserNotFound } r => NotFound(r.ErrorsText),
{ } r => throw r.UnhandledErrorException()
{ IsSuccess: true } r => Ok(),
{ ErrorNr: ErrorNr.UserNotFound } r => NotFound(r.ErrorsText),
{ } r => throw r.UnhandledErrorException()
};

/// <response code="200">If the user has been deleted</response>
Expand All @@ -85,8 +85,8 @@ public async Task<ActionResult> UpdateUser(Guid id, User user)
public async Task<ActionResult> DeleteUser(Guid id)
=> await RequestTenant.DeleteUser(id) switch
{
{ IsSuccess: true } r => Ok(),
{ ErrorCode: ErrorCode.UserNotFound } r => NotFound(r.ErrorsText),
{ } r => throw r.UnhandledErrorException()
{ IsSuccess: true } r => Ok(),
{ ErrorNr: ErrorNr.UserNotFound } r => NotFound(r.ErrorsText),
{ } r => throw r.UnhandledErrorException()
};
}
2 changes: 1 addition & 1 deletion src/Example/Contracts/Contracts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.0.0-rc2" />
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace Orleans4Multitenant.Contracts;


[Flags]
public enum ErrorCode
public enum ErrorNr
{
UserNotFound = 1,

Expand Down
43 changes: 21 additions & 22 deletions src/Example/Contracts/Foundation/Result.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
// Version: 1.0.0-preview.2 (Using https://semver.org/)
// Updated: 2022-08-26
// Version: 1.0.0 (Using https://semver.org/)
// Updated: 2022-11-10
// See https://github.com/Applicita/Orleans.Results for updates to this file.

using System.Diagnostics.CodeAnalysis;
using Orleans;

namespace Orleans4Multitenant.Contracts;

/// <summary>
/// Result without value; use to return either <see cref="Ok"/> or <see cref="ResultBase{ErrorCode}.Error"/>(s)
/// Result without value; use to return either <see cref="Ok"/> or <see cref="ResultBase{ErrorNr}.Error"/>(s)
/// </summary>
[GenerateSerializer, Immutable]
public class Result : ResultBase<ErrorCode>
public class Result : ResultBase<ErrorNr>
{
public static Result Ok { get; } = new();

Expand All @@ -21,16 +20,16 @@ public Result(IEnumerable<Error> errors) : base(ImmutableArray.CreateRange(error
Result(Error error) : base(error) { }

public static implicit operator Result(Error error) => new(error);
public static implicit operator Result(ErrorCode code) => new(code);
public static implicit operator Result((ErrorCode code, string message) error) => new(error);
public static implicit operator Result(ErrorNr nr) => new(nr);
public static implicit operator Result((ErrorNr nr, string message) error) => new(error);
public static implicit operator Result(List<Error> errors) => new(errors);
}

/// <summary>
/// Result with value; use to return either a <typeparamref name="TValue"/> or <see cref="ResultBase{ErrorCode}.Error"/>(s)
/// Result with value; use to return either a <typeparamref name="TValue"/> or <see cref="ResultBase{ErrorNr}.Error"/>(s)
/// </summary>
[GenerateSerializer]
public class Result<TValue> : ResultBase<ErrorCode, TValue>
public class Result<TValue> : ResultBase<ErrorNr, TValue>
{
public Result(ImmutableArray<Error> errors) : base(errors) { }
public Result(IEnumerable<Error> errors) : base(ImmutableArray.CreateRange(errors)) { }
Expand All @@ -39,13 +38,13 @@ public Result(IEnumerable<Error> errors) : base(ImmutableArray.CreateRange(error

public static implicit operator Result<TValue>(TValue value) => new(value);
public static implicit operator Result<TValue>(Error error) => new(error);
public static implicit operator Result<TValue>(ErrorCode code) => new(code);
public static implicit operator Result<TValue>((ErrorCode code, string message) error) => new(error);
public static implicit operator Result<TValue>(ErrorNr nr) => new(nr);
public static implicit operator Result<TValue>((ErrorNr nr, string message) error) => new(error);
public static implicit operator Result<TValue>(List<Error> errors) => new(errors);
}

[GenerateSerializer]
public abstract class ResultBase<TErrorCode, TValue> : ResultBase<TErrorCode> where TErrorCode : Enum
public abstract class ResultBase<TErrorNr, TValue> : ResultBase<TErrorNr> where TErrorNr : Enum
{
[Id(0)] TValue? value;

Expand Down Expand Up @@ -82,7 +81,7 @@ public TValue Value
}

[GenerateSerializer]
public abstract class ResultBase<TErrorCode> where TErrorCode : Enum
public abstract class ResultBase<TErrorNr> where TErrorNr : Enum
{
public bool IsSuccess => !IsFailed;
public bool IsFailed => errors?.Length > 0;
Expand All @@ -96,9 +95,9 @@ public abstract class ResultBase<TErrorCode> where TErrorCode : Enum
public ImmutableArray<Error> Errors => errors ?? throw new InvalidOperationException("Attempt to access the errors of a success result");

/// <summary>
/// Returns the errorcode for a failed result with a single error; otherwise throws an exception
/// Returns the error nr for a failed result with a single error; otherwise throws an exception
/// </summary>
public TErrorCode ErrorCode => Errors.Single().Code;
public TErrorNr ErrorNr => Errors.Single().Nr;

/// <summary>
/// Returns all errors formatted in a single string for a failed result; throws an <see cref="InvalidOperationException"/> for a success result
Expand All @@ -111,14 +110,14 @@ public abstract class ResultBase<TErrorCode> where TErrorCode : Enum
/// <remarks>Intended for use with <see cref="Microsoft.AspNetCore.Mvc.ValidationProblemDetails"/> (in MVC controllers) or <see cref="Microsoft.AspNetCore.Http.Results.ValidationProblem"/> (in minimal api's) </remarks>
/// <param name="validationErrorFlag">The enum flag used to identify an error as a validation error</param>
/// <param name="validationErrors">If the return value is true, receives all errors in a dictionary suitable for serializing into a https://tools.ietf.org/html/rfc7807 based format; otherwise set to null</param>
/// <returns>True for a failed result that has the <paramref name="validationErrorFlag"/> set in the <typeparamref name="TErrorCode"/> for <b>all</b> errors; false otherwise</returns>
/// <returns>True for a failed result that has the <paramref name="validationErrorFlag"/> set in the <typeparamref name="TErrorNr"/> for <b>all</b> errors; false otherwise</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0001:Simplify Names", Justification = "Full name is necessary to ensure link works independently of global usings")]
public bool TryAsValidationErrors(TErrorCode validationErrorFlag, [NotNullWhen(true)] out Dictionary<string, string[]>? validationErrors)
public bool TryAsValidationErrors(TErrorNr validationErrorFlag, [NotNullWhen(true)] out Dictionary<string, string[]>? validationErrors)
{
if (IsFailed && Errors.All(error => error.Code.HasFlag(validationErrorFlag)))
if (IsFailed && Errors.All(error => error.Nr.HasFlag(validationErrorFlag)))
{
validationErrors = new(Errors
.GroupBy(error => error.Code, error => error.Message)
.GroupBy(error => error.Nr, error => error.Message)
.Select(group => new KeyValuePair<string, string[]>(group.Key.ToString(), group.ToArray())));
return true;
}
Expand All @@ -136,9 +135,9 @@ protected ResultBase() { }
public NotImplementedException UnhandledErrorException(string? message = null) => new($"{message}Unhandled error(s): " + ErrorsText);

[GenerateSerializer, Immutable]
public record Error([property: Id(0)] TErrorCode Code, [property: Id(1)] string Message = "")
public record Error([property: Id(0)] TErrorNr Nr, [property: Id(1)] string Message = "")
{
public static implicit operator Error(TErrorCode code) => new(code);
public static implicit operator Error((TErrorCode code, string message) error) => new(error.code, error.message);
public static implicit operator Error(TErrorNr nr) => new(nr);
public static implicit operator Error((TErrorNr nr, string message) error) => new(error.nr, error.message);
}
}
4 changes: 2 additions & 2 deletions src/Example/Services.Tenant/Foundation/Errors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

static class Errors
{
internal static Result.Error UserNotFound(Guid id) => new(ErrorCode.UserNotFound, $"User {id} not found");
internal static Result.Error IdIsNotEmpty(Guid id) => new(ErrorCode.IdIsNotEmpty, $"{id} is not the empty guid");
internal static Result.Error UserNotFound(Guid id) => new(ErrorNr.UserNotFound, $"User {id} not found");
internal static Result.Error IdIsNotEmpty(Guid id) => new(ErrorNr.IdIsNotEmpty, $"{id} is not the empty guid");
}
4 changes: 2 additions & 2 deletions src/Example/Services.Tenant/Services.Tenant.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.0.0-rc2" />
<PackageReference Include="Orleans.Multitenant" Version="1.0.0-rc2" />
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.0.0" />
<PackageReference Include="Orleans.Multitenant" Version="1.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/Orleans.Multitenant/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Orleans.Hosting;
using Orleans.Multitenant.Internal;
using Orleans.Providers;
using Orleans.Runtime;
Expand Down
8 changes: 4 additions & 4 deletions src/Orleans.Multitenant/Orleans.Multitenant.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PropertyGroup>
<IsPackable>true</IsPackable>
<PackageId>Orleans.Multitenant</PackageId>
<PackageVersion>1.0.0-rc2</PackageVersion>
<PackageVersion>1.0.0</PackageVersion>
<Title>Orleans Multitenant</Title>
<Description>Secure, flexible tenant separation for Microsoft Orleans 7</Description>
<Authors>VincentH.NET;Applicita</Authors>
Expand Down Expand Up @@ -41,9 +41,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Orleans.Runtime" Version="7.0.0-rc2" />
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.0.0-rc2" />
<PackageReference Include="Microsoft.Orleans.Streaming" Version="7.0.0-rc2" />
<PackageReference Include="Microsoft.Orleans.Runtime" Version="7.0.0" />
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.0.0" />
<PackageReference Include="Microsoft.Orleans.Streaming" Version="7.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/Orleans.Multitenant/Readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Secure, flexible tenant separation for [Microsoft Orleans 7](https://github.com/dotnet/orleans/releases/tag/v7.0.0-rc2)
Secure, flexible tenant separation for [Microsoft Orleans 7](https://github.com/dotnet/orleans/releases/tag/v7.0.0)

Docs: see the [repo readme](https://github.com/Applicita/Orleans.Multitenant#readme) and the inline C# documentation. All public Orleans.Multitenant API's come with full inline documentation.

[Release Notes](https://github.com/Applicita/Orleans.Multitenant/releases/tag/1-0-0-rc-2)
[Release Notes](https://github.com/Applicita/Orleans.Multitenant/releases/tag/1-0-0)
6 changes: 3 additions & 3 deletions src/Tests/Orleans.Multitenant.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0-preview-20221003-04" />
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.0.0-rc2" />
<PackageReference Include="Microsoft.Orleans.TestingHost" Version="7.0.0-rc2" />
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.0.0" />
<PackageReference Include="Microsoft.Orleans.TestingHost" Version="7.0.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down

0 comments on commit c04aa9a

Please sign in to comment.