Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the IBlogService (or any other service) in an Orchard module

Tags:

orchardcms

I'm trying to create a controller which retrieves certain blogposts from the blog. I want to retrieve the top 20 posts & the 20 top rated posts.

For this I've created a controller which will retrieve this information. After digging into the standard Orchard blog module I see I need the IBlogService or the IBlogPostService. I can see these are injected in the BlogPostController, like so:

public BlogPostController(
            IOrchardServices services, 
            IBlogService blogService, 
            IBlogPostService blogPostService,
            IFeedManager feedManager,
            IShapeFactory shapeFactory) 

But how are those services wired/connected/injected? I can't find the piece of code where the constructor is called, neither can I find some wiring like I'm used to in StructureMap.

Can I just add Iservices in the constructor and will Orchard make sure I've got the right objects, or do I need to do something before?

At the moment my class looks like this (default):

public class FrontpageController : Controller
{
    public IOrchardServices Services { get; set; }

    public FrontpageController(IOrchardServices services)
    {
        Services = services;
        T = NullLocalizer.Instance;
    }

    public Localizer T { get; set; }

    [HttpGet]
    public ActionResult Index()
    {
        //Do something to get blogposts

        throw new NotImplementedException();
    }
}
like image 769
Jan_V Avatar asked Dec 08 '25 21:12

Jan_V


1 Answers

Orchard uses dependeny injection via inversion-of-control, using a library called AutoFac. Sounds like a mouthful, but it really isn't. Essentially you specify the services you require in the constructor's parameters, and AutoFac automagically resolves them and calls the constructor with instances of classes that implement the interface you specify.

You are already injecting IOrchardServices into your controller, so you can do the same with any other class/interface that implements IDependency. (IBlogPostService and IBlogService both inherit from IDependency)

To do the same with the blogs service, then you can do the following:

public class FrontpageController : Controller
{
    public IOrchardServices Services { get; set; }
    private readonly IBlogService _blogs;
    private readonly IBlogPostService _posts;

    public FrontpageController(IOrchardServices services, IBlogService blogs, IBlogPostService posts)
    {
        Services = services;
        T = NullLocalizer.Instance;
        _posts = posts;
        _blogs = blogs;
    }

    public Localizer T { get; set; }

    [HttpGet]
    public ActionResult Index()
    {
        //Do something to get blogposts

        throw new NotImplementedException();
    }
}

Then in your Index method you can just start using _blogs and _posts to perform blog-related operations.

like image 146
mdm Avatar answered Dec 10 '25 11:12

mdm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!