EventHandler ASP.NET CORE

问题描述 投票:3回答:1

我在ASP.NET CORE中有一个项目。我想在完成ExchangeService类的RejectExchange方法之前调用一个方法(以后再调用几种方法)。因此,我读到了有关EventHandlers的信息,但我不知道我应该在哪里将方法分配给委托。我有ExchangeService类:

public class ExchangeService : IExchange
{
    private readonly SocialNetworkContext _context;
    private readonly IUser _userService;
    private readonly IItem _itemService;
    private readonly IExchangeRequestNotification _exchangeRequestNotificationService;

    public event EventHandler<Exchange> ExchangeRejecting;

    protected virtual void OnExchangeRejecting(Exchange exchange)
    {
        ExchangeRejecting?.Invoke(this, exchange);
    }

    public void RejectExchange(int id)
    {
        var exchange = GetById(id);

        OnExchangeRejecting(exchange);

        Delete(exchange);
    }

    public ExchangeService(SocialNetworkContext context, IUser userService, IItem itemService, IExchangeRequestNotification exchangeRequestNotificationService)
    {
        _context = context;
        _userService = userService;
        _itemService = itemService;
        _exchangeRequestNotificationService = exchangeRequestNotificationService;            
    }
}

而且我想在ExchangeRequestNorificationService中删除有关交换的通知,然后再删除交换,因此我创建了方法OnExchangeRejecting:

public class ExchangeRequestNotificationService : IExchangeRequestNotification
{
    private readonly SocialNetworkContext _context;
    private readonly IUser _userService;

    public ExchangeRequestNotificationService(SocialNetworkContext context, IUser userService)
    {
        _context = context;
        _userService = userService;
    }

    public void OnExchangeRejecting(object source, Exchange exchange) => Delete(GetByExchangeId(exchange.Id));    

    public void Delete(ExchangeRequestNotification exchangeRequestNotification)
    {
        _context.Remove(exchangeRequestNotification);
        _context.SaveChanges();
    }
}

一切都很好,但我不知道必须在哪里将方法分配给委托。我有ExchangeController,在其中使用依赖项注入并将IExchangeService传递给构造函数,并在其中分配要委派的方法:

public ExchangeController(IExchange exchangeService, IExchangeRequestNotification exchangeRequestNotification, IChat chatService)
    {
        _exchangeService = exchangeService;
        _exchangeRequestNotificationService = exchangeRequestNotification;
        exchangeService.ExchangeRejecting += _exchangeRequestNotificationService.OnExchangeRejecting;
        exchangeService.ExchangeRejecting += chatService.OnExchangeRejecting;
    }

但是我认为这是不对的,因为在此控制器中,我实际上并不需要ExchangeRequestNotificationService和ChatService(也具有方法OnExchangeRejecting),我只需要ExchangeService。仅将ExchangeRequestNotificationService传递给控制器​​,以便将方法分配给方法OnRejectingExchange上的委托,而且这样会更糟(注入的不需要的服务更多)。

我的问题是我应该在哪里为代理委派方法?对不起,我的英语,希望您能理解我的问题:)

c# asp.net asp.net-core delegates eventhandler
1个回答
0
投票

我一直在问自己同样的问题,而我在关于stackoverflow的文章中碰到过。由于我没有在网上找到解决方案,因此我尝试自己解决该问题。

对于该解决方案,我使用库Autofac作为DI提供程序并配置了我的服务,该服务通过Autofac的Lifetime事件公开了EventHandler。

示例:

builder.RegisterType<MyService>().InstancePerRequest().OnActivated(e =>
{
    var dep = e.Context.Resolve<MyEventHandler>();
    e.Instance.EntitySaved += dep.OnEntitySaved;
});

因此,无论我的服务注入到哪里,我都可以保证EventHandler总是被注册。为了避免循环依赖问题,我选择了OnActivated生命周期事件而不是OnActivating事件。例如如果MyHandler应该由EventHandler间接使用。

最好的问候,丹尼尔

© www.soinside.com 2019 - 2024. All rights reserved.