Dependency injection with multiple repositories

后端 未结 4 2097
梦毁少年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:43

    Do your repositories have object-level state? Probably not, so create them as singletons and have a DI container provide them to CACService.

    Otherwise, are they actually expensive to create? If not, creating a new one per request has negligible cost compared to the RPC and database operations.

    Using the Ninject dependency injection container, your CACService might look like the following. Other DI containers have equally succinct mechanisms of doing this.

    public class CACSService
    {
        public CACService
        {
            // need to do this since WCF creates us
            KernelContainer.Inject( this );
        }
    
        [Inject]
        public IUserRepository Repository
        { set; get; }
    
        [Inject]
        public IBusinessRepository BusinessRepository
        { set; get; }
    }
    

    And during your application startup, you would tell Ninject about these types.

    Bind().To().InSingletonScope();
    Bind().To().InSingletonScope();
    

提交回复
热议问题