Dependency injection with multiple repositories

后端 未结 4 2096
梦毁少年i
梦毁少年i 2021-02-06 10:13

I have a wcf service and on the client i have:

var service = new ServiceReference1.CACSServiceClient()

The actual service code is:

         


        
4条回答
  •  暖寄归人
    2021-02-06 10:47

    Instead of instantiating ("newing up") the repositories on construction, you could lazy load them in their properties. This would allow you to keep your second constructor, but have your first constructor do nothing.

    The user could then assign these, as needed, otherwise.

    For example:

    public class CACSService
    {
        public CACSService() {}
    
        public CACSService(IUserRepository Repository, IBusinessRepository businessRepository)
        {
            _IRepository = Repository;
            _IBusinessRepository = businessRepository;
        }
    
        private IUserRepository _IRepository;
        public IUserRepository Repository
        {
            get {
                 if (this._IRepository == null)
                      this._IRepository = new UserRepository();
                 return this._IRepository;
            }
        }
    
       // Add same for IBusinessRepository
    }
    

提交回复
热议问题