关于温莎城堡的一些问题。
我有一个看起来像这样的服务:
public interface IMysRepository
{
ISomeObject GetById(int id);
}
public interface IMyService
{
ISomeObject GetById(int id);
}
public class MyService : IMyService
{
MyService(IMysRepository repository)
{
_repository = repository;
}
public ISomeObject GetById(int id)
{
return _repository.GetById(id);
}
IMysRepository _repository
}
我认为最好的办法是将其保留在容器中,但不确定是否应将其保留为Singleton或Transient。它保留的唯一状态是存储库。我认为最好的方法是作为Singleton,代码也看起来更干净。例如:
//Singleton version.
var service = container.Reslove<IMyService>();
var obj = IMyService.GetById(100);
VS
//Transient version.
var service = container.Reslove<IMyService>();
try
{
var obj = IMyService.GetById(100);
}
finally
{
container.Release(service);
}
另外我想知道的是Castle thread safe吗?
答案 0 :(得分:0)
实际上取决于存储库的编码方式。阅读this article以了解不同生活方式的组成部分如何相互作用。我总是试图使我的组件线程安全,无状态,所以我可以让它们成为单例,它使一切变得更简单。
BTW:你应该尽量避免调用Resolve()。 Don't call the container, it will call you
是的,Windsor是线程安全的。