开发者

initialize library projects when application starts

In my solution I have several library projects.

I want that each library project will make some initialization when application start.

I know I can do it using static class and static method, but I don't know if this is the best solution.

Those libraries can be used in web application or in win application.

What is the best way to do so?

EDIT:

Example: "Common" project referenced by "Transmissions" projects. "Common" defines a structure of al开发者_开发问答erts that allows registrations. In application initialization, I want the "Transmissions" to registar its own alerts in the alerts structure of "Common".


I would recommend using something like Managed Extensibility Framework and then you can create an interface that contains an Initialize method, when your application starts up you would use MEF to get all of the available libraries and call their Initialize method. This is a lot more flexible and lets you add and remove new components in the future.


Not exactly sure what you are trying to achieve. Using statics you don't have a guaranteed order in which these are initialized. That may not be a requirement.

A different option would be to implement a simple class in each library assuming these are assemblies containing managed code as well.

In the start-up sequence of your application you can then use reflection to go through all libraries and then find that simple class in each library, create one instance of it and invoke the method that contains the initialization code.

There are different ways for identifying the simple class in each library. My recommendation would be to have them implement a particular interface or derive from a particular class. In this case you would have a well-defined interface the start-up sequence of your application could use.

With this approach you also have the option of adding some logic to enforce a particular sequence in which the initialization code is executed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