I am trying to implement the CQRS pattern in clean architecture.
I know If a commads/query then i need to add a folder named as commads/query in applictionLayer.
Now I want to create a void method. not a command or query it is just a void method. and I want to use this again into my queries and commands.
public void FormatSomethingHtml(Something something)
{
something.FinePrint = _helper.FormatHtml(something.FinePrint, something.SiteCode.Name);
// removed rest
}
I don't know where should create this method.
If I create this method inside commandHandler, I could not reuse it. i.e:
public class UpdateSomethingCommandHandler : IRequestHandler<UpdateSomethingCommand, Something>
{
// removed rest
public void FormatSomethingHtml(Something something)
{
something.FinePrint = _helper.FormatHtml(something.FinePrint, something.SiteCode.Name);
// removed rest
}
}
In above method I can use inside UpdateSomethingCommandHandler.
But I could not reuse it in another handler.
ie. When I call like this
new UpdateSomethingCommandHandler.FormatSomethingHtml(something);
I got error.
Where can create a common method?
This should be a utility service and inject it in any query/handler you need.
Ex:
public interface IFormatService
{
void FormatSomethingHtml(Something something)
}
public class HtmlFormatService: IFormatService
{
public void FormatSomethingHtml(Something something)
{
something.FinePrint = _helper.FormatHtml(something.FinePrint, something.SiteCode.Name);
// removed rest
}
}
In your handler, you would inject it and use it just like any other service
Also, I would consider changing the return type to either Something so you can update the object and return ot or simply accept a string and return the formatted string like this:
public string FormatSomethingHtml(string something)
{
return _helper.FormatHtml(something.FinePrint, something.SiteCode.Name);
}
In your handler/query
var something = new Something();
something.Fingerprint = _formatter.FormatSomethingHtml(...)