In my opinion service locator should be hardly visible in project. You should write all your classes as no service locator pattern is used, by using constructor (preferred) or setter dependencies, like below- and let the service locator wire them up together. In my projects I have referenced the service locator primarily in two places-
- In creating the main entry object. e.g. I used it in creating the instance of WCF services. I injected it in to WCF stack using IInstanceProvider and let it create the instances of web services. Rest of my code does not know anything about service locator and I used it in similar styles in other situations (like windows services) just to create the entry object.
- In registering all dependencies with IoC container. Ideally during the the start up of application you will set up the dependencies.
- Expose underlying IoC container using an interface with few methods to support type resolution. It helps the client of service locator to avoid tie-up with specific IoC container.
- On registration side directly use IoC container's API. It is inefficient and overkill to access an IoC container's registration API through your custom interface.
No comments:
Post a Comment