Skip to content

Commit

Permalink
handle open generic type passed as parameter to ImplementInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
NeVeSpl committed Nov 25, 2023
1 parent c5f723d commit 9e69647
Show file tree
Hide file tree
Showing 32 changed files with 399 additions and 43 deletions.
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,23 @@ The library is available as a package on NuGet: [NetArchTest.eNhancedEdition](ht
[TestClass]
public class SampleApp_ModuleAlpha_Tests
{
static readonly Assembly AssemblyUnderTest = typeof(SampleApp.ModuleAlpha.TestUtils).Assembly;
static readonly Assembly AssemblyUnderTest = typeof(TestUtils).Assembly;

[TestMethod]
public void PersistenceIsNotAccessibleFromOutsideOfModuleExceptOfDbContext()
{
var result = Types.InAssembly(AssemblyUnderTest)
.That()
.ResideInNamespace("SampleApp.ModuleAlpha.Persistence")
.ResideInNamespace("SampleApp.ModuleAlpha.Persistence")
.And()
.DoNotHaveNameEndingWith("DbContext")
.Should()
.NotBePublic()
.GetResult();

Assert.IsTrue(result.IsSuccessful);
}

[TestMethod]
public void DomainIsIndependent()
{
Expand All @@ -89,11 +92,32 @@ public class SampleApp_ModuleAlpha_Tests
"System",
"SampleApp.ModuleAlpha.Domain",
"SampleApp.SharedKernel.Domain",
"SampleApp.BuildingBlocks.Domain"
"SampleApp.BuildingBlocks.Domain"
)
.GetResult();

Assert.IsTrue(result.IsSuccessful, "Domain has lost its independence!");
}

}

[TestClass]
public class SampleApp_ModuleOmega_Tests
{
static readonly Assembly AssemblyUnderTest = typeof(TestUtils).Assembly;

[TestMethod]
public void RequestHandlersShouldBeSealed()
{
var result = Types.InAssembly(AssemblyUnderTest)
.That()
.ImplementInterface(typeof(IRequestHandler<,>))
.Should()
.BeSealed()
.GetResult();

Assert.IsTrue(result.IsSuccessful);
}
}
```

Expand Down
7 changes: 5 additions & 2 deletions documentation/readme.nt
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ The library is available as a package on NuGet: [NetArchTest.eNhancedEdition](ht
### Examples

```csharp
{{ sample = data.Classes | Symbols.WhereNameMatches "SampleApp_ModuleAlpha_Tests" | Array.First
sample.SourceCode
{{ sample1 = data.Classes | Symbols.WhereNameMatches "SampleApp_ModuleAlpha_Tests" | Array.First
sample1.SourceCode}}

{{ sample2 = data.Classes | Symbols.WhereNameMatches "SampleApp_ModuleOmega_Tests" | Array.First
sample2.SourceCode
}}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
using System.Reflection;
using MediatR;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetArchTest.Rules;
using SampleApp.ModuleAlpha;

namespace NetArchTest.SampleTests
{
[TestClass]
public class SampleApp_ModuleAlpha_Tests
{
static readonly Assembly AssemblyUnderTest = typeof(SampleApp.ModuleAlpha.TestUtils).Assembly;
static readonly Assembly AssemblyUnderTest = typeof(TestUtils).Assembly;

[TestMethod]
public void PersistenceIsNotAccessibleFromOutsideOfModuleExceptOfDbContext()
{
var result = Types.InAssembly(AssemblyUnderTest)
.That()
.ResideInNamespace("SampleApp.ModuleAlpha.Persistence")
.ResideInNamespace("SampleApp.ModuleAlpha.Persistence")
.And()
.DoNotHaveNameEndingWith("DbContext")
.Should()
.NotBePublic()
.GetResult();

Assert.IsTrue(result.IsSuccessful);
}

Expand All @@ -34,10 +37,12 @@ public void DomainIsIndependent()
"System",
"SampleApp.ModuleAlpha.Domain",
"SampleApp.SharedKernel.Domain",
"SampleApp.BuildingBlocks.Domain"
"SampleApp.BuildingBlocks.Domain"
)
.GetResult();

Assert.IsTrue(result.IsSuccessful, "Domain has lost its independence!");
}

}
}
28 changes: 28 additions & 0 deletions samples/NetArchTest.SampleTests/SampleApp_ModuleOmega_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Reflection;
using MediatR;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetArchTest.Rules;
using SampleApp.ModuleOmega;


