Copied to clipboard

Flag this post as spam?

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


  • Jannik Anker 48 posts 258 karma points c-trib
    Jul 15, 2021 @ 12:31
    Jannik Anker
    1

    Custom database CRUD (or, where is my IScopeProvider?)

    Ahoy-hoy people!

    I'm working with v9 RC1, trying to create repositories/services for my custom tables (created via migrations), and I thought it would be pretty much same procedure as last version, but... the docs say there should be an IScopeProvider but it's nowhere to be found? I've even checked the Umbraco source and it's not there.

    If the scope provider is gone for good, I'm not sure how I would get to work with the database :-(

    Got any pointers?

  • Jannik Anker 48 posts 258 karma points c-trib
    Jul 15, 2021 @ 12:49
    Jannik Anker
    100

    Well, color me surprised :-D

    My repositories were placed in a separate project which only had the Umbraco.Cms.Core package installed. No IScopeProvider there, in spite of it belonging to the Umbraco.Cms.Core namespace.

    However, I tried to find it in my website project, and there it is! Turns out I need the Umbraco.Cms.Infrastructure package on top of Umbraco.Cms.Core in my separate project.

    Kinda sucks that the namespaces seem to be all over the place now :-(

  • Harry Spyrou 212 posts 604 karma points
    Jul 15, 2021 @ 13:37
    Harry Spyrou
    0

    Someone else more experienced can correct me if I'm wrong, but it seems in line with how .NET Core is built. Less things are in .Core packages now and if you need more, you can add a package and use it in your code.

    I think Microsoft does the same thing with .NET 5.

    E.g. you have a Microsoft.Azure.Storage package and then if you need to use something that's not part of the Core, you install Microsoft.Azure.Storage.Extensions or something of the sort.

  • Jannik Anker 48 posts 258 karma points c-trib
    Jul 15, 2021 @ 15:20
    Jannik Anker
    1

    Yeah, you're right, and I'm not really opposing that way of doing things. What bothers me is that the Umbraco.Cms.Core namespace is being used all over the place, meaning that I have to install Umbraco.Cms.Infrastructure to get access to functionality within the Umbraco.Cms.Core namespace. That (to me) makes it very difficult to know what is where, and why. Might just be me.

  • john blair 48 posts 219 karma points
    Jul 26, 2023 @ 16:55
    john blair
    0

    In case it helps anyone with Umbraco 11. I had the same problem and ended up injecting the repository into the api controller.

    [PluginController("globaltrade")]
    [JsonCamelCaseFormatter]
    public class BooksApiController : UmbracoAuthorizedJsonController, IBooksRepository
    {
        private IBooksRepository booksRepository;
        private ILogger log;
        public BooksApiController( ILogger log, IBooksRepository booksRepository)
    

    The repo has the ISCopeProvider injected and needs the Umbraco.Cms.Infrastructure package installed.

    using Umbraco.Cms.Infrastructure.Scoping;
    
    namespace JB.Backoffice.GlobalTrade
    {
        public class BooksRepository :  IBooksRepository
        {
            private IScopeProvider _scopeProvider;
            public BooksRepository(IScopeProvider scopeProvider)
            {
                _scopeProvider= scopeProvider;
            }
    

    To make this work you need to add the repo into the DI container during services configuration in the Startup.cs file i.e. AddTransient

     public void ConfigureServices(IServiceCollection services)
        {
            // Strongly typed configuration added to the DI container as IOptions<EnvironmentOptions> etc.
            services.Configure<EnvironmentOptions>(_config.GetSection(EnvironmentOptions.Environment));
            services.Configure<EnvironmentSecretOptions>(_config.GetSection(EnvironmentSecretOptions.EnvironmentSecret));
    
            // Restore HTTPContext.Current
            services.AddHttpContextAccessor();
    
            services.AddTransient<IBooksRepository, BooksRepository>();
    

    Finally to return POCO object which are pascal case in C# as native javascript JSON objects with camel case add the [JsonCamelCaseFormatter] attribute to the api controller class.

Please Sign in or register to post replies

Write your reply to:

Draft