Вопрос

Problem:
I am constructing a framework which accepts a file, translates it and executes it. The framework should be able to handle any type of file, to this end i have provided a method of uploading a DLL containing classes and methods for translating and executing a file. What i am looking for, is the best way to define the plugin interface

Solution A:
Define a set of interfaces which are publicly available. Plugins should implement these interfaces.

Solution B:
Define some abstract classes which are publicly available. Plugins should inherrit and override the abstract methods on these classes.

Solution C: rcravens
Pass interfaces around inside the code, create an abstract class which is publicly available to allow for plugin extensibility. Chosen
This solution was chosen ahead of interface only because it enables basic implementation (handy in this case). It was chosen ahead of abstract class only because it enables mocking within the code. The composition frameworks are excellent, but a bit over the top for something as lightweight as this application where only limited extensibility is desired.

Solution D: Jay and Chris Shain
Implement a composition framework (such as Managed Extensibility Framework(MEF)) and build around it

If any new solutions appear, i will add them to this list. The answer will go to the person who is best able to justify their solution (possibly with advantages and limitation)

Thanks in advance,
Tech Test Dude

Это было полезно?

Решение

At the lowest level I believe you need interfaces. This allows most mocking frameworks to easily provide fakes. Around your code you should pass around the interfaces. If you need some base implementation that can be refactored into an abstract base class, then do it. Abstract base classes and interfaces are not mutually exclusive concepts. Some times it makes sense to have both.

Другие советы

What you are writing sounds suspiciously like what Managed Extensibility Framework supports: http://mef.codeplex.com/. Maybe just use that and avoid re-inventing the wheel?

I don't think there's a better solution between interface or abstract class, it really depends on what you need. But personnally I would probably go with abstract class, for the simple fact that it offers more flexibility.

You could provide some abstract methods that are essential to define particular behavior of the plugin, while virtual methods offers a way to optionnaly override a default behavior.

The abstract class also can provide some utility methods that could be usefull to the plugin's authors.

Basically, an abstract class can offer all that an interface offer, and more. So it's probably a better for future extension.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top