Skip to content

Vertical Slice coupling with Notifications – where should side-effects live? #1168

Description

@mikek9084

Title: Vertical Slice Architecture – handling side-effects without cross-slice coupling

Hi 👋

I’m applying a vertical slice architecture with MediatR, and I’m running into a design concern around side-effects and coupling between slices.

Context

After each student is created, I want to fire:

StudentCreatedNotification

This notification is handled by multiple handlers:

  • StudentReminderWorker
  • StudentViewedNotificationHandler

Current command handler:

public class CommandHandler : IRequestHandler<Command, int>
{
    private readonly SchoolContext _db;

    public CommandHandler(SchoolContext db) => _db = db;

    public async Task<int> Handle(Command message, CancellationToken token)
    {
        var student = new Student
        {
            FirstMidName = message.FirstMidName,
            LastName = message.LastName,
            EnrollmentDate = message.EnrollmentDate!.Value
        };

        await _db.Students.AddAsync(student, token);
        await _db.SaveChangesAsync(token);

        mediator.get().publish(
            StudentCreatedNotification(student.Id)
        );

        return student.Id;
    }
}

Problem

Now the Student vertical slice is directly aware of and publishing a notification that belongs to another module/slice.

This introduces coupling between:

  • Student slice (core business behavior)
  • Notification/Worker slices (side-effects)

Which feels like it breaks vertical slice isolation.

Questions

  1. Where should this kind of side-effect live in a vertical slice architecture?
  2. How do you avoid coupling between slices when publishing notifications/events?

Would really appreciate guidance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions