1
votes

We have several custom lists, each of which has several event receivers associated with it (although only a couple have actually been written as of now). This event receivers handle a mish-mash of individual events and perform various bits of functionality. My question is how to organize them in a Visual Studio solution/project in a way that makes sense. Which event receivers should have their own function/class/folder/project/solution?

Currently we have two solutions, each of which is simply using whatever Visual Studio default to when creating a new Event Receiver project with its own solution. The other has a single project, with one folder named after a particular list which is set to handle two events (ItemAdded and ItemUpdating) on that list. The folder contains two C# files, each of which contains its own class with one function for that particular event receiver.

But if I have several event receivers for one list, should I put those into a single file? Or into separate files in the same folder? Or into separate projects? And if I have two logically distinct actions that should be performed on the same list item in the same event, should I have one event receiver which does both actions, or separate event receivers for each?

My current thought was to have a folder for each list, with separate files for each event receiver, and using a single event receiver to perform whatever actions are appropriate during that event (whether independent or not). I'm just wondering what a "best practices" sort of organization would be.

1

1 Answers

0
votes

This is more ore less a personal view, as I haven't seen any best practices on the subject. In my projects, I have the following structure for lists and their corresponding event handler classes:

12  
|- TEMPLATE  
|-- FEATURES  
|--- My Feature  
|---- CustomList1  
|----- Allitems.aspx  
|----- DispForm.aspx  
|----- EditForm.aspx  
|----- ListDefinition.xml  
|----- ItemEventReceiver.cs  
|----- ListEventReceiver.cs  
|----- schema.xml  
|----- Etc, etc...  
|---- CustomList2  
|----- Allitems.aspx  
|----- Etc, etc...  

I have a file for Item Receiver Events and one class for List Receiver Events.
In the ItemEventReceiver.cs , I have all ItemDeleting, ItemAdding etc. , and in the ListEventReceiver.cs I have FieldAdding, FieldUpdated etc.

One alternative would be to place all event receiver code/classes in a separate folder structure, but in my opinion it's better to have them in the same folder as the list definition. That way, everything related to that list is in one place.

In the end, I think you must use the structure which works best for you and your team and be consistent.