Created
March 6, 2019 19:33
-
-
Save hartviglarsen/02090abeae67e2cb07dd06d6f34d2812 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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