service-locator

Confused over using IOC container, service locator and factory

為{幸葍}努か 提交于 2020-01-02 06:45:30
问题 Suppose I have a BaseForm which depends on an ILogger or IResourceManager or something like that. Currently it resolves the correct implementation of the required service using the service locator which I know is an anti-pattern. Is using the constructor injection the right way to resolve this kind of dependency? Do I have to register my BaseForm (and its' derived types) in the container in order to create instances of them with resolved dependencies? Doesn't that complicate everything? Is it

Can't use registered singetons in ConfigureServices without them being instantiated twice

China☆狼群 提交于 2020-01-01 14:34:13
问题 I have a .Net Core project that registers a number of Singletons as follows: public void ConfigureServices(IServiceCollection services) { services.AddMemoryCache(); services.AddLogging(); services.AddSingleton<IConfiguration>(Configuration); services.AddSingleton<IDbFactory, DefaultDbFactory>(); services.AddSingleton<IUserRepository, UserRepository>(); services.AddSingleton<IEmailService, EmailService>(); services.AddSingleton<IHostedService, BackgroundService>(); services.AddSingleton

What is the actual difference betwen a service locatior and a dependency injection?

家住魔仙堡 提交于 2019-12-31 06:20:08
问题 I was going through the previous discussion in which there was a detailed discussion on the difference between a service locator and a dependency injector, but still I am not able to get that. Can I get a general response without any code? 回答1: This code sample applies the Dependency Injection principle: public class UserService : IUserService { private IUserRepository repository; // Constructor taking dependencies public UserService(IUserRepository repository) { this.repository = repository;

How can I set up Lazy Loading with ZF3 (no ServiceLocator pattern from anywhere)

 ̄綄美尐妖づ 提交于 2019-12-23 12:59:54
问题 I am writing a new ZF2 app. I have noticed that ServiceLocator usage pattern of calling services "from anywhere" has been deprecated from ZF3. I want to write code in mind for ZF3. I was able to set up my Controller to call all dependencies at constructor time. But that means loading i.e. Doctrine object upfront before I need it. Question How do I set it up so that it is only loaded when I need it immediately? (lazy-loaded). I understand that ZF3 moves loading to Controller construction,

Factory pattern without service locator

无人久伴 提交于 2019-12-22 13:05:44
问题 I am currently stuck at trying to write a factory class that doesn't rely on service location. The only other alternative I can think of is to use constructor injection to inject all possible instances, but that may lead to surprises as classes are passed via reference. It is also possibly going to be costly and messy once the number of possible providers grow. The providers themselves are full complex classes that have their own dependencies so manual construction is out of the picture.

Why the Service Locator is a Anti-Pattern in the following example?

时光毁灭记忆、已成空白 提交于 2019-12-21 05:16:22
问题 I have a MVC application with a Domain Model well defined, with entities, repositories and a service layer. To avoid having to instantiate my service classes inside my controllers, and thus, mess my controllers with logic that does not suit they, I created a helper that acts as a sort of Service Locator, but after reading a bit, I realized that many devs: http://blog.tfnico.com/2011/04/dreaded-service-locator-pattern.html http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx http

How to combine designable components with dependency injection

感情迁移 提交于 2019-12-21 04:07:05
问题 When creating a designable .NET component, you are required to provide a default constructor. From the IComponent documentation: To be a component, a class must implement the IComponent interface and provide a basic constructor that requires no parameters or a single parameter of type IContainer. This makes it impossible to do dependency injection via constructor arguments. (Extra constructors could be provided, but the designer would ignore them.) Some alternatives we're considering: Service

Which pattern to use for logging? Dependency Injection or Service Locator?

拥有回忆 提交于 2019-12-20 08:56:33
问题 Consider this scenario. I have some business logic that now and then will be required to write to a log. interface ILogger { void Log(string stuff); } interface IDependency { string GetInfo(); } class MyBusinessObject { private IDependency _dependency; public MyBusinessObject(IDependency dependency) { _dependency = dependency; } public string DoSomething(string input) { // Process input var info = _dependency.GetInfo(); var intermediateResult = PerformInterestingStuff(input, info); if

@EJB injection vs lookup - performance issue

耗尽温柔 提交于 2019-12-19 19:49:31
问题 I have a question related with possible performance issue while using @EJB annotation. Imagine following scenario public class MyBean1 implements MyBean1Remote{ @EJB private MyBean2Remote myBean2; @EJB private MyBean2Remote myBean3; ... @EJB private MyBean20Remote myBean20; } There is a bean with many dependencies to other beans. According to EJB spec if I would like to inject MyBean1Remote to some other bean, container would have to take all required dependencies from its pool inject it into

Dependency management in Zend Framework 2 MVC applications

陌路散爱 提交于 2019-12-19 09:15:36
问题 As the ServiceLocatorAwareInterface will likely be removed from the AbstractController in ZF3, dependencies should instead be passed via the constructor or via setter methods. With this in mind, consider the use case of a user or site controller with actions such as register, activate account, login, logout, etc. At a minimum, this would require a UserService and 2 forms. Add a few more related actions (remote authentication, linking of accounts, etc.) and you end up with 4 or 5 forms.