通过 类 引发事件

Throw event thru classes

我有很长的流程,有时我想 return 流程状态。 我有以下代码:

public delegate Task DeviceOnSureMdmResetIMEIHandler(TabletWithoutDeviceIdDto sureMdmTablet, string oldIMEI);
public delegate Task LocalTabletGroupIsNotFoundHandler(TabletWithoutDeviceIdDto sureMdmTablet);
public delegate Task ExistingDeviceIsNotTabletHandler(Device device);
public delegate Task ErrorCombiningDevicesHandler(string result);

public interface ISureMdmTabletService
{
    List<TabletWithoutDeviceIdDto> Tablets { get; set; }

    event DeviceOnSureMdmResetIMEIHandler DeviceOnSureMdmResetIMEIEvent;
    event LocalTabletGroupIsNotFoundHandler LocalTabletGroupIsNotFoundEvent;
    event ExistingDeviceIsNotTabletHandler ExistingDeviceIsNotTabletEvent;
    event ErrorCombiningDevicesHandler ErrorCombiningDevicesEvent;

    List<string> DeviceIsChanged(TabletWithoutDeviceIdDto newItem, Device dbDevice);
    string ImeiIsChanged(TabletWithoutDeviceIdDto newItem, Tablet dbTablet);
    List<string> TabletIsChanged(TabletWithoutDeviceIdDto newItem, Tablet dbTablet);
    bool ShouldPhoneNumberBeUpdated(TabletWithoutDeviceIdDto newItem, Device device);
    bool ShouldICCIDBeUpdated(TabletWithoutDeviceIdDto newItem, Device device);
    bool ShouldIMSIBeUpdated(TabletWithoutDeviceIdDto newItem, Device device);

    void UpdateAlreadyExistingTablets();
    void AddNewTablets();
    void DeletedTablets();
    Task DuplicateImeiRecordsAsync();

    Task<UpdateTabletsFromSureMdmResultDto> UpdateTabletsSaveChangesAsync();
}


public class SureMdmTabletService : ISureMdmTabletService
{
    public event DeviceOnSureMdmResetIMEIHandler DeviceOnSureMdmResetIMEIEvent;
    public event LocalTabletGroupIsNotFoundHandler LocalTabletGroupIsNotFoundEvent;
    public event ExistingDeviceIsNotTabletHandler ExistingDeviceIsNotTabletEvent;
    public event ErrorCombiningDevicesHandler ErrorCombiningDevicesEvent;

    private List<TabletWithoutDeviceIdDto> _tablets;
    public List<TabletWithoutDeviceIdDto> Tablets
    {
        get
        {
            return _tablets;
        }
        set
        {
            _tablets = value;
        }
    }
    private List<string> _tabletsIMEI { get; set; }
    private List<string> _tabletsIds { get; set; }

    private List<Tablet> _alreadyExistsTablets { get; set; }
    private List<Tablet> _allTablets { get; set; }


    private readonly ICombine2DevicesIntoOneService _combine2DevicesIntoOneService;
    private readonly DataContext _dbContext;
    private readonly IMapper _mapper;
    private DeviceModel _deviceModel;

    public SureMdmTabletService(DataContext dbContext, ICombine2DevicesIntoOneService combine2DevicesIntoOneService)
    {
          // .....
    }

    public List<string> DeviceIsChanged(TabletWithoutDeviceIdDto newItem, Device dbDevice)
    {
          // .....
    }

    public string ImeiIsChanged(TabletWithoutDeviceIdDto newItem, Tablet dbTablet)
    {
          // .....
    }

    public List<string> TabletIsChanged(TabletWithoutDeviceIdDto newItem, Tablet dbTablet)
    {
          // .....
    }

    // .....


    public void UpdateAlreadyExistingTablets()
    {
         **DeviceOnSureMdmResetIMEIEvent.Invoke(...);**
    }
}

及其包装:

public interface ISureMdmTabletUpdateCommandService
{
    Task<UpdateTabletsFromSureMdmResultDto> CommandAsync(List<TabletWithoutDeviceIdDto> tablets);
}

public class SureMdmTabletUpdateCommandService : ISureMdmTabletUpdateCommandService
{
    private readonly ISureMdmTabletService _sureMdmTabletService;

    public SureMdmTabletUpdateCommandService(ISureMdmTabletService sureMdmTabletService)
    {
        _sureMdmTabletService = sureMdmTabletService;
    }

    public async Task<UpdateTabletsFromSureMdmResultDto> CommandAsync(List<TabletWithoutDeviceIdDto> tablets)
    {
        _sureMdmTabletService.Tablets = tablets;
        _sureMdmTabletService.UpdateAlreadyExistingTablets();

        _sureMdmTabletService.AddNewTablets();

        _sureMdmTabletService.DeletedTablets();

        await _sureMdmTabletService.DuplicateImeiRecordsAsync();

        return await _sureMdmTabletService.UpdateTabletsSaveChangesAsync();
    }
}

此包装由客户端应用程序使用:

                var resultTablets = await _sureMdmTabletUpdateCommandService.CommandAsync(tablets);

我想像这样订阅来自客户端应用程序的事件:

_sureMdmTabletUpdateCommandService.DeviceOnSureMdmResetIMEIEvent += 

但是 ISureMdmTabletUpdateCommandService 没有,nested class 有。如何在没有 ISureMdmTabletUpdateCommandService 内部的虚拟中间事件的情况下小心地抛出这些事件?

拥有包装器需要你拥有所有的基础设施,你不需要有完整的事件,只有一个中间方法来附加和分离,这样你就可以进行显式的事件实现:

object objectLock = new Object();

event DeviceOnSureMdmResetIMEIHandler DeviceOnSureMdmResetIMEIEvent
    {
        add
        {
            lock (objectLock)
            {
                _sureMdmTabletService.DeviceOnSureMdmResetIMEIEvent += value;
            }
        }
        remove
        {
            lock (objectLock)
            {
                _sureMdmTabletService.DeviceOnSureMdmResetIMEIEvent -= value;
            }
        }
    } 

锁是必需的,因为 += 和 -= 操作不是原子的,它的读取修改写入。


有那么难过,我根本不建议使用事件。我只是将 Action<TabletWithoutDeviceIdDto, string> 作为参数传递,甚至没有为其声明自定义委托。它只是更简单的方法,如果您使用 Func<TabletWithoutDeviceIdDto, string, Task> 而不是事件,它可以事件支持异步操作。