Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tracey Baird 5 posts 95 karma points
    Jan 13, 2022 @ 23:24
    Tracey Baird
    0

    Dependency Injection help with IRecordReaderService

    I'm wanting to retrieve all form entries for a particular form.

    _readerService is null and I'm not sure what I'm missing:

    public class EventBookingSurfaceController : SurfaceController
    {
        protected UmbracoHelper _umbraco;
        public EventBookingSurfaceController(UmbracoHelper umbraco) { _umbraco = umbraco; }
    
        private readonly IRecordReaderService _readerService;
        public EventBookingSurfaceController(IRecordReaderService readerService) { _readerService = readerService; }
    
        public ActionResult GetBookings()
        {
            IPublishedContent pageNode = _umbraco.Content(1227);
    
            if (pageNode is Event thisEvent)
            {
                Guid formId = Guid.Parse(thisEvent.RegistrationForm.ToString());
    
                IEnumerable<IRecord> formRecords = _readerService.GetRecordsFromForm(formId, 1, 1000).Items;
    
                //Work to do here with formRecords
    
                return PartialView("_BookedAttendees", formRecords);
            }
    
            return PartialView("_BookedAttendees");
        }
    
    }
    

    I suspect I'm missing something here with my dependency injection process as "_readerService" is null. Any advice appreciated, Thanks.

  • Kevin Jump 2343 posts 14890 karma points MVP 8x c-trib
    Jan 14, 2022 @ 15:33
    Kevin Jump
    0

    Hi Tracey,

    have you registered your IRecordReaderService in a composer ?

    if you register it via a composer it should be there when the surface controller is initialized.

    probably something like :

    public class MyRecordReaderComposer: IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.RegisterUnique<IRecordReaderService, RecordReaderService>();
        }
    }
    

    assuming you implimentation of the RecordReaderService is called RecordReaderService

    Umbraco will then pick that class up and register your service when umbraco starts up.

  • Tracey Baird 5 posts 95 karma points
    Jan 16, 2022 @ 04:34
    Tracey Baird
    0

    Thanks Kevin, I really appreciate your help.

    I wondered if that was the direction I needed to head. I had assumed this would work in a similar way to UmbracoHelper which didn't require registration in a composer. I've added a composer but get an error "Cannot access internal class 'RecordReaderService' here".

    I'm trying to follow instructions here: https://our.umbraco.com/documentation/Add-ons/umbracoforms/developer/working-with-data/ in order to get submitted form data to process. There isn't really any info on how to implement the RecordReaderService other than "injecting the interface".

    This is definitely something I'm new to, I'm assuming there are more steps to get this working. Do you have any further suggestions?

  • Tracey Baird 5 posts 95 karma points
    Jan 16, 2022 @ 22:56
    Tracey Baird
    100

    Worked this one out - RecordReaderService is already available but I needed to inject it in the same constructor as the UmbracoHelper:

    public EventBookingSurfaceController(UmbracoHelper umbraco, IRecordReaderService recordReaderService)
        {
            _umbraco = umbraco;
            _readerService = recordReaderService;
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft