How to call a method inside another assembly that you cannot reference?
I'm trying to made a call represented by the red line, but, my domain layer cannot reference my application layer because it will cause a cyclic redundancy, so, looking on internet I found a way to do that loading the assembly, but, I'm not sure if it is the better way to do that
The application use dependency-injection, this is a simplified diagram of the application:
This is the complete implementation of the Domain class
public class SomeDomainService<T> : ISomeDomainService<T>
{
private readonly IServiceProvider _serviceProvider;
public SomeDomainService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public async Task<SomeEntity> SomeProcess(SomeEntity someEntity)
{
await Task.Delay(1000);
someEntity.Id = new Random().Next(3000);
someEntity.Name = $"Entity {someEntity.Id}";
someEntity.Description = "lorem ipsum";
/*
*
* SOME PROCESS
*
*/
var aplicationAssembly = Assembly.Load("Application");
var types = aplicationAssembly.GetTypes();
var type = types.FirstOrDefault(t=> t.Name == "IAnotherApplicationService`1");
if (type != null)
{
var domainMessage = new Message {
Id = someEntity.Id.ToString(),
Title = $"Some entity {someEntity.Name}",
Body = "Message from Domain"
};
var runtimeType = type.MakeGenericType(typeof(Message));
var methods = runtimeType.GetRuntimeMethods();
var service = _serviceProvider.GetService(runtimeType);
var method = service.GetType().GetMethod("SendSomeMessage");
method.Invoke(service, new object[] { domainMessage});
}
return someEntity;
}
}
