Open Closed

Repository Injection #173

pnw created

I have a class that isn't an ApplicationService but still needs to use one of my repositories. I can't have a constructor so I figured to use manual resolving.

public class Q : ISingletonDependency
        public IThingRepository thingRepository { get; set; }

        public Q()
            this.thingRepository = IocManager.Instance.Resolve<IThingRepository>();

        public List<Thing> GetDbThings(List<Thing> claims, long uid)
            // look up more things in the database
            var thingQ = from t in thingRepository.GetAll()
                        where t.UserID == uid
                        select new UOR { };

            foreach (var t in thingQ)
                // do things with t

            return newlistofthings;

The problem is that when I use thingRepository, I get the following exception:

The operation cannot be completed because the DbContext has been disposed

There must be something simple I'm missing?

1 Answer(s)
  • 0
    hikalkan created
    Support Team

    Just make GetDbThings method as virtual in order to make UOW work. See docs: <a class="postlink" href=""> ... strictions</a>

    But you should get reference of Q from IocManager. Whe you need to Q, always use IocManager.Instance.Resolve<Q> or inject it (this is better). In this way, you can also inject repositories into Q.