namespace NetArchTest.SampleTests
{
[TestClass]
public class SampleApp_ModuleOmega_Tests
{
static readonly Assembly AssemblyUnderTest = typeof(TestUtils).Assembly;

[TestMethod]
public void RequestHandlersShouldBeSealed()
{
var result = Types.InAssembly(AssemblyUnderTest)
.That()
.ImplementInterface(typeof(IRequestHandler<,>))
.Should()
.BeSealed()
.GetResult();

Assert.IsTrue(result.IsSuccessful);
}
}
}
31 changes: 31 additions & 0 deletions samples/SampleApp.BuildingBlocks/Persistence/GenericRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace SampleApp.BuildingBlocks.Persistence
{
public abstract class GenericRepository<TEntity, TContext> where TEntity : class
where TContext : DbContext
{
protected readonly TContext context;


public GenericRepository(TContext context)
{
this.context = context;
}


public void Add(TEntity entity)
{
context.Set<TEntity>().Add(entity);
}
public TEntity GetById(long id)
{
return context.Set<TEntity>().Find(id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<Folder Include="Domain\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion samples/SampleApp.ModuleAlpha/App/Users/UsersService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace SampleApp.ModuleAlpha.App.Users
{
internal class UsersService
internal sealed class UsersService
{

public async Task<long> CreateUser(CreateUser createUser)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SampleApp.ModuleOmega.App.RequestHandlers.Questions
{
internal class QuestionOnListDTO
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MediatR;
using SampleApp.ModuleOmega.Persistence;

namespace SampleApp.ModuleOmega.App.RequestHandlers.Questions
{
internal sealed class ReadQuestionsHandler : IRequestHandler<ReadQuestionsQuery, List<QuestionOnListDTO>>
{
private readonly ReadOnlyTestCreationDbContext context;

public ReadQuestionsHandler(ReadOnlyTestCreationDbContext context)
{
this.context = context;
}


public async Task<List<QuestionOnListDTO>> Handle(ReadQuestionsQuery query, CancellationToken cancellationToken)
{


return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MediatR;

namespace SampleApp.ModuleOmega.App.RequestHandlers.Questions
{
internal class ReadQuestionsQuery : IRequest<List<QuestionOnListDTO>>
{
}
}
11 changes: 11 additions & 0 deletions samples/SampleApp.ModuleOmega/Domain/ITestCreationUoW.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using SampleApp.ModuleOmega.Domain.Questions;

namespace SampleApp.ModuleOmega.Domain
{
internal interface ITestCreationUoW
{
IQuestionRepository Questions { get; }

Task Save();
}
}
13 changes: 13 additions & 0 deletions samples/SampleApp.ModuleOmega/Domain/Questions/Answer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SampleApp.ModuleOmega.Domain.Questions
{
internal sealed class Answer
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SampleApp.ModuleOmega.Domain.Questions
{
internal interface IQuestionRepository
{
Question GetByIdWithAnswers(long id);
}
}
13 changes: 13 additions & 0 deletions samples/SampleApp.ModuleOmega/Domain/Questions/Question.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SampleApp.ModuleOmega.Domain.Questions
{
internal sealed class Question
{
private readonly List<Answer> _answers = new List<Answer>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using SampleApp.BuildingBlocks.Persistence;
using SampleApp.ModuleOmega.Domain.Questions;

namespace SampleApp.ModuleOmega.Persistence.Questions
{
internal sealed class QuestionRepository : GenericRepository<Question, TestCreationDbContext>, IQuestionRepository
{
public QuestionRepository(TestCreationDbContext context) : base(context)
{

}


public Question GetByIdWithAnswers(long id)
{
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace SampleApp.ModuleOmega.Persistence
{
internal sealed class ReadOnlyTestCreationDbContext : TestCreationDbContext
{


public ReadOnlyTestCreationDbContext(DbContextOptions options) : base(options)
{
ChangeTracker.LazyLoadingEnabled = false;
ChangeTracker.AutoDetectChangesEnabled = false;
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}
17 changes: 17 additions & 0 deletions samples/SampleApp.ModuleOmega/Persistence/TestCreationDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using SampleApp.ModuleOmega.Domain.Questions;

namespace SampleApp.ModuleOmega.Persistence
{
internal class TestCreationDbContext : DbContext
{
public DbSet<Question> Questions { get; protected set; }


public TestCreationDbContext(DbContextOptions options) : base(options)
{
ChangeTracker.DeleteOrphansTiming = CascadeTiming.OnSaveChanges;
}
}
}
Loading

0 comments on commit 9e69647

Please sign in to comment.