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

EXPOSERS: Create EFxceptions Identity Context #25

Merged
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
13 changes: 0 additions & 13 deletions STX.EFxceptions.Identity.SqlServer.Tests.Acceptance/DeleteMe.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// ----------------------------------------------------------------------------------
// Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;
using STX.EFxceptions.Abstractions.Models.Exceptions;
using STX.EFxceptions.Identity.SqlServer.Tests.Acceptance.Models.Clients;

namespace STX.EFxceptions.Identity.SqlServer.Tests.Acceptance
{
public partial class EFxceptionsIdentityContextTests
{
[Fact]
public void ShouldSaveChangesSuccessfully()
{
// given
var client = new Client
{
Id = Guid.NewGuid()
};

// when
context.Clients.Add(client);
context.SaveChanges();

// then
context.Clients.Remove(client);
context.SaveChanges();
}

[Fact]
public void ShouldThrowDuplicateKeyExceptionOnSaveChanges()
{
// given
var client = new Client
{
Id = Guid.NewGuid()
};

// when . then
Assert.Throws<DuplicateKeyException>(() =>
{
try
{
for (int i = 0; i < 2; i++)
{
context.Clients.Add(client);
context.SaveChanges();
}
}
catch (ArgumentException argumentException)
{
throw new DuplicateKeyException(argumentException.Message);
}
finally
{
context.Clients.Remove(client);
context.SaveChanges();
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// ----------------------------------------------------------------------------------
// Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace STX.EFxceptions.Identity.SqlServer.Tests.Acceptance
{
public partial class EFxceptionsIdentityContextTests
{
private readonly DbContextOptions<EFxceptionsIdentityContext<IdentityUser, IdentityRole, string>> options;
private readonly MyEFxceptionsIdentityContext<IdentityUser, IdentityRole, string> context;

public EFxceptionsIdentityContextTests()
{
options = new DbContextOptionsBuilder<EFxceptionsIdentityContext<IdentityUser, IdentityRole, string>>()
.UseInMemoryDatabase(databaseName: "InMemoryDataBaseIdentity")
.Options;

this.context = new MyEFxceptionsIdentityContext<IdentityUser, IdentityRole, string>(options);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// ----------------------------------------------------------------------------------
// Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;

namespace STX.EFxceptions.Identity.SqlServer.Tests.Acceptance.Models.Clients
{
public class Client
{
public Guid Id { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// ----------------------------------------------------------------------------------
// Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using STX.EFxceptions.Identity.SqlServer.Tests.Acceptance.Models.Clients;

namespace STX.EFxceptions.Identity.SqlServer.Tests.Acceptance
{
public class MyEFxceptionsIdentityContext<TUser, TRole, TKey>
: EFxceptionsIdentityContext<TUser, TRole, TKey>
where TUser : IdentityUser<TKey>
where TRole : IdentityRole<TKey>
where TKey : IEquatable<TKey>
{
public MyEFxceptionsIdentityContext(DbContextOptions options) : base(options)
{ }

public DbSet<Client> Clients { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
Expand All @@ -20,4 +21,8 @@
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\STX.EFxceptions.Identity.SqlServer\STX.EFxceptions.Identity.SqlServer.csproj" />
</ItemGroup>

</Project>
69 changes: 69 additions & 0 deletions STX.EFxceptions.Identity.SqlServer/EFxceptionsIdentityContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// ----------------------------------------------------------------------------------
// Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------


using System;
using Microsoft.AspNetCore.Identity;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using STX.EFxceptions.Abstractions.Brokers.DbErrorBroker;
using STX.EFxceptions.Abstractions.Services.EFxceptions;
using STX.EFxceptions.Identity.Core;
using STX.EFxceptions.SqlServer.Base.Brokers.DbErrorBroker;
using STX.EFxceptions.SqlServer.Base.Services.Foundations;

namespace STX.EFxceptions.Identity.SqlServer
{
public abstract class EFxceptionsIdentityContext<TUser, TRole, TKey>
: IdentityDbContextBase<TUser, TRole, TKey, IdentityUserClaim<TKey>, IdentityUserRole<TKey>,
IdentityUserLogin<TKey>, IdentityRoleClaim<TKey>, IdentityUserToken<TKey>, SqlException>
where TUser : IdentityUser<TKey>
where TRole : IdentityRole<TKey>
where TKey : IEquatable<TKey>
{
public EFxceptionsIdentityContext(DbContextOptions options) : base(options)
{ }

protected EFxceptionsIdentityContext() : base()
{ }

protected override IDbErrorBroker<SqlException> CreateErrorBroker() =>
new SqlServerErrorBroker();

protected override IEFxceptionService CreateEFxceptionService(
IDbErrorBroker<SqlException> errorBroker)
{
return new SqlServerEFxceptionService(errorBroker);
}
}

public abstract class EFxceptionsIdentityContext<
TUser, TRole, TKey, TUserClaim, TUserRole, TUserLogin, TRoleClaim, TUserToken>
: IdentityDbContextBase<TUser, TRole, TKey,
TUserClaim, TUserRole, TUserLogin, TRoleClaim, TUserToken, SqlException>
where TUser : IdentityUser<TKey>
where TRole : IdentityRole<TKey>
where TKey : IEquatable<TKey>
where TUserClaim : IdentityUserClaim<TKey>
where TUserRole : IdentityUserRole<TKey>
where TUserLogin : IdentityUserLogin<TKey>
where TRoleClaim : IdentityRoleClaim<TKey>
where TUserToken : IdentityUserToken<TKey>
{
public EFxceptionsIdentityContext(DbContextOptions options) : base(options)
{ }

protected EFxceptionsIdentityContext() : base()
{ }

protected override IDbErrorBroker<SqlException> CreateErrorBroker() =>
new SqlServerErrorBroker();

protected override IEFxceptionService CreateEFxceptionService(
IDbErrorBroker<SqlException> errorBroker)
{
return new SqlServerEFxceptionService(errorBroker);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="STX.EFxceptions.Abstractions" Version="0.1.6" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.1" />
<PackageReference Include="STX.EFxceptions.Identity.Core" Version="0.1.6" />
<PackageReference Include="STX.EFxceptions.Abstractions" Version="0.1.6" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\STX.EFxceptions.SqlServer.Base\STX.EFxceptions.SqlServer.Base.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using STX.EFxceptions.Abstractions.Brokers.DbErrorBroker;
using System;

namespace STX.EFxceptions.SqlServer.Base.Services.Foundations
{
Expand Down
Loading