假设有一个ASP.NET MVC应用程序使用Entity Framework 6,代码优先方法和StructureMap作为IoC.
它还使用工作单元模式.以下是代码:
域类
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
IUnitOfWork和DbContext:
public interface IUnitOfWork
{
IDbSet<TEntity> Set<TEntity>() where TEntity : class;
int SaveChanges();
}
public class Sample07Context : DbContext, IUnitOfWork
{
public DbSet<Product> Products { set; get; }
#region IUnitOfWork Members
public new IDbSet<TEntity> Set<TEntity>() where TEntity : class
{
return base.Set<TEntity>();
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
服务类中的业务逻辑:
public interface …Run Code Online (Sandbox Code Playgroud) c# structuremap asp.net-mvc entity-framework dependency-injection
我低于错误.我设置它类似于asp.net mvc 4.
没有为此对象定义的无参数构造函数.描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
异常详细信息:System.MissingMethodException:没有为此对象定义的无参数构造函数.
终于找到了实际异常"尝试获取HomeController类型的实例时出现激活错误,密钥"""
当我要将服务类注入到家庭控制器时出现错误
structuremap dependency-injection c#-4.0 asp.net-mvc-4 asp.net-mvc-5
目前我有一个ActionFilter,它从HttpContext获取当前用户名,并将其传递给在服务方法上使用它的操作.例如:
Service.DoSomething(userName);
Run Code Online (Sandbox Code Playgroud)
我现在有理由不在动作级别而是在控制器构造级别执行此操作.目前我正在使用结构图来创建控制器并注入服务.我正在寻找类似的东西:
public interface IUserProvider
{
string UserName { get; }
}
public class HttpContextUserProvider : IUserProvider
{
private HttpContext context;
public HttpContextUserProvider(HttpContext context)
{
this.context = context;
}
public string UserName
{
get
{
return context.User.Identity.Name;
}
}
}
Run Code Online (Sandbox Code Playgroud)
也就是说,我的IoC foo非常弱,因为这是我用过的第一个项目.
所以我的问题是......如何告诉结构图在HttpContextUserProvider的构造函数中传递HttpContext?这看起来很奇怪......我不知道怎么想HttpContext.
我很好奇这两种方法之间的区别.我正在使用开放式泛型实现装饰器模式,无论我使用它AddAllTypesOf还是ConnectImplementationsToTypesClosing无关紧要,我都能获得相同的功能.
public class CommandRegistry : Registry
{
public CommandRegistry()
{
For<CommandProcessor>().Use<DefaultCommandProcessor>().Transient();
Scan(scanner =>
{
scanner.AssemblyContainingType<SaveCoolCommandHandler>();
//scanner.AddAllTypesOf(typeof(CommandHandler<>));
//scanner.AddAllTypesOf(typeof(IValidator<>));
//scanner.AddAllTypesOf(typeof(LogMehh<>));
scanner.ConnectImplementationsToTypesClosing(typeof(CommandHandler<>));
scanner.ConnectImplementationsToTypesClosing(typeof(IValidator<>));
scanner.ConnectImplementationsToTypesClosing(typeof(LogMehh<>));
});
var handlerType = For(typeof(CommandHandler<>));
handlerType.DecorateAllWith(typeof(CommandValidator<>)); //Second
handlerType.DecorateAllWith(typeof(CommandLogger<>)); //First
// ObjectFactory.WhatDoIHave();
}
}
Run Code Online (Sandbox Code Playgroud)
ObjectFactory.WhatDoIHave()无论我选择哪种方法,调用也会给出相同的结果.
我查看了源代码,这些方法肯定做了不同的事情,我只是无法确切地确定区别是什么.当一个人优先于另一个时,是否有任何指导方针或情景?
c# structuremap asp.net-mvc dependency-injection inversion-of-control
我无法弄清楚如何通过代码为StructureMap(版本2.5)中的类型定义默认构造函数(当它存在重载时).
我想获得一个服务的实例,容器必须将Linq2Sql数据上下文实例注入其中.
我用'bootstrapper'方法写了这个:
ForRequestedType<MyDataContext>().TheDefault.Is.OfConcreteType<MyDataContext>();
Run Code Online (Sandbox Code Playgroud)
当我运行我的应用程序时,我收到此错误:
StructureMap异常代码:202
没有为PluginFamily定义的默认实例MyNamespace.Data.SqlRepository.MyDataContext,MyNamespace.Data,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null
如果我注释掉所有我不需要的Linq2Sql生成的构造函数,它可以正常工作.
更新:哦,我忘了说我不会使用该[StructureMap.DefaultConstructor]属性.
简单地说,HybridHttpOrThreadLocalScoped和HttpContextScoped有什么区别?
我在为我的测试注入一个实例到structmap时遇到了问题.
我的对象图看起来像这样
internal class ConfigurationManager : IConfigurationManager : IManager
{
public ISomeManager SomeManager { get; set; }
}
internal class SomeManager : ISomeManager : IManager
{
public IConfigurationManager ConfigurationManager { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
1)首先我创建容器并添加所有找到的注册表
_container = new Container(c => c.Scan(s =>
{
s.TheCallingAssembly();
s.LookForRegistries();
}));
Run Code Online (Sandbox Code Playgroud)
其中一个扫描的程序集包含以下注册
x.For<IConfigurationManager>().Singleton.Use<ConfigurationManager>();
Run Code Online (Sandbox Code Playgroud)
2)然后我想为这些经理注入一个特殊的模拟对象
_configurationManagerStub = MockRepository.GenerateStub<IConfigurationManager>();
_container.Inject(_configurationManagerStub);
Run Code Online (Sandbox Code Playgroud)
3)然后创建管理器实例,而不配置setter注入(以避免循环依赖)
foreach (Type pluginType in AllManagers())
{
managerInstances.Add(_container.GetInstance(pluginType));
}
Run Code Online (Sandbox Code Playgroud)
4)最后我使用BuildUp方法设置IManager类型的属性.
_container.Configure(x => x.SetAllProperties(c =>
{
// configure the property injection for all managers …Run Code Online (Sandbox Code Playgroud) 我已经发现了一些2008年的基准测试结果用于测试的排名前几位的.NET DI/IoC容器的性能在这里.但我一直无法找到任何更新的结果.是否有任何基准测试可以比较一些大的IoC容器(StructureMap,Unity,Ninject,Autofac,Castle Windsor等)?
我是structureMap的新手.如何使用流畅的配置为以下类定义构造函数参数?谢谢
public BlobContainer(CloudStorageAccount account
, string containerName
, string contentType
, BlobContainerPermissions blobContainerPermissions)
{
}
Run Code Online (Sandbox Code Playgroud) 我在Automapper中找到的大多数示例都使用静态Mapper对象来管理类型映射.对于我的项目,我需要使用StructureMap注入IMapperEngine作为对象构造的一部分,以便我们可以在单元测试中模拟映射器,因此我们不能使用静态映射器.我还需要支持配置AutoMapper配置文件.
我的问题是如何配置StructureMap注册表,以便在构造MyService实例时它可以提供IMappingEngine的实例.
这是Service构造函数签名:
public MyService(IMappingEngine mapper, IMyRepository myRepository, ILogger logger)
Run Code Online (Sandbox Code Playgroud)
这是StructureMap注册表
public class MyRegistry : StructureMap.Configuration.DSL.Registry
{
public MyRegistry()
{
For<IMyRepository>().Use<MyRepository>();
For<ILogger>().Use<Logger>();
//what to do for IMappingEngine?
}
}
Run Code Online (Sandbox Code Playgroud)
我要加载的配置文件
public class MyAutoMapperProfile : AutoMapper.Profile
{
protected override void Configure()
{
this.CreateMap<MyModel, MyDTO>();
}
}
Run Code Online (Sandbox Code Playgroud) structuremap ×10
c# ×5
asp.net-mvc ×3
autofac ×1
automapper ×1
c#-4.0 ×1
http ×1
httpcontext ×1
ninject ×1