Copied to clipboard

Flag this post as spam?

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


  • john blair 42 posts 209 karma points
    Sep 03, 2019 @ 15:23
    john blair
    1

    IScopeProvider is polluting my application - help

    I've migrated my plugin from v7 to v8 of umbraco. One of the really annoying things that I had to update my repositories to inject an IScopeProvider to access the database.

    e.g.

    public BooksRepository(IScopeProvider scopeProvider)
        {
            this.scopeProvider = scopeProvider;
        }
    

    Now, this was fine within my backoffice plugin as the umbraco injector would provide the scopeprovider when required.

    However, I also want to use the repository in my front end razor templates - which now need a scopeProvider and there is no way to inject it.

    @{
    EcBooksSection booksection = Model;
    
    Html.RequiresCss("~/styles/css/booksSection.css");
    Html.RequiresJs("~/scripts/ec/booksSection.js");
    var booksRepository = new BooksRepository( scopeProvider  required - WTF?);
    var books = booksRepository.GetAll();
    

    The real problem is I don't want to inject this scopeProvider - this is umbraco framework noise that I don't want to deal with in my application - why can't a static instance of the Database be available like it was in v7 that could just be used internally within the repository and not poluute my application?

    Also why am i having to work with a scopeprovider - assuming its transaction related - as I don't need a transaction when I do read only operations from the database? Data access seems very badly designed in v8.

    Given, if I have to work with this scopeProvider how do i make this available in my razor templates in .net framework v4.7.2? Note: This is not .net core so the @inject directive is not available.

    Thanks.

  • john blair 42 posts 209 karma points
    Sep 03, 2019 @ 15:53
    john blair
    101

    I decided to refactor my repository to avoid exposing IScopeProvider on my front end templates.

    I now use the following to get an instance of the Database

     public Book Create(Book item)
        {
            using (var scope = Current.ScopeProvider.CreateScope(autoComplete: true))
            {
                scope.Database.Insert(item);
            }
            return item;
        }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies