Implement partial methods for multiple assemblies

In one of the applications I'm working on, there are two main functions: Create and update.

However, sometimes you need to add your own code, so I thought about expanding the code, allowing third parties to write and paste their own code:

OnCreating OnCreated OnUpdating OnUpdated

Is there a way to include the above in multiple builds? MEF can help here?

Thanks. Relationship


Thank you all for your answers.

The presence of such an interface means that every external assembly must implement this interface as necessary. Then, the code of my application, do I need to scroll through the currently running assemblies, detect all classes that implement this interface, and run their methods?

Does MEF fit here? Can I export the implementation from external assemblies and import them inside my application?

Thanks. Relationship

+7
source share
7 answers

As for your MEF question, you can do something like the following to run methods from the interface:

var catalog = new DirectoryCatalog("bin"); var container = new CompositionContainer(catalog); container.ComposeParts(); var plugins = container.GetExportedValues<IPlugin>(); foreach (IPlugin plugin in plugins) { plugin.OnCreating(); } 

Or create an event interface as suggested by Brian Mines:

 public interface IPlugin { event OnCreatingEventHandler OnCreating; } 

then the above code will be more like:

 var catalog = new DirectoryCatalog("bin"); var container = new CompositionContainer(catalog); container.ComposeParts(); var plugins = container.GetExportedValues<IPlugin>(); foreach (IPlugin plugin in plugins) { plugin.OnCreating += MyOnCreatingHandler; } 

I think I like the latter for the names of the methods you specified. For my work with the plugin, I created an interface similar to the following:

 public interface IPlugin { void Setup(); void RegisterEntities(); void SeedFactoryData(); } 

The RegisterEntities() method extends the database schema at run time, and the SeedFactoryData() method adds any default data (for example, adding a default user, pre-populating a city table, etc.).

+2
source

You cannot have partial assembly classes, because partial classes are a language function, not a CLR function. The C # compiler combines all partial classes into one real class, and this single class remains the only one after compilation.

You have several alternatives:

  • Suggest events
  • Make methods virtual and override them
  • Use interface

Your problem looks like it is better suited to events. A user can simply subscribe to them in another assembly.

+14
source

Partial classes supported in assemblies are not supported.

The reason is that all partial class definitions are combined into one class at compile time. This single class is in one assembly.

+1
source

Consider using the interface:

 IUpdatableObject<X> Creating(X obj); Created(X obj); Updating(X obj); Updated(X obj); 

And then use this interface to add to user code; each third party can implement this interface (well, either they, or you can through the shell), and this can become a connecting link in adding custom business logic.

NTN.

+1
source

You (or the user) can use the extension methods: http://msdn.microsoft.com/en-us/library/bb383977.aspx

+1
source

Partial methods and classes must be in the same assembly. Partial methods compile if not used. If you need extension points for you, you should explore virtual methods or events.

0
source

Partial classes in assemblies are not supported because the concept of a partial class is to allow multiple developers to contribute different methods and members for the same class in a particular namespace.
This was done to help developers synchronize code into one class, within the same assembly, after compilation.

0
source

All Articles