Property Injection and/or Adding a Controller to an Alternate MenuItem View

Topics: Customizing Orchard, Troubleshooting, Writing modules
Feb 1, 2013 at 6:57 PM
Edited Feb 10, 2013 at 11:45 AM
Hi all,

I'm trying (and failing) to get property injection working in Orchard CMS.

This is necessary, because the below code is acting something like a code-behind for a view (horrible I know).

The reason being that the view doesn't have a controller that I can use constructor injection on, because this is an alternate view for a MenuItem, i.e. MenuItemLink-MyCustomMenuItem.cshtml.

Not much more to say than that, except what's in the comments (note the NULL comment for the property that I am trying to set in the code below).

Oh, I've also tried adapting the code from Orchard's LoggingModule for DI, but equally that doesn't work for me either.

How do I :
a. get the below property injection to work? (I'm pretty sure I will need it at some point regardless)
b. (if possible) get my own controller in the way of the view so I can use constructor injection on the controller instead?
using System.Diagnostics;
using System.Xml.Linq;
using Autofac;
using Autofac.Core;
using Orchard;
using Module = Autofac.Module;

namespace MyCo.MyCustomMenuItem.Services 
{

    public class MyCustomMenuItemModule : Module 
    {

        protected override void AttachToComponentRegistration(
            IComponentRegistry componentRegistry, 
            IComponentRegistration registration)
        {
            if (implementationType.ToString() == 
                        "MyCo.MyCustomMenuItem.Services.MyCustomMenuItem") 
            {
                // this does get called, but doesn't have the desired effect
                registration.Activated += (s, e) => e.Context.InjectUnsetProperties(e);
            }
        }

    }

    public interface IFeedX : IDependency {
        XDocument GetResource();
    }
    
    public class FeedX : IFeedX
    {
        public XDocument GetResource() 
        {
            return new XDocument();
        }
    }

    public interface IMyCustomMenuItem : IDependency 
    {
        XDocument GetResourceData();

        IFeedX FeedX { get; set; }
    }   
    
    public class MyCustomMenuItem : IMyCustomMenuItem
    {
        public IFeedX FeedX { get; set; }
        
        // called by view
        public XDocument GetResourceData()
        {
            Debug.WriteLine(FeedX); // NULL!!???

            return FeedX.GetResource(); 
        }

    }

}
Coordinator
Feb 10, 2013 at 8:38 AM
Didn't I answer that on StackOverflow?
Feb 10, 2013 at 11:39 AM
Edited Feb 10, 2013 at 11:44 AM
Yep, sorry, I only posted on stackoverflow after not getting a response here. I guess I was too impatient!

Here is the stackoverflow link for anyone that wants to follow this, or can help (still struggling with this atm): StackOverflow Link

Links to property injection in Orchard are here: Link A, Link B