Can I override SaveChangesAsync() for all my repositories?

36 Views Asked by At

I have a pretty large Blazor project where I have some small repositories (for simple entities) that derive from a GenericRepository like

public class MySmallRepositoryOne : GenericRepository<MySmallEntityOne>, IMySmallRepositoryOne

But I also have Repositories for more complex entities that doens't derive from GenericRepository.

Are there a way to make an

public override int SaveChangesAsync()
{
    // my code

    return base.SaveChangesAsync();
}

that override SaveChangesAsync() for all repositories? Or does every repository need to derive from Generic and it's in generic I put this override?

1

There are 1 best solutions below

0
Qiang Fu On

Yes, you could override "savechanges" in specific "xxxDbContext". But SaveChangesAsync should be written asynchronously to use await. Needn't derive every repository.

        public override int SaveChanges()
        {
            return base.SaveChanges();
        }
        public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
        {
            return base.SaveChangesAsync(cancellationToken);
        }