-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom CQRS with dependency injection
- Loading branch information
Showing
8 changed files
with
89 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using NewTests.DependencyInjection; | ||
|
||
namespace NewTests.CQRS; | ||
|
||
public class GetUpperValueQuery : IQueryHandler<string> | ||
{ | ||
private readonly string _value; | ||
|
||
[Inject] public IStringFormatter Formatter { get; set; } | ||
|
||
public GetUpperValueQuery(string value) | ||
{ | ||
_value = value; | ||
} | ||
|
||
public string Handle() | ||
{ | ||
return Formatter.Format(_value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Autofac; | ||
using NewTests.DependencyInjection; | ||
|
||
namespace NewTests.CQRS; | ||
|
||
public class GetUpperValueQuerySpecs | ||
{ | ||
[Test] | ||
public void It_get_upper_value() | ||
{ | ||
var container = new AppContainer(); | ||
container.RegisterDependencies(builder => builder.RegisterType<UpperStringFormatter>().As<IStringFormatter>()); | ||
|
||
var query = new GetUpperValueQuery("Hello"); | ||
|
||
var result = Messages.Dispatch(query); | ||
|
||
Assert.That(result, Is.EqualTo("HELLO")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace NewTests.CQRS; | ||
|
||
public interface ICommandHandler | ||
{ | ||
void Handle(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace NewTests.CQRS; | ||
|
||
public interface IQueryHandler<T> | ||
{ | ||
T Handle(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace NewTests.CQRS; | ||
|
||
[AttributeUsage(AttributeTargets.Property)] | ||
public class InjectAttribute : Attribute | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using NewTests.DependencyInjection; | ||
using System.Reflection; | ||
|
||
namespace NewTests.CQRS; | ||
|
||
public static class Messages | ||
{ | ||
public static T Dispatch<T>(IQueryHandler<T> query) | ||
{ | ||
DependencyInjection(query); | ||
return query.Handle(); | ||
} | ||
|
||
public static void Dispatch(ICommandHandler command) | ||
{ | ||
DependencyInjection(command); | ||
command.Handle(); | ||
} | ||
|
||
private static void DependencyInjection(object obj) | ||
{ | ||
foreach (var property in obj.GetType().GetProperties().Where(p => p.GetCustomAttribute(typeof(InjectAttribute)) != null)) | ||
{ | ||
property.SetValue(obj, AppContainer.Resolve(property.PropertyType)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters