C
C#4d ago
Zoli

Why event is not triggered correctly?

I have a unit of work pattern and when I save an item I want to fire an event to trigger a sync method in another service class. Unfortunatelly this way it is not triggered what am I doing wrong?
/// <summary>
/// Sync agent. It's the sync orchestrator
/// Knows both the Sync Server provider and the Sync Client provider.
/// </summary>
public class SyncAgent
{
private readonly IUnitOfWork _unitOfWork;

public SyncAgent(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}

public async Task SynchronizeAsync()
{

Debug.WriteLine("Test saved change");
// get syncronizable entities
//var result = await _unitOfWork.GetSyncableItemsAsync();
}
}

public class UnitOfWork : IUnitOfWork
{

public event EventHandler EntitySaved;

public async Task SaveChangesAsync()
{
// saving stuff
// ...

// fire event
EntitySaved?.Invoke(this, new EventArgs());
}
}

public class RemoteSyncHandler
{
private readonly SyncAgent _syncAgent;

public RemoteSyncHandler(SyncAgent syncAgent)
{
_syncAgent = syncAgent;
}

public void OnChangesSaved(object sender, EventArgs e)
{
_syncAgent.SynchronizeAsync().SafeFireAndForget();
}
}

// in the the Ioc

services.AddScoped<RemoteSyncHandler>(provider =>
{
var handler = new RemoteSyncHandler(provider.GetRequiredService<SyncAgent>());
var unitOfWork = provider.GetRequiredService<IUnitOfWork>();

// Subscribe to the ChangesSaved event
unitOfWork.EntitySaved += handler.OnChangesSaved;

return handler;
});
/// <summary>
/// Sync agent. It's the sync orchestrator
/// Knows both the Sync Server provider and the Sync Client provider.
/// </summary>
public class SyncAgent
{
private readonly IUnitOfWork _unitOfWork;

public SyncAgent(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}

public async Task SynchronizeAsync()
{

Debug.WriteLine("Test saved change");
// get syncronizable entities
//var result = await _unitOfWork.GetSyncableItemsAsync();
}
}

public class UnitOfWork : IUnitOfWork
{

public event EventHandler EntitySaved;

public async Task SaveChangesAsync()
{
// saving stuff
// ...

// fire event
EntitySaved?.Invoke(this, new EventArgs());
}
}

public class RemoteSyncHandler
{
private readonly SyncAgent _syncAgent;

public RemoteSyncHandler(SyncAgent syncAgent)
{
_syncAgent = syncAgent;
}

public void OnChangesSaved(object sender, EventArgs e)
{
_syncAgent.SynchronizeAsync().SafeFireAndForget();
}
}

// in the the Ioc

services.AddScoped<RemoteSyncHandler>(provider =>
{
var handler = new RemoteSyncHandler(provider.GetRequiredService<SyncAgent>());
var unitOfWork = provider.GetRequiredService<IUnitOfWork>();

// Subscribe to the ChangesSaved event
unitOfWork.EntitySaved += handler.OnChangesSaved;

return handler;
});
7 Replies
HtmlCompiler
HtmlCompiler4d ago
that's scoped, you need a persisten "bus" to send events from the looks of it although i guess if set right it should work
Zoli
ZoliOP4d ago
So somthing like WeakReferenceMessenger would be better fit for this?
HtmlCompiler
HtmlCompiler4d ago
i never used that, sorry
SevenMgCreatine
Hey, just my thought: You register the RemoteSyncHandler but where is it getting constructed/injected somewhere? I mean if you set a breakpoint in the lambda in the IoC does this every getting triggered? services.AddScoped<IUnitOfWork>(provider => { var uow = new UnitOfWork(); var remoteSyncHandler = provider.GetRequiredService<RemoteSyncHandler>(); uow.EntitySaved += remoteSyncHandler.OnChangesSaved; return uow; }); and just register RemoteSyncHandler and the SyncAgent as Scoped too.
HtmlCompiler
HtmlCompiler4d ago
i would study the problem for a while to be certain this (captring SaveChanges) is the correct solution to apply because depending on the urgency of the sync and on the size of the data a timed batch sync could also work
Zoli
ZoliOP4d ago
@SevenMgCreatine You are right it is not hit the breakpoint at all. I did not instantiated at all.
joe
joe4h ago
By ensuring proper event subscription, managing instances correctly, and handling asynchronous calls appropriately, your synchronization process should trigger as expected when an entity is saved in your unit of work

Did you find this page helpful?