关于温莎城堡的几个问题
关于温莎城堡的一些问题。
我有一个看起来像这样的服务:
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 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 线程安全吗?
A few questions about Castle Windsor.
I have a service that looks like this:
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
}
I'm thinking the best thing to do is keep that in the container but not sure if I should keep it as Singleton or Transient. The only state it keeps is the repository. I was thinking the best way would be as a Singleton and the code looks cleaner too. For example:
//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);
}
Also the other thing I'm wondering is Castle thread safe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它实际上取决于存储库的编码方式。阅读这篇文章,了解不同生活方式的组成部分如何相互作用。我总是尝试使我的组件成为线程安全、无状态的,这样我就可以使它们成为单例,这使一切变得更简单。
顺便说一句:您应该尽量避免调用 Resolve() 。 不要调用容器,它会调用您。
是的,Windsor 是线程安全的。
It actually depends on how the repository is coded. Read this article to understand how components of different lifestyles interact. I always try to make my components thread-safe, stateless so I can make them singletons, it makes everything simpler.
BTW: you should try to avoid calling Resolve() as much as possible. Don't call the container, it will call you.
And yes, Windsor is thread-safe.