Tag: service locator

LightInject IoC容器在解析类型时抛出stackoverflow

在尝试LightInject IoC容器http://www.lightinject.net/时,它会在解析ISomeService类型时抛出stackoverflowexception: 所有类型都在App_Start中注册: container.RegisterAssembly(“MyApp*.dll”); 然后,当我尝试在控制器中解决它时,它会失败并抛出stackoverflowexception: public SomeController(ISomeService someService) { _someService = someService; } 使用ServiceLocator时也会出现相同的错误: ServiceLocator.Current.GetInstance(); 我已经跟踪了它,我可以看到它在LightInject ServiceContainer类中失败了,但是我不明白为什么它失败了。 public object GetInstance(Type serviceType) { return GetDefaultDelegate(serviceType, true)(constants.Items); } 在调用GetDefaultDelegate之后,执行路径再次在GetInstance中结束,导致无限循环和堆栈溢出。 编辑2 – 已进一步跟踪此问题,它似乎是由SomeService同时具有构造函数和属性注入引起的: public class SomeService : ISomeService { public IAnotherService AnotherService { get; set; } public SomeService(IAnotherService anotherService) { AnotherService = anotherService; } } 依赖IAnotherService AnotherService是通过构造函数和属性注入的,但是无意使用属性注入器。 编辑3 […]

如何将我的申请与会员服务分开?

我正在使用Castle Windsor的ASP.NET MVC 4项目。 其中一个控制器依赖于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… } } 当我开始为这个控制器编写unit testing时(使用Moq),我遇到了一个问题: private Mock membershipServiceMock; [TestInitialize] public void MyTestInitialize() { membershipServiceMock […]

StructureMap通过注入而不是服务位置来解析依赖性

在我的项目中,我使用汇编扫描程序注册了许多ISerializers实现。 FWIW这是注册我的ISerializers的代码 Scan(scanner => { scanner.AssemblyContainingType(); scanner.AddAllTypesOf().NameBy(type => type.Name); scanner.WithDefaultConventions(); }); 然后正确注册 ISerializer (…ISerializer) Scoped as: Transient JsonSerializer Configured Instance of …JsonSerializer BsonSerializer Configured Instance of …BsonSerializer 等等。 目前,我能够弄清楚如何解决我想要的串行器的唯一方法是使用硬编码服务位置调用 jsonSerializer = ObjectFactory.GetNamedInstance(“JsonSerializer”); 现在我在class上知道我特别想要jsonSerializer,所以有没有办法配置一个规则或类似的东西,说ISerializer根据属性名连接命名实例? 所以我可以 MySomeClass(ISerializer jsonSerializer, ….) StructureMap正确解决了这种情况? 或者我正在接近这个错误,也许我应该只注册实现ISerializer的具体类型,然后专门使用 MySomeClass(JsonSerializer jsonSerializer, ….) 对于具体类的这些方面的东西?

处理与服务定位器模式一起使用时MongoDB如何存储DateTime

我的同事和我在辩论中处于僵局,其他人的意见将不胜感激。 我们利用服务定位器模式和通用接口来抽象我们的所有数据访问,以便随着需求的变化,我们可以轻松地在不同的数据源之间进行交换。 我们的调用代码没有指示数据的存储位置或方式。 它只是通过服务注册表提供的服务访问数据。 当我们在对象上有DateTime字段并将其存储到MongoDB数据源时,我们正在讨论的问题就出现了。 我注意到的是,当我们在C#中有一个带有DateTime的对象时,它显示为正确的时间。 当我们使用MongoVUE登录MongoDB服务器来检查对象时,它会显示正确的时间。 但是当我们检索对象时,DateTime现在是UTC。 当将内存中的DateTime与从MongoDB数据源检索到的对象中的DateTime进行比较时,这显然会产生问题。 我知道Mongo在内部将DateTime存储为UTC时间。 我甚至理解为什么在你打电话时它可能会返回UTC。 这是辩论开始的地方。 有人建议这只是一个美容问题,只是显示日期时的一个问题。 因此,我们应该简单地在界面层中调用.ToLocalTime。 我不同意并声称这危险地打破了我们在实现服务定位器模式时创建的抽象层。 它还提出了与那些日期时间的交互问题,因为它涉及触发其他事件。 我在其他地方读到的是,我们应该将我们的时间存储为字符串,特别是作为UTC格式的一些标准。 以这种方式,接口层不知道或不关心如何存储DateTime,也不知道我们的对象,因为每个数据源都以相同的方式存储该字符串。 我已经成功使用ISO 1806格式,但我的同事认为这是一个’hacky’修复,使用.toLocalTime是处理这种情况的适当方法。 我对其他人对这个话题的看法很感兴趣。 提前感谢您的意见。

Singleton Vs ServiceLocator

使用服务定位器与单身人士相比有哪些优缺点? 我已经读到单身人士很糟糕,但我想知道服务定位器是否通常是更好的做事方式。