service-locator

Service Locator pattern in Swift

社会主义新天地 提交于 2019-12-07 00:10:28
问题 I'm interested in a flexible universal Service Locator design pattern implementation in Swift. A naive approach may be as follows: // Services declaration protocol S1 { func f1() -> String } protocol S2 { func f2() -> String } // Service Locator declaration // Type-safe and completely rigid. protocol ServiceLocator { var s1: S1? { get } var s2: S2? { get } } final class NaiveServiceLocator: ServiceLocator { var s1: S1? var s2: S2? } // Services imlementation class S1Impl: S1 { func f1() ->

MVC3, Ninject, MvcSiteMapProvider - How to inject dependency to overridden method

孤街醉人 提交于 2019-12-07 00:08:13
问题 I have an MVC3 application that is using Ninject and MvcSiteMapProvider. I have created this class which MvcSiteMapProvider uses to dynamically add nodes to my sitemap: public class PageNodeProvider : DynamicNodeProviderBase { public override IEnumerable<DynamicNode> GetDynamicNodeCollection() { // need to get repository instance var repository = // how do I get this??? foreach (var item in repository.GetItems()) { yield return MakeDynamicNode(item); } } } The MvcSiteMapProvider instantiates

How can I decouple my application from my membership service?

最后都变了- 提交于 2019-12-06 15:43:34
I'm working on an ASP.NET MVC 4 project that uses Castle Windsor. One of the controllers has a dependency on MembershipService: public class FooController : Controller { private readonly MembershipService MembershipService; public FooController( MembershipService membershipService ) { MembershipService = membershipService; } [Authorize( Roles = "Administrator" )] public ActionResult DoSomething() { var user = MembershipService.GetUser( User.Identity.Name ); // etc... } } When I started writing unit tests for this controller (using Moq), I ran into a problem: private Mock<MembershipService>

Service Locator pattern in Swift

删除回忆录丶 提交于 2019-12-05 04:43:39
I'm interested in a flexible universal Service Locator design pattern implementation in Swift. A naive approach may be as follows: // Services declaration protocol S1 { func f1() -> String } protocol S2 { func f2() -> String } // Service Locator declaration // Type-safe and completely rigid. protocol ServiceLocator { var s1: S1? { get } var s2: S2? { get } } final class NaiveServiceLocator: ServiceLocator { var s1: S1? var s2: S2? } // Services imlementation class S1Impl: S1 { func f1() -> String { return "S1 OK" } } class S2Impl: S2 { func f2() -> String { return "S2 OK" } } // Service

MVC3, Ninject, MvcSiteMapProvider - How to inject dependency to overridden method

做~自己de王妃 提交于 2019-12-05 04:25:07
I have an MVC3 application that is using Ninject and MvcSiteMapProvider . I have created this class which MvcSiteMapProvider uses to dynamically add nodes to my sitemap: public class PageNodeProvider : DynamicNodeProviderBase { public override IEnumerable<DynamicNode> GetDynamicNodeCollection() { // need to get repository instance var repository = // how do I get this??? foreach (var item in repository.GetItems()) { yield return MakeDynamicNode(item); } } } The MvcSiteMapProvider instantiates this type itself, so I'm not sure how to inject my repository into it. I thought about using service

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

人走茶凉 提交于 2019-12-04 13:24:20
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<ISettingsRepository, SettingsRepository>(); services.AddSingleton(typeof(TokenManager)); var sp = services

What if Dependency Injection is not possible?

吃可爱长大的小学妹 提交于 2019-12-04 12:22:38
问题 After much kicking and screaming, I'm starting to accept DI despite how much cleaner SL may seem as dependencies grow. However, IMO there's still a significant show-stopper with regards to DI: DI is not possible when you don't have control over an object's instantiation. In the ASP.NET world, examples include: HttpModule, HttpHandler, Page, etc. In the above scenario we would resort to static service location to resolve dependencies, typically via HttpContext.Current , which invariably infers

Spring ServiceLocator or pure factory pattern?

一世执手 提交于 2019-12-04 09:15:31
问题 99% of my dependency is manage with DI pattern via @Autowired Spring annotation. Nevertheless in a particular scenario, I can't determine which implementation to be used until run-time. The most known case, is the multiple implementation of parsers. The first solution is to used multiple @Autowired (ugly mode) Interface Parser { <T> T parse(); } @Component("JsonParser") class JsonParser implements Parser { ... } @Component("XmlParser") class XmlParser implements Parser { ... } class MyService

Service locator for generics

房东的猫 提交于 2019-12-04 05:01:13
问题 I have say a dozen types T which inherit from EntityObject and IDataObject . I have generic the following interface IDataManager<T> where T : EntityObject, IDataObject ... I have also base class for data managers BaseDataManager<T> : IDataManager<T> where T : EntityObject, IDataObject .... And i have particular classes public class Result : EntityObject, IDataObject .... public class ResultDataManager : BaseDataManager<Result> ... I want to implement service locator, which will return

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

▼魔方 西西 提交于 2019-12-03 16:35:39
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://underground.infovark.com/2010/06/18/the-service-locator-pattern-is-the-new-global-variable/ http:/