Copied to clipboard

Flag this post as spam?

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


  • kapil 21 posts 114 karma points
    Apr 30, 2019 @ 18:01
    kapil
    0

    Register dbContext through composition

    I want to use the Microsoft.EntityFrameworkCore.dbContext into the controller.

    I would really appreciate if I can get to know how we can register dependency for that in:

    public class Global : UmbracoApplication, IComposer
        {
            public void Compose(Composition composition)
            {
                composition.Register<IRepository1, Repository1>();
                // want to register dbContext here with so that I can use it like mentioned in section 2
    
            }
    

    Section2

      public class Repository1 : IRepository1
        {
            private SomeDbContext _context;
    
            public Repository1(SomeDbContext dbContext)
            {
                _context = dbContext;
            }
    }
    
  • Corné Strijkert 80 posts 456 karma points c-trib
    May 01, 2019 @ 10:12
    Corné Strijkert
    0

    Hi kapil,

    That should not be that difficult. Give it a try with:

    composition.Register(typeof(SomeDbContext));
    

    or

    composition.Register<SomeDbContext>(factory => new SomeDbContext(), Lifetime.Scope);
    

    in your component.

    Instead of implementing the IComposer interface you should implement the IUserComposer interface.

    It is good to know that Umbraco 8 uses LightInject for DI, so you can search the www for specific scenarios with LightInject. https://www.lightinject.net/

    You can also take a look at https://github.com/umbraco/Umbraco-CMS/blob/977bf91409a7965519c0504228cc68b11c6ca5c6/src/Umbraco.Web/Runtime/WebInitialComposer.cs to see different ways you can register stuff.

  • kapil 21 posts 114 karma points
    May 01, 2019 @ 20:34
    kapil
    1

    Thanks for your reply Corne. factory option worked with the combination of passing new dbContextOption builder

  • 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