NickStrupat / EntityFramework.Triggers

Adds events for entity inserting, inserted, updating, updated, deleting, and deleted
MIT License
373 stars 46 forks source link

Abstract Class byte[] instead? #41

Closed sethdorris closed 5 years ago

sethdorris commented 5 years ago

In your example of the abstract class Trackable, why would you not use something like

[Rowversion]
public byte[] Inserted {get ; private set } 

Is this because only 1 rowversion tag can be on an object, therefore we wouldn't have a rowversion for updated?

NickStrupat commented 5 years ago

I don't use rowversion because it's strictly a SQL Server thing, so it wouldn't work with any other EF providers. If you know you're going to be using SQL Server, it makes sense to use rowversion

NickStrupat commented 5 years ago

Just to give more context, this whole library operates on top of Entity Framework regardless of the provider. Good optimizations could definitely be made by targeting individual providers.

For example, having the library create and delete SQL Server triggers which directly invoke the handlers would have advantages, but it would require implementing that for each provider that I choose to support. An early decision I made was to have all my code operate within the public API of Entity Framework, mostly so it works on all EF providers and is easy to maintain.

sethdorris commented 5 years ago

I see... thank you

sethdorris commented 5 years ago

This library wouldn't be for me then if I have multiple applications that share the same database and if another application changes the underlying database records the triggers with this library wouldn't detect those changes and fire an event handler would it?

NickStrupat commented 5 years ago

You're correct in that this library does not provide the functionality you're describing. You can, however, use the triggers to write the changes into a shared message queue that each instance of your application subscribes to.

sethdorris commented 5 years ago

Thanks for the reply @NickStrupat do you have documentation on how to use the library to do that with EF? Not sure how to use EF to write to a queue

NickStrupat commented 5 years ago

Basically you attach event handlers to all the triggers you're interested in, where the handlers each send messages into some message queue you need to set up yourself.

Something like...

Triggers<Foo>.Inserted += e => messageQueue.SendMessage(JsonConvert.SerializeObject(new { Change = "Inserted", Entity = e.Entity }));

You need to make sure your message queue (aka service bus) is set up so that all your application instances send messages to one central queue, which then sends each message out to all the other instances. That way, the instances each receive updates which were made by only one application.

Just in case you're looking for one, there are several message queue libraries out there for .NET. A good list here https://github.com/quozd/awesome-dotnet#queue

NickStrupat commented 5 years ago

In case you are using dependency injection, it is supported in the latest version of this library. The closest thing I have to documentation is a tiny example in another issue thread.

https://github.com/NickStrupat/EntityFramework.Triggers/issues/23#issuecomment-442510823