EventHandler ASP.NET CORE

时间:2017-12-25 12:24:33

标签: c# asp.net asp.net-core delegates eventhandler

我在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上的委托,并且在更糟糕的情况下,它将更糟糕(更多不需要注入的服务)。

我的问题是我应该如何激动地将方法分配给代表?对不起我的英语,我希望你能理解我的问题:)

1 个答案:

答案 0 :(得分: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间接使用。

最好的问候, 丹尼尔