Manually Resolve IOrchardServices for Bundle Config

Topics: Core, Customizing Orchard, General, Writing themes
Aug 20, 2012 at 8:22 AM

Hi I am just trying to find out how would I manually resolve IOrchardServices so I can call:

var themePath = services.WorkContext.CurrentTheme.Path;

to then register bundles for bundling and minification?

 

I don't want to hardcode the theme's path if i can help it..

 

Sorry I've never used autofac and the docs haven't got an example I could find.

Cheers,

Tom

Aug 20, 2012 at 10:33 PM
Edited Aug 20, 2012 at 10:34 PM

What do you mean manually?

If you just add it as a parameter in your constructor, you should get an instance....

public class Something : IDependency {
   public Something(IOrchardServices services){
        //etc.
   }
}
Aug 22, 2012 at 12:03 AM
Thanks so much for the reply.. I was wondering how do i then create a new Something()

do I have to use DependencyResolver.Resolve?

basically I'm trying to create some bundles for styling

namespace Orchard.Web
{
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
//var services = container.Resolve<IOrchardServices>();

//var themePath = orchardService.WorkContext.CurrentTheme.Path;
bundles.Add(new StyleBundle("~/Content/css").Include(themePath + "/Styles/bootstrap.css", themePath + " /Styles/responsive.css"));
}
}
}

of course I'd string.Format and get things neat but just wanted to illustrate the concept..

Cheers,
Tom


On Wed, Aug 22, 2012 at 7:16 AM, BrandonJoyce <notifications@codeplex.com> wrote:

From: BrandonJoyce

What do you mean manually?

If you just add it as a parameter in your constructor, you should get an instance....

public class Something : IDependency {
   public Something(IOrchardServices services){
        //etc.
   }
}

Read the full discussion online.

To add a post to this discussion, reply to this email (orchard@discussions.codeplex.com)

To start a new discussion for this project, email orchard@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Coordinator
Aug 22, 2012 at 1:11 AM

You don't. You get them injected as a constructor parameter.

Aug 22, 2012 at 3:04 AM
So then in app start if i want to register my bundles how would that look.. currently im using:
BundleConfig.RegisterBundles(BundleTable.Bundles);

IOrchardService would need to be a static field in order to reuse it in my RegisterBundles method

On Wed, Aug 22, 2012 at 10:11 AM, bertrandleroy <notifications@codeplex.com> wrote:

From: bertrandleroy

You don't. You get them injected as a constructor parameter.

Read the full discussion online.

To add a post to this discussion, reply to this email (orchard@discussions.codeplex.com)

To start a new discussion for this project, email orchard@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Coordinator
Aug 22, 2012 at 3:27 AM

You don't have access to app start. What are bundles?

Aug 22, 2012 at 3:30 AM
mvc4 uses them but you can get them working in earlier versions.. they allow bundling/grouping and minification of scripts, css files etc so you only serve one file for all your scripts and one for your css

really nice stuff

On Wed, Aug 22, 2012 at 12:27 PM, bertrandleroy <notifications@codeplex.com> wrote:

From: bertrandleroy

You don't have access to app start. What are bundles?

Read the full discussion online.

To add a post to this discussion, reply to this email (orchard@discussions.codeplex.com)

To start a new discussion for this project, email orchard@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Aug 22, 2012 at 3:39 AM

Maybe the combinator module will solve your issue?  http://combinator.codeplex.com/

Coordinator
Aug 22, 2012 at 3:39 AM

oh, that? Yes, that won't work currently with Orchard. At least, I don't think it can. There is a module on the gallery for that however: http://gallery.orchardproject.net/List/Modules/Orchard.Module.Piedone.Combinator

Aug 22, 2012 at 3:44 AM
I have it working with orchard if I hardcode the theme path it works beautifully with dotless and everything.. my main issue is just working out how i could make the theme path dynamic by using the IOrchardServices instance

On Wed, Aug 22, 2012 at 12:40 PM, bertrandleroy <notifications@codeplex.com> wrote:

From: bertrandleroy

oh, that? Yes, that won't work currently with Orchard. At least, I don't think it can. There is a module on the gallery for that however: http://gallery.orchardproject.net/List/Modules/Orchard.Module.Piedone.Combinator

Read the full discussion online.

To add a post to this discussion, reply to this email (orchard@discussions.codeplex.com)

To start a new discussion for this project, email orchard@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Coordinator
Aug 22, 2012 at 3:52 AM

Yes, that's the issue and why I said I don't think you can make it work with the current Orchard because it probably relies on setting-up global stuff and doing so very early, which a module can't do. Then again I've never looked at the details. The combinator module seems to be the way to go for now.

Jan 9, 2013 at 1:27 PM
Edited Jan 9, 2013 at 1:30 PM

You could use the IOrchardShellEvents interface (this is one way to do things during orchard startup)... create a BundlesConfig.cs file inside your module, reference your System.Web.Optimization.dll which is not included in Orchard out of the box, and add the following code:

 

using JetBrains.Annotations;
using Orchard.Environment;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;

namespace Orchard.jQuery.Ias
{
    [UsedImplicitly]
    public class BundlesConfig : IOrchardShellEvents
    {
        public Startup()
        {
            // this is called more than once
        }

        void IOrchardShellEvents.Activated()
        {
            BundleCollection bundles = BundleTable.Bundles;
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));
        }

        void IOrchardShellEvents.Terminating()
        {

        }
    }
}