This project is read-only.

attributes question

Topics: Customizing Orchard, Troubleshooting, Writing modules, Writing themes
Mar 27, 2014 at 7:23 PM

when is this needed? trying to understand what it does.
Mar 28, 2014 at 11:06 AM
Edited Mar 28, 2014 at 11:29 AM
You use this when you define multiple features in your module's manifest (Module.txt).
When decorating your classes with [OrchardFeature("NameOfFeature")], only those classes with those attributes where the specified feature is enabled will be registered with AutoFac.

For example, imagine you defined two features in Module.txt:
  • Feature1
  • Feature2
And you have two classes in your module:
  • MyComponent1
  • MyComponent2
With code looking like this:
public interface IMyComponent : IDependency {}

public class MyComponent1 : IMyComponent {}

public class MyComponent2 : IMyComponent {}
Now, if we had a third class that accepts an enumerable of IMyComponent instances, AutoFac will inject instances of all of its implementations it can find, BUT only those implementations whose associated feature name is enabled.
For example, if we enable "Feature1" but disable "Feature2", the constructor of the following class:
public interface IMyService : IDependency {}

public class MyService : IMyService {
   public MyService(IEnumerable<IMyComponent> components) {
      // "components" will only contain instances of IMyComponent whose implementations' attributes' features are enabled.
will receive an enumerable with just an instance of MyComponent1.
If you enable "Feature2", the enumerable will have an instance of MyComponent2 as well.

This is useful because Orchard provides many interfaces and base classes you can implement, but not all of those implementations should be activated for all installations; the user can choose which features are turned on or off. For example, if a user wants to use the Lists feature, she enables that feature. Since that feature name is tied to a migration class, some content handlers and drivers, all of those classes will be registered with AutoFac and hence execute (in a more or less similar way I just showed).

Pretty neat if you ask me.
Mar 28, 2014 at 3:29 PM
I get it now! Thanks for that very complete answer. Love your walk thru on you blog too. Great stuff!