Dependency injection with multiple repositories

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

    The beauty of pure DI is that you shouldn't worry about the lifetimes of your dependencies, because these are managed for you by whoever supply them (a DI Container, or some other code you wrote yourself).

    (As an aside, you should get rid of your current Bastard Injection constructors. Throw away the parameterless constructor and keep the one that explicitly advertises its dependencies.)

    Keep your constructor like this, and use _IRepository and _IBusinessRepository as needed:

    public CACSService(IUserRepository Repository, IBusinessRepository businessRepository) 
    { 
        _IRepository = Repository; 
        _IBusinessRepository = businessRepository; 
    } 
    

    If you worry that one of these repositories are not going to be needed at run-time, you can inject a lazy-loading implementation of, say, IUserRepsository instead of the real one you originally had in mind.

    Let's assume that IUserRepository looks like this:

    public interface IUserRepository
    {
        IUser SelectUser(int userId);
    }
    

    You can now implement a lazy-loading implementation like this:

    public class LazyUserRepository : IUserRepository
    {
        private IUserRepository uRep;
    
        public IUser SelectUser(int userId)
        {
            if (this.uRep == null)
            {
                this.uRep = new UserRepository();
            }
            return this.uRep.SelectUser(userId);
        }
    }
    

    When you create CACService, you can do so by injecting LazyUserRepository into it, which ensures that the real UserRepository is only going to be initialized if it's needed.

    The beauty of this approach is that you don't have to do this until you need it. Often, this really won't be necessary so it's nice to be able to defer such optimizations until they are actually necessary.

    I first described the technique of Lazy Dependencies here and here.

    0 讨论(0)
  • 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<IUserRepository>().To<UserRepository>().InSingletonScope();
    Bind<IBusinessRepository>().To<BusinessRepository>().InSingletonScope();
    
    0 讨论(0)
  • 2021-02-06 10:46

    Preface: This is a general guide to dependency inversion. If you need the default constructor to do the work (e.g. if it is new'ed up by reflection or something else), then it'll be harder to do this cleanly.

    If you want to make your application configurable, it means being able to vary how your object graph is constructed. In really simple terms, if you want to vary an implementation of something (e.g. sometimes you want an instance of UserRepository, other times you want an instance of MemoryUserRepository), then the type that uses the implementation (CACService in this case) should not be charged with newing it up. Each use of new binds you to a specific implementation. Misko has written some nice articles about this point.

    The dependency inversion principle is often called "parametrise from above", as each concrete type receives its (already instantiated) dependencies from the caller.

    To put this into practice, move the object creation code out of the CACService's parameterless constructor and put it in a factory, instead.

    You can then choose to wire up things differently based on things like:

    • reading in a configuration file
    • passing in arguments to the factory
    • creating a different type of factory

    Separating types into two categories (types that create things and types that do things) is a powerful technique.

    E.g. here's one relatively simple way of doing it using a factory interface -- we simply new up whichever factory is appropriate for our needs and call its Create method. We use a Dependency Injection container (Autofac) to do this stuff at work, but it may be overkill for your needs.

    public interface ICACServiceFactory
    {
        CACService Create();
    }
    
    // A factory responsible for creating a 'real' version
    public class RemoteCACServiceFactory : ICACServiceFactory
    {
        public CACService Create()
        {
             return new CACService(new UserRepository(), new BusinessRepository());
        }        
    }
    
    // Returns a service configuration for local runs & unit testing
    public class LocalCACServiceFactory : ICACServiceFactory
    {
        public CACService Create()
        {
             return new CACService(
                   new MemoryUserRepository(), 
                   new MemoryBusinessRepository());
        }     
    }
    
    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
提交回复
热议问题