EventHandlers saved to databases.

Posted by Stacey on Stack Overflow See other posts from Stack Overflow or by Stacey
Published on 2010-03-21T17:03:41Z Indexed on 2010/03/21 17:11 UTC
Read the original article Hit count: 144

Filed under:
|
|

In a database application (using Sql Server right now, in C#, with Entity Framework 4.0) I have a situation where I need to trigger events when some values change. For instance assume a class "Trackable".

class Trackable
{
 string Name { get; set; }
 int Positive { get; set; }
 int Negative { get; set; }
 int Total { get; set; }
 // event OnChanged
}

Trackable is represented in the database as follows;

table Trackables
Id | guid
name | varchar(32)
positive | int
negative | int

Total is of course, calculated at runtime.

When a trackable event changes, I want to inspect its previous value, and then see what it is changing to, and be capable of reacting accordingly. However different trackables need to trigger different events (to avoid a huge, massive cascading switch/if block).

If this were just only C# code it would be easy - but they have to be saved to the database. I can't divide up each different trackable into a different table/class, that would be silly - they are all identical, but the event raised is different based on how they are made.

So I guess my question is, is there any way to store an event handler in a database such that..

Trackable t1 = new Trackable()
{
 Name = "Trackable1"
 OnChange += TrackableChangedEventHandler(OnTrackable1Change)
}

Trackable t2 = new Trackable()
{
 Name = "Trackable2",
 OnChange += TrackableChangedEventHandler(OnTrackable2Change)
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about database