Skip to content

Instantly share code, notes, and snippets.

@hartviglarsen
Created March 6, 2019 19:33
Show Gist options
  • Select an option

  • Save hartviglarsen/02090abeae67e2cb07dd06d6f34d2812 to your computer and use it in GitHub Desktop.

Select an option

Save hartviglarsen/02090abeae67e2cb07dd06d6f34d2812 to your computer and use it in GitHub Desktop.
using System.Web.Mvc;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
namespace UmbracoDI
{
public class MyComposer : IComposer
{
public void Compose(Composition composition)
{
composition.Register<IMyService, MyService>();
}
}
public interface IMyService
{
string GetName();
}
public class MyService : IMyService
{
public string GetName() => "Morten";
}
public class ContentPageController : RenderMvcController
{
private readonly IMyService _myService;
public ContentPageController(IMyService myService)
{
_myService = myService;
}
public ActionResult ContentPage(ContentModel model)
{
var vm = new ContentPageViewModel(model.Content)
{
SomeServiceName = _myService.GetName()
};
return CurrentTemplate(vm);
}
}
public class ContentPageViewModel : ContentModel
{
public ContentPageViewModel(IPublishedContent content) : base(content) { }
public string SomeServiceName { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment