我在解决这个问题时采用了稍微不同的方法。我的解决方案包括一个 winform 前端、一个主类库 (DLL) 以及在该 dll 中的一个辅助工作类:
WinForm
|-----> PickGen 库
|---------> 分配类
我决定做的是在分配类可以调用的主 dll (PickGen) 中创建事件,然后这些事件方法将在 UI 中调用事件。
因此,allocations 在 PickGen 中引发了一个事件,该事件采用参数值并在表单中引发事件。从代码的角度来看,这是最底层的:
public delegate void AllocationService_RaiseAllocLog(string orderNumber, string message, bool logToDatabase);
public delegate void AllocationService_RaiseAllocErrorLog(string orderNumber, string message, bool logToDatabase);
public class AllocationService { ...
public event AllocationService_RaiseAllocLog RaiseAllocLog;
public event AllocationService_RaiseAllocErrorLog RaiseAllocErrorLog;
然后在子类代码中:
RaiseAllocErrorLog(SOHNUM_0, ShipmentGenerated + ": Allocated line QTY was: " + allocatedline.QTY_0 + ", Delivered was: " + QTY_0 + ". Problem batch.", false);
在主 DLL 类库中,我有这两个事件方法:
private void PickGenLibrary_RaiseAllocLog(string orderNumber, string message, bool updateDB)
{
RaiseLog(orderNumber, message, false);
}
private void PickGenLibrary_RaiseAllocErrorLog(string orderNumber, string message, bool updateDB)
{
RaiseErrorLog(orderNumber, message, false);
}
我在创建分配对象时在这里建立连接:
AllocationService allsvc = new AllocationService(PickResult);
allsvc.RaiseAllocLog += new AllocationService_RaiseAllocLog(PickGenLibrary_RaiseAllocLog);
allsvc.RaiseAllocErrorLog += new AllocationService_RaiseAllocErrorLog(PickGenLibrary_RaiseAllocErrorLog);
然后我还设置了委托,以将主类与 winform 代码联系起来:
public delegate void JPPAPickGenLibrary_RaiseLog(string orderNumber, string message, bool logToDatabase);
public delegate void JPPAPickGenLibrary_RaiseErrorLog(string orderNumber, string message, bool logToDatabase);
这可能不是最优雅的方式,但最终,它确实有效,而且不会太晦涩。