Question Help with "MediatR" template implementation

rammfire

Member
Joined
Sep 1, 2020
Messages
14
Programming Experience
Beginner
Hi! I want to master this small library and I have the following question. I have implemented:
Domain
Client.cs:
namespace MediatrDemo.Domain
{
    public class Client
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string OtherInfo { get; set; }
    }
}
Dependencies
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);
        }
    }
}
Commands
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();
        }
    }
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.
 
It would help if you linked to where this so call "small library" is at and any documentation it may have.
 
If you look in the samples directory of that repository, almost all of them have some kind of BuildMediator() method that returns an IMediator instance.
 

Latest posts

Back
Top Bottom