0

I am starting a project where the program will poll a database every x seconds (say 10) and get rows that fall within a certain time period (based on a datetime field). This could be 0 - 40 rows or so for each poll. These rows will then be put into another database after some business logic is run. My initial thought is there will be some logic that puts the rows (converted to objects) in a collection, like a list, and the list has an event that gets fired when an item is added. Finally, the row is put in the dest. DB.

I'm thinking about it like this:

PsuedoCode

private List<Person> PersonList;
public MyClassConst()
{
    PersonList = new List<Person>();
    PersonList.AddingEvent += HandleAddingEvent;
}
void HandleAddingEvent(...)
{
   // run business logic/rules
   // insert/update row in destination DB
}

void Timer_Elapsed()
{
   // query and convert to Person objects
   var personData = _personDao.GetPersons();

   for(int i=0; i< personData.Count; ++i)
   {
      PersonList.add(personData[i]);
   }
}

My concern is that if there are 40 rows, the event will be triggered 40 times, which doesn't seem ideal. How would I disable the event until after all the 40 objects have been added to the list.

if there is a better solution than using a list to do this, by all means, please share.

O.O
  • 11,077
  • 18
  • 94
  • 182
  • http://stackoverflow.com/questions/5288434/how-to-monitor-sql-server-table-changes-by-using-c – spender May 23 '13 at 20:17
  • @spender - I fail to see where I said I was using Sql Server, because I'm not. – O.O May 23 '13 at 20:19
  • What **are** you using? – spender May 23 '13 at 20:20
  • @spender - InterSystems Cache DB. The database is not the point of the question. – O.O May 23 '13 at 20:23
  • @O.O spender's point is that rather than polling the DB for changes, most DB's will provide some sort of trigger mechanism that lets you execute code when an item is added/updated. – Servy May 23 '13 at 20:32
  • @Servy - ya, unfortunately, even if this DB did that (and I'm pretty sure it doesn't), there is no API or way for me to use it. I def. already looked at this option. – O.O May 23 '13 at 20:42

2 Answers2

1

Well, the technical answer to your question is just to use an ObservableCollection. It has an event that is fired when the collection is modified.

But I don't think that's the appropriate solution for your problem here. What you really have here is a producer/consumer queue. That can be best served using a BlockingCollection.

Expose the collection to both the producer(s) and the consumer(s). The producers can Add items to the collection, and the consumers can Take items. The collection is specifically designed to be accessed from multiple threads simultaneously, you don't need to synchronize access.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • BlockingCollection looks like the ideal solution, even though I'm not going to be having multiple apps talking to it, it doesn't need to be thread-safe for my purpose. – O.O May 23 '13 at 20:31
  • @O.O Well, it's actually quite common to have one producer and one consumer. That's still two threads. – Servy May 23 '13 at 20:32
  • So, if the producer added 40 objects to the BlockingCollection, could I batch the inserts to one insert of 40 within the consumer, or am I overthinking this? – O.O May 23 '13 at 20:46
  • 1
    @O.O If your application has no possibility of multiple consumers then what is the rationale behind using BlockingCollection collection rather basic. Just a thought and decision is up to you. – S.N May 23 '13 at 20:47
  • @O.O Just use a `BlockingCollection>` so that each item in the collection represents one batch. – Servy May 23 '13 at 20:55
  • There are thoughts that there will end up being multiple apps now, so this seems like the way to go. – O.O Jun 27 '13 at 15:23
  • @O.O I meant threads, not applications. You can't access a blocking collection for entirely different applications. – Servy Jun 27 '13 at 15:25
1

Change your list to an ObservableCollection<T>. And subscribe for CollectionChanged events and with the event hadler you can check the number of items available at any given point and based on that you can trigger the Db insertion logic. Read more

S.N
  • 4,910
  • 5
  • 31
  • 51
  • I don't want to hit the event handler 40 times. – O.O May 23 '13 at 20:24
  • Here you are not firing any events. This is default feature of ObservableCollection and it will trigger the events whether or not it has any subscriber. Now if you don't wanted to take this path. Then try to check the number of elements in the List after performing each addition. Use Count property of the list to do this. – S.N May 23 '13 at 20:36