Open Closed

Repository Injection #173


0
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>();
        }

        [UnitOfWork]
        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 { ...properties... };

            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="http://www.aspnetboilerplate.com/Pages/Documents/Unit-Of-Work#DocUowRestrictions">http://www.aspnetboilerplate.com/Pages/ ... 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.