Using the same parameter value depending on Windsor

I'm not sure if this can be done and it might be necessary to rethink the whole thing, however I thought I would ask to do this.

Ok I have a repository class with the following constructor

    public Repository(ISessionManager sessionManager, string dbName)
    {
        this.sessionManager = sessionManager;
        this.dbName = dbName;
    }

      

Currently, anywhere in an application that needs to use a repository, it uses dependency injection and the repository is registered with the container as follows.

Component.For<IRepository<User, int>>().ImplementedBy<Repository<User, int>>()
                     .DependsOn(Dependency.OnValue("dbName", "admin")),

      

What I want to do is set the value of the database name to the level for the services that the repositories will use.

For example, having a service with a constructor

public SecurityService(ITransactionFactory transactionFactory,
                       IRepository<User, int> userRepository,
                       string dbName)

      

I want the dbName argument to be registered in the container with the ServiceService and pass that value when I enter the userRepository argument.

So, if another service needs to use the same repository with a different database, then when it is registered a new name is provided.

Is it possible?

+3


source to share


1 answer


I don't know that I know exactly what you are looking for, but using named registrations may lead you to the same end goal in a way that slightly more Windsor is "right".

For example, the following two registrations can be in tandem:

Component.For<IRepository<User, int>>()
         .ImplementedBy<Repository<User, int>>()
         .DependsOn(Dependency.OnValue("dbName", "admin")
         .Named("adminRepositoryRegistration")

Component.For<IRepository<User, int>>()
         .ImplementedBy<Repository<User, int>>()
         .DependsOn(Dependency.OnValue("dbName", "user")
         .Named("userRepositoryRegistration")

      

You can then plug in your services to depend on specific Windsor registrations. For example, this will be related to the first registration:



Component.For<IUserService>()
         .ImplementedBy<UserService>()
         .DependsOn(Dependency.OnComponent("userRepository", "userRepositoryRegistration")

      

and this will bind to the second one:

Component.For<ISecurityService>()
         .ImplementedBy<SecurityService>()
         .DependsOn(Dependency.OnComponent("adminRepository", "adminRepositoryRegistration")

      

Overall, I feel like it makes the solution a bit easier as it handles all of the dependency settings in the DI registration, instead of leaving loose ends to wrap around in actual services, and allows you to route dependencies as needed.

0


source







All Articles