有没有理由从默认范围(瞬态?)切换到其他东西,除了需要为功能原因控制范围(例如Singleton)?
如果我坚持使用默认范围,每个插件类型的每个默认实例都将在每个请求(假设是一个Web应用程序)上实际实例化,这是正确的吗?这会明显影响性能吗?
我已经考虑过使用Http Session范围来限制每个登录用户的一个实例.但是,这将导致(至少)每个插件类型的一个实例始终存储在内存中.使用默认范围,这些实例仅在处理页面请求时保留在内存中.我不确定哪个更好.
如果您使用StructureMap,您通常如何为每个插件类型配置范围?
感谢您的任何见解,
菲尔
我刚开始使用StructureMap,之前曾使用过Spring.Net.我喜欢DefaultConventionScanner和扫描程序集的能力,并使用约定优于配置来查找类.但似乎存在一个限制,即实现接口的类必须是公共的,而我们喜欢将接口公开,并将我们的实现保留在程序集内部.
有没有办法要求DefaultConventionScanner查找内部类?
我是StructureMap的忠实粉丝,几乎可以用于我所做的一切.我只使用它与接口.我想知道是否有人有使用抽象类的经验?或者...它不支持这种类型的布线?如果你有这个工作,你可以发一个例子吗?
谢谢!
structuremap abstract-class dependency-injection interface ioc-container
我有一个线程安全对象,创建起来很昂贵,需要通过我的应用程序(Lucene.Net IndexReader)提供.
该对象可能变为无效,此时我需要重新创建它(IndexReader.IsCurrent为false,需要使用IndexReader.Reopen的新实例).
我希望能够使用IoC容器(StructureMap)来管理对象的创建,但是如果可能的话,我无法解决.感觉就像某种"有条件的单身"生命周期.
StructureMap是否提供了这样的功能?还有其他建议吗?
这可能只是一个新手问题,但我有以下几点:
public class FooSettings {}
public class BarSettings {}
public class DohSettings {}
// There might be many more settings types...
public interface IProcessor { ... }
public class FooProcessor
: IProcessor
{
public FooProcessor(FooSettings) { ... }
}
public class BarProcessor
: IProcessor
{
public BarProcessor(BarSettings) { ... }
}
public class DohProcessor
: IProcessor
{
public DohProcessor(DohSettings) { ... }
}
// There might be many more processor types with matching settings...
public interface IProcessorConsumer {}
public class …Run Code Online (Sandbox Code Playgroud) 我正在研究遗留代码.
我有相同类的不同方法,它们将不同的参数传递给依赖项的构造函数.我正在尝试引入一些基本的IoC用法.现在我让StructureMap像这样传递我的参数:
var thing = ObjectFactory.GetInstance<IThingInterface>(new ExplicitArguments(
new Dictionary<string, object> {
{ "constructorArgA", notShown },
{ "constructorArgB", redacted.Property } }));
Run Code Online (Sandbox Code Playgroud)
为constructorArgA和B传递的实际属性取决于我的位置.
而不是"constructorArgA"有一种方法可以通过实际类型配置它,就像配置objectFactory时一样,如:
x.For<IHidden>().Use<RealType>()
.Ctor<IConfig>().Is(new Func<IContext, IConfig>(
(context) => someMethodToGetIConfig()));
Run Code Online (Sandbox Code Playgroud)
如果我从头开始编写这个,我可能会将依赖关系构建得有点不同以避免这种情况,但这对我来说不是一个选择.
对Structure-Map来说很新.试图弄清楚它是如何工作的,我怎样才能从中受益.
到目前为止我有这个..
的Global.asax.cs:
IContainer container = new Container(x =>
{
x.For<IControllerActivator>().Use
<StructureMapControllerActivator>();
x.For<IUserRepo>().Use<UserRepo>();
});
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
Run Code Online (Sandbox Code Playgroud)
StructureMapControllerActivator:
public class StructureMapControllerActivator : IControllerActivator
{
private readonly IContainer _container;
public StructureMapControllerActivator(IContainer container )
{
this._container = container;
}
public IController Create(RequestContext requestContext, Type controllerType)
{
return _container.GetInstance(controllerType) as IController;
}
}
Run Code Online (Sandbox Code Playgroud)
StructreMapDependencyResolver:
private readonly IContainer _container;
public StructureMapDependencyResolver(IContainer container )
{
this._container = container;
}
public object GetService(Type serviceType)
{
object instance = _container.TryGetInstance(serviceType);
if(instance == null && !serviceType.IsAbstract)
{
_container.Configure(c => …Run Code Online (Sandbox Code Playgroud) c# structuremap asp.net-mvc dependency-injection asp.net-mvc-3
我们有一个MVC 4项目,并使用Structure Map作为IoC框架.当我们对使用DbContext的服务类进行异步调用时,它会抛出异常:
Value cannot be null.
Parameter name: httpContext
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: httpContext
Run Code Online (Sandbox Code Playgroud)
一旦抛出这个:
The operation cannot be completed because the DbContext has been disposed.
Run Code Online (Sandbox Code Playgroud)
我怎么能纠正这个?
我有一个现有的应用程序使用2.x版本的Structuremap中的最后一个版本,它工作正常.StructureMap 3刚刚上线,我决定尝试更新它,看看它是怎么回事.
但无论我做什么,我似乎无法正确解决当前用户.我不确定它是否试图在应用程序的生命周期中过早地构建依赖项或者交易可能是什么.因为最近的发布,几乎没有任何信息,我发现它还没有任何用处.
注册依赖项的行.
For<HttpContextBase>().Use(() => new HttpContextWrapper(HttpContext.Current));
For<ICurrentUser>().HybridHttpOrThreadLocalScoped().Use(x => GetCurrentUser(x));
Run Code Online (Sandbox Code Playgroud)
我解决依赖关系的方法
private ICurrentUser GetCurrentUser(IContext context)
{
try
{
var httpContext = context.GetInstance<HttpContextBase>();
if (httpContext == null) return null;
if (httpContext.User == null) return null;
var user = httpContext.User;
if (!user.Identity.IsAuthenticated) return null;
var personId = user.GetIdentityId().GetValueOrDefault();
return new CurrentUser(personId, user.Identity.Name);
}
catch (Exception ex)
{
context.GetInstance<ILogger>().Error("Error trying to determine the current user.", ex);
throw new Exception("Error trying to determine the current user.", ex);
}
}
Run Code Online (Sandbox Code Playgroud)
我的ICurrentUser接口
public interface ICurrentUser …Run Code Online (Sandbox Code Playgroud) 我试图了解如何使用Dapper(IDbConnection)依赖注入,并仍然能够使用内置的dispose.
我在网上发现了一些文章,但我觉得很容易理解.
我想弄清楚的是如何使这个简单的类可测试:
public class UserProfileRepository : IUserProfileRepository
{
private readonly IConfigRepository _configRepository;
public UserProfileRepository(IConfigRepository configRepository)
{
_configRepository = configRepository;
}
public UserProfile GetUserProfile(string userId)
{
const string query = @"Select UserId, UserName
From Users
Where UserId = @UserId";
using (var conn = new SqlConnection(_configRepository.GetConnectionString("MyConnectionString")))
{
conn.Open();
return conn.Query<UserProfile>(query, new { UserId = userId }).SingleOrDefault();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个看起来像这样的配置库,所以我可以模拟对web.config的请求:
public class ConfigRepository : IConfigRepository
{
public string GetConnectionString(string key)
{
var conString = ConfigurationManager.ConnectionStrings[key];
if (conString != null)
{
return …Run Code Online (Sandbox Code Playgroud) structuremap ×10
c# ×5
asp.net ×1
asp.net-mvc ×1
autofac ×1
dapper ×1
dbcontext ×1
httpcontext ×1
interface ×1
lucene.net ×1
ninject ×1
singleton ×1