Unity中的StructureMap是否相同:
ServiceLocator.Current.GetAllInstances<IT>
Run Code Online (Sandbox Code Playgroud)
试图遵循这个小模式......
我正在使用StructureMap通过实例调用来丰富我的一些对象
ProxyGenerator.CreateInterfaceProxyWithTarget(myObject, MYInterceptor)
目前我有MYInterceptor我的容器内部,我应该为拦截器实现任何类型的缓存?
第二个问题是我应该ProxyGenerator在容器内注册,如果是,我应该应用任何类型的缓存吗?
我整天都在摸索着我的头脑,我找不到任何解决方案,所以我得到了帮助.听到我的问题:我有两个实现一个接口的类
public interface ICacheObject
{
string Get();
}
public class WebCacheObject : ICacheObject
{
public string Get()
{
return "Web";
}
}
public class SysteCacheObject : ICacheObject
{
public string Get()
{
return "System";
}
}
Run Code Online (Sandbox Code Playgroud)
所以在其他一些clase For Class in Class Test中,我需要注入WebCacheObject,并且在Test2中我必须注入SystemCacheObject.我在Initialize中做了这个:
ObjectFactory.Initialize(c =>{ c.For<IMessage>().Use<Message>();
c.For<ICacheObject>().ConditionallyUse(t =>{t.If(g => g.RequestedName == "HTTP")
.ThenIt.Is.ConstructedBy(
() =>
new WebCacheObject());
t.If(g =>g.RequestedName =="OtherCache")
.ThenIt.Is.ConstructedBy(
() =>
new SysteCacheObject
());
});
Run Code Online (Sandbox Code Playgroud)
但是我不知道如何调用Test-s clase-s所以如果我这样调用条件是真的(或者如何改变条件,那么这将起作用)
ObjectFactory.GetInstance <'ITEST>()
测试Clase将在其他情况下使用WebCache SystemCache ???
对不起,我的英语不好.
我不禁想到,比我的StructureMap Registry中的当前代码有更好的方法。
For<ISchedulerFactory>().Use(() => new StdSchedulerFactory());
For<IScheduler>().Use(() => new StdSchedulerFactory().GetScheduler());
Run Code Online (Sandbox Code Playgroud)
有没有办法让它使用以前的注册类型并从中调用方法? (GetScheduler()在ISchedulerFactory接口上)
我在NET MVC 2应用程序中使用StructurMap(刚刚升级到2.6.1)和Jimmy Bogard的智能模型绑定器.我也正在调整Dominic Pettifer的技术,以便您可以使用智能模型绑定器将DI注入到viewModel中,以获得需要重新填充选择列表的回发方案!
我知之甚少StructureMap,我得到的一个问题是structuremap 202 no instance defined errorviewModel与无参数构造函数的绑定.
所以在我的IOCMOdelBinder class尝试使用TryGetInstance()而不是GetInstance()前者如果与modelType不匹配则返回null.基本上,如果它找不到已注册的实例,则回退到默认的模型绑定器.
我的覆盖CreateModel类看起来像这样:
protected override object CreateModel(ControllerContext controllerContext,
ModelBindingContext bindingContext, Type modelType)
{
var myInstance = ObjectFactory.TryGetInstance(modelType);
if (myInstance != null)
{
return myInstance;
}
else
{
return base.CreateModel(controllerContext, bindingContext, modelType);
}
}
Run Code Online (Sandbox Code Playgroud)
我拿出了ObjectFactory.GetInstance(modelType);我希望它们以相同的方式工作的行,但TryGetInstance返回null并GetInstance返回正确的对象,所以它肯定在注册表中.我可以使用GetInstance但必须将它包装在try catch中,这有点不那么优雅!有什么建议吗?
我正在尝试注册工厂方法来创建开放泛型类型的实例MongoCollection<>.但是,当我GetInstance看来它使用的是MongoCollection的构造函数而不是工厂方法.
var mongo = new MongoConfiguration("mongodb://localhost", "test");
For(typeof (MongoCollection<>)).Use(c =>
{
var requestedType = c.BuildStack.Current.RequestedType; // set breakpoint here
var type = requestedType.GetGenericArguments()[0];
return mongo.GetCollection(type);
});
Run Code Online (Sandbox Code Playgroud)
然后我做
ObjectFactory.GetInstance<MongoCollection<User>>();
Run Code Online (Sandbox Code Playgroud)
当我运行该GetInstance行时,它永远不会在工厂方法中遇到断点,但它会抛出一个StructureMapException说法"没有为PluginFamily MongoDb.Driver.MongoServerSettings定义默认实例".有一个构造函数MongoCollection需要一个MongoServerSettings,但我不希望结构图使用该构造函数,我希望它使用我的工厂方法.
任何想法为什么它不使用工厂方法?这是一个错误吗?
我配置了一个ASP.NET MVC 4的Web API项目使用StructureMap 2.6.2.0中描述的这个帖子,但访问/api/parts返回下面的错误,尽管显式调用StructuremapMvc.Start();的Application_Start():
{
"ExceptionType": "System.ArgumentException",
"Message": "Type 'MyProject.Web.Controllers.PartsController' does not have
a default constructor",
"StackTrace": " at System.Linq.Expressions.Expression.New(Type type)\r\n
at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"
}
Run Code Online (Sandbox Code Playgroud)
我实现IDependencyResolver并IDependencyScope设置了Web API依赖关系解析器,如下所示~/App_Start/StructureMapMvc.cs:
GlobalConfiguration.Configuration.DependencyResolver =
new StructureMapHttpDependencyResolver(container);
Run Code Online (Sandbox Code Playgroud)
为什么Web API仍然抱怨默认的consturctor?
structuremap asp.net-mvc dependency-injection asp.net-web-api
我为StructureMap ObjectFactory.TryGetInstance得到"无法解决符号错误"
但ObjectFactory.GetInstance没问题.
StructureMap版本3.程序集包括"使用StructureMap;" 我在MVC 5项目中使用它.
错过任何其他包括?'
在我的项目中,我使用的是StructureMap.Net4(版本3.0.3)和StructureMap(3.0.3).我使用以下代码为Ioc配置了setter注入
public static IContainer Initialize()
{
ObjectFactory.Initialize(x =>
{
x.For<ICacheManager>().Use<MemmoryCacheManager>();
x.SetAllProperties(y => y.OfType<ICacheManager>);
//x.ForConcreteType<AuthorizationManager>()
// .Configure.Setter<ICacheManager>(y => y.CacheManager)
// .IsTheDefault();
});
return ObjectFactory.Container;
}
Run Code Online (Sandbox Code Playgroud)
但我得到一个错误无法解析符号SetAllProperties.我已经引用了以下命名空间
using StructureMap;
using StructureMap.Graph;
Run Code Online (Sandbox Code Playgroud)
为什么我收到此错误?我怎么能解决这个问题?或者我应该引用任何其他命名空间
我有一个使用StructureMap的ASP.NET MVC应用程序.
我创建了一个名为SecurityContext的服务,它具有静态Current属性.简化版本如下所示:
public class SecurityContext : ISecurityContext
{
public bool MyProperty { get; private set; }
public static SecurityContext Current
{
get
{
return new SecurityContext() { MyProperty = true };
}
}
}
Run Code Online (Sandbox Code Playgroud)
我已经在我的StructureMap注册表中将其连接起来,如下所示:
For<ISecurityContext>().Use(() => SecurityContext.Current);
Run Code Online (Sandbox Code Playgroud)
我对Use方法的Linq表达式重载的理解是返回的具体对象对于整个HTTP请求范围是相同的.
但是,我已经设置了一个测试用例,其中我的上下文接口被注入两个位置,一次在控制器的构造函数中,并再次使用SetterProperty我的视图继承自的基类中的属性.
在调试时,我观察Current静态方法被击中两次,所以我的假设是错误的.谁能纠正我在这里做的事情?我想要这个请求范围的原因是因为我正在从数据库中将某些数据加载到我的上下文类中,所以我不希望这对于给定的页面加载多次发生.
提前致谢.