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();
}
}
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
IOrchardServicesinto your controller, so you can do the same with any other class/interface that implementsIDependency. (IBlogPostServiceandIBlogServiceboth inherit fromIDependency)To do the same with the blogs service, then you can do the following:
Then in your
Indexmethod you can just start using_blogsand_poststo perform blog-related operations.