Can you use EventPublication in a static class?

Topics: CAB & Smart Client Software Factory
Oct 27, 2006 at 5:42 AM
originally posted by: pcfountain

I am trying to create a static class that fires events. This will basically be a utility class that allows certain methods to be called easily from anywhere. The problem that I'm having is I have an EventPublication attribute in this class, and of course the corresponding EventHandler must be static. When I call that EventHandler later in the code, it is null. I have tried adding a WorkItem to this class as well, using both the ServiceDependency and CreateNew attributes, but the WorkItem never gets created either.

Is it possible to fire CAB events from a static class?
Oct 27, 2006 at 8:02 PM
originally posted by: hmoeller

The basic principle of CAB is - besides the WorkItem concept - the ObjectBuilder. It uses a set of strategies to achieve about everything you can do with CAB. Those strategies are applied during object construction. Since static classes won't be instantiated at any time, you won't be able to use all those nice CAB attributes on them. (Well, at least it won't have any effect to use them...)

Think about changing your static class into a service. Those services are automatically created as singletons. That has about the same effect as a static class, but you get all the CAB stuff at your hands. Of course, you'll pay with some performance loss since static classes normally perform better than objects in most cases.