Use IBlogService (or any other service) in Orchard module

I am trying to create a controller that fetches specific blogposts from a blog. I want top 20 posts and top 20 posts.

For this, I created a controller that will retrieve this information. After going to the Orchard standard blog module, I see that I need an IBlogService or an IBlogPostService. I see that they are injected into the BlogPostController, for example:

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

      

But how do these services get hooked up / hooked up / injected? I can't find the piece of code where the constructor is called, and I can't find some wiring like I'm used to in StructureMap.

Can I just add the Iservices to the constructor and will Orchard make sure I have the objects I want, or do I need to do something earlier?

At the moment my class looks like this (by 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();
    }
}

      

+3


source to share


1 answer


Orchard uses dependent injection through inversion control using a library called AutoFac . It sounds like a gulp, but it really isn't. Basically, you specify the required services in the parameters of the constructor, and AutoFac automatically solves them and calls the constructor with instances of the classes that implement the interface you specified.

You will already inject 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 a blogging service, 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 method, Index

you can just start using _blogs

and _posts

to do blogging related operations.

+5


source







All Articles