Spring.Net 中的服务定位器风格查找 - 更好的想法?
当我希望只有一个对象时,我使用以下方法按类型从 Spring.Net 上下文获取对象,因此我不需要在代码中添加魔术字符串。我已经看到这个区域在配置文件中出现,因为那里可能很慢。有没有一种更简单、更好的方法来做到这一点?
public static T Locate<T>()
{
var objList = Context.GetObjectsOfType(typeof(T));
if (objList.Count == 0)
throw new Exception("No object of type: " + typeof(T).FullName + " found");
if (objList.Count > 1)
throw new Exception("Multiple objects of type: " + typeof(T).FullName + " found");
T oneObj = default(T);
foreach (DictionaryEntry e in objList)
oneObj = (T)e.Value;
return oneObj;
}
有时我也使用这种风格来传递运行时参数。在有人向我提出反模式之前……据我所知,执行服务定位器类型模式是传递其值仅在运行时已知的参数的唯一方法。
public static T Locate<T>(params object[] arguments) where T : class
{
var objectNames = Context.GetObjectNamesForType(typeof(T));
if (objectNames.Length == 1)
{
return Context.GetObject(objectNames[0], arguments) as T;
}
if (objectNames.Length == 0)
{
throw new ApplicationException("No object of type: " + typeof(T).FullName + " found");
}
throw new ApplicationException("Multiple objects of type: " + typeof(T).FullName + " found");
}
I'm using the following method to get objects from the Spring.Net context by type when I expect to have only one so I don't need to put magic strings in my code. I've seen this area come up in profiles as potentially slow there. Is there a less brute force and better way for me to be doing this?
public static T Locate<T>()
{
var objList = Context.GetObjectsOfType(typeof(T));
if (objList.Count == 0)
throw new Exception("No object of type: " + typeof(T).FullName + " found");
if (objList.Count > 1)
throw new Exception("Multiple objects of type: " + typeof(T).FullName + " found");
T oneObj = default(T);
foreach (DictionaryEntry e in objList)
oneObj = (T)e.Value;
return oneObj;
}
At times I also use this style to pass in run-time parameters. Before someone jumps on me for the anti-pattern... as far as I can tell doing a service locator type pattern is the only way to pass in parameters whose values are only known at runtime.
public static T Locate<T>(params object[] arguments) where T : class
{
var objectNames = Context.GetObjectNamesForType(typeof(T));
if (objectNames.Length == 1)
{
return Context.GetObject(objectNames[0], arguments) as T;
}
if (objectNames.Length == 0)
{
throw new ApplicationException("No object of type: " + typeof(T).FullName + " found");
}
throw new ApplicationException("Multiple objects of type: " + typeof(T).FullName + " found");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想使用
ServiceLocator
模式,我建议您使用 codeplex 上名为 CommonServiceLocator 的小项目< /a>.它附带了一个 Spring.net 适配器实现。这样,您的代码将不依赖于 Spring 容器,允许您根据需要使用另一个容器。
一般来说,我认为您不想从代码中过多地访问 IOC 容器。让容器进行接线;-)。
If you want to use the
ServiceLocator
pattern, I suggest you use a small project on codeplex called CommonServiceLocator. It comes with a Spring.net adapter implementation.This way, your code wil not depend on the Spring container, allowing you to use another container if you want.
In general, I don't think you want to access you IOC container too much from code. Let the container do the wiring ;-).