Hi! I want to master this small library and I have the following question. I have implemented:
Domain
Dependencies
Commands
and
And finally, I want to somehow run the method GetMethodAsync() from class Program.cs
After all, to initialize a class, I need to pass an mediator to the class constructor, and I can't figure out how to do it.
It's obvious that I'm doing something wrong.
Domain
Client.cs:
namespace MediatrDemo.Domain
{
public class Client
{
public int ID { get; set; }
public string Name { get; set; }
public string OtherInfo { get; set; }
}
}
IPersonObj.cs:
namespace MediatrDemo.Dependencies
{
public interface IPersonObj
{
Task<Client> GetClient();
}
}
GetDataObject.cs:
namespace MediatrDemo.Dependencies
{
public class GetDataObject : IPersonObj
{
public async Task<Client> GetClient()
{
var db = new EntityContext();
var tempClient = db.DearClient.Where(q => q.ID == 1);
var p = new Client();
foreach (var item in tempClient)
{
p.Name = item.Name;
p.OtherInfo = item.OtherInfo;
}
Debug.WriteLine(p.Name);
return await Task.FromResult(p);
}
}
}
GetDataCommand.cs:
namespace MediatrDemo.Commands
{
public class GetDataCommand : IRequest<Task<Client>>
{
}
}
GetDataHandler.cs:
namespace MediatrDemo.Commands
{
public class GetDataHandler : RequestHandler<GetDataCommand, Task<Client>>
{
private readonly IPersonObj _data;
public GetDataHandler(IPersonObj data)
{
_data = data;
}
protected override async Task<Client> Handle(GetDataCommand request)
{
return await _data.GetClient();
}
}
}
and
PrimaryApp.cs:
namespace MediatrDemo.Application
{
public class PrimaryApp
{
private Client client = new Client();
private readonly IMediator _mediator;
public PrimaryApp(IMediator mediator)
{
_mediator = mediator;
}
public async void GetMethodAsync()
{
client = await await _mediator.Send(new GetDataCommand());
}
}
}
And finally, I want to somehow run the method GetMethodAsync() from class Program.cs
Program.cs:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
PrimaryApp primaryApp = new PrimaryApp(); // Error!
Console.ReadLine();
}
}
It's obvious that I'm doing something wrong.