Skip to content

Commit

Permalink
Add custom CQRS with dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
toxis committed Oct 23, 2024
1 parent a30a81e commit 6b58bdf
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 4 deletions.
20 changes: 20 additions & 0 deletions NewTests/CQRS/GetUpperValueQuery.cs
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);
}
}
20 changes: 20 additions & 0 deletions NewTests/CQRS/GetUpperValueQuerySpecs.cs
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"));
}
}
6 changes: 6 additions & 0 deletions NewTests/CQRS/ICommandHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace NewTests.CQRS;

public interface ICommandHandler
{
void Handle();
}
6 changes: 6 additions & 0 deletions NewTests/CQRS/IQueryHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace NewTests.CQRS;

public interface IQueryHandler<T>
{
T Handle();
}
6 changes: 6 additions & 0 deletions NewTests/CQRS/InjectAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace NewTests.CQRS;

[AttributeUsage(AttributeTargets.Property)]
public class InjectAttribute : Attribute
{
}
27 changes: 27 additions & 0 deletions NewTests/CQRS/Messages.cs
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));
}
}
}
6 changes: 3 additions & 3 deletions NewTests/DependencyInjection/AppContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace NewTests.DependencyInjection;

public class AppContainer
{
private IContainer _container;
private static IContainer _container;

public void RegisterDependencies(Action<ContainerBuilder> action)
{
Expand All @@ -13,7 +13,7 @@ public void RegisterDependencies(Action<ContainerBuilder> action)
_container = builder.Build();
}

public object Resolve(Type type) => _container.Resolve(type);
public static object Resolve(Type type) => _container.Resolve(type);

public T Resolve<T>() => _container.Resolve<T>();
public static T Resolve<T>() => _container.Resolve<T>();
}
2 changes: 1 addition & 1 deletion NewTests/DependencyInjection/DependencyInjectionSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void It_resolves_lower_formatter(Type type, string result)
builder.RegisterType(type).As<IStringFormatter>();
});

var formatter = container.Resolve<IStringFormatter>();
var formatter = AppContainer.Resolve<IStringFormatter>();

Assert.That(formatter.Format("Hello"), Is.EqualTo(result));
}
Expand Down

0 comments on commit 6b58bdf

Please sign in to comment.