在使用这种方法一段时间后,我发现自己只在每个控制器中添加完全相同的样板代码,所以我决定做一些反射魔法.与此同时,我放弃使用MVC作为我的观点 - Razor是如此乏味和丑陋 - 所以我基本上使用我的处理程序作为JSON后端.我目前使用的方法是使用Route位于某个常见程序集中的属性来装饰我的查询/命令,如下所示:
[Route("items/add", RouteMethod.Post)]
public class AddItemCommand { public Guid Id { get; set; } }
[Route("items", RouteMethod.Get)]
public class GetItemsQuery : IQuery<GetItemsResponse> { }
// The response inherits from a base type that handles
// validation messages and the like
public class GetItemsResponse : ServiceResponse { }
Run Code Online (Sandbox Code Playgroud)
然后我实现了一个MVC主机,它提取带注释的命令/查询,并在启动时为我生成控制器和处理程序.有了这个,我的应用程序逻辑终于没有MVC了.查询响应也会自动填充验证消息.我的MVC应用程序现在看起来像这样:
+ MvcApp +- Global.asax +- Global.asax.cs - Startup the host and done +- Web.config
在意识到我真的不在主机之外使用MVC - 并且经常遇到框架所具有的bazillion依赖性问题 - 我实现了另一个基于NServiceKit的主机.在我的应用程序逻辑中没有什么必须改变,并且依赖关系到了System.Web, …
我有一个新的MVC Web项目,我在MVC和WebApi中使用.我在我的全局文件中使用以下代码设置了Simple Injector(来自nuGet的2.5.2版本)
// Register Injectors
SimpleInjectorConfig.Register();
Run Code Online (Sandbox Code Playgroud)
在我的SimpleInjectorConfig.cs文件中
public class SimpleInjectorConfig
{
public static void Register() {
// Create the container as usual.
Container container = new Container();
// services
container.Register<IService, MyService>();
// data
container.Register<IRepository, MyRepository>();
// Register your types, for instance using the RegisterWebApiRequest
// extension from the integration package:
container.RegisterMvcControllers(
System.Reflection.Assembly.GetExecutingAssembly());
container.RegisterMvcAttributeFilterProvider();
// This is an extension method from the integration package.
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
// verify its all ok
container.Verify();
// add dependency
GlobalConfiguration.Configuration.DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(container);
}
} …Run Code Online (Sandbox Code Playgroud) asp.net asp.net-mvc dependency-injection controller simple-injector
我的服务接口有一个名称空间 Services.Interfaces
Service Interfaces的实现具有名称空间 Web.UI.Services
例如,我有2个服务实现
这就是我目前使用SimpleInjector注册这些服务的方法.
container.Register<IUserService, UserService> ();
container.Register<ICountryService, CountryService> ();
Run Code Online (Sandbox Code Playgroud)
问题:如果我有超过100个服务夸大了一点.我需要为每项服务添加一行.
如何使用Simple Injector将所有实现从一个程序集注册到所有接口形成另一个程序集?
c# asp.net-mvc ioc-container inversion-of-control simple-injector
我正在尝试在我的Windows窗体应用程序中实现IoC.我的选择落在Simple Injector上,因为它快速而轻巧.我还在我的应用程序中实现了工作单元和存储库模式.这是结构:
DbContext:
public class MemberContext : DbContext
{
public MemberContext()
: base("Name=MemberContext")
{ }
public DbSet<Member> Members { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();\
}
}
Run Code Online (Sandbox Code Playgroud)
型号:
public class Member
{
public int MemberID { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
GenericRepository:
public abstract class GenericRepository<TEntity> : IGenericRepository<TEntity>
where TEntity : class
{
internal DbContext context;
internal DbSet<TEntity> dbSet;
public GenericRepository(DbContext context)
{
this.context = context; …Run Code Online (Sandbox Code Playgroud) c# entity-framework inversion-of-control repository-pattern simple-injector
在ASP.NET Core 2.0中,有一种方法可以通过实现IHostedService接口来添加后台任务(请参阅https://docs.microsoft.com/en-us/aspnet/core/fundamentals/hosted-services?view=aspnetcore-2.0).通过本教程,我能够使它工作的方式是在ASP.NET Core容器中注册它.我的目标是从队列中读取消息并在后台处理作业; 消息将发布到队列(通过控制器操作),然后在定时间隔内在后台处理.
// Not registered in SimpleInjector
services.AddSingleton<IHostedService, MyTimedService>();
Run Code Online (Sandbox Code Playgroud)
当我将此注册放在ASP.NET Core容器中时,它会在应用程序启动时自动启动该过程.但是,当我在SimpleInjector中注册时,服务不会自动启动.我相信这是因为我们只使用MvcControllers和MvcViewComponents注册SimpleInjector容器:
// Wire up simple injector to the MVC components
container.RegisterMvcControllers(app);
container.RegisterMvcViewComponents(app);
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是当我想开始从SimpleInjector注入组件寄存器(例如存储库,带有装饰器的通用处理程序......)到IHostedService的实现中时,如下所示:
public class TimedService : IHostedService, IDisposable
{
private IJobRepository _repo;
private Timer _timer;
public TimedService(IJobRepository repo)
{
this._repo = repo;
}
...
...
...
}
Run Code Online (Sandbox Code Playgroud)
由于IHostedService在ASP.NET Core而非Simple Injector中注册,因此在运行定时后台服务时收到以下错误:
未处理的异常:System.InvalidOperationException:尝试激活"Optimization.API.BackgroundServices.TimedService"时,无法解析类型"Optimization.Core.Interfaces.IJobRepository"的服务.
所以我的问题是,在Simple Injector中实现后台任务的最佳方法是什么?这是否需要一个独立的集成包而不是标准的MVC集成?我怎样才能将我的Simple Injector注册注入IHostedService?如果我们可以在Simple Injector中注册后自动启动服务,我认为这样可以解决这个问题.
感谢您在此提出任何建议以及有关此主题的任何建议!我可能做错了什么.在过去的一年里,我非常喜欢使用Simple Injector.
在Simple Injector中,我可以执行以下操作:
container.RegisterSingle<IAuctionContext>(() => new AuctionContext(
new Uri("http://localhost:60001/AuctionDataService.svc/")));
Run Code Online (Sandbox Code Playgroud)
我在这里做的是说,IAuctionContext找到它后,用这个新的替换它AuctionContext.问题是,通过调用RegisterSingle,只会使用一个实例AuctionContext.我希望它能够传递Uri如上所述的参数但不具有单个实例但每次都允许新实例.
这怎么可能?
我在我的asp.net mvc 4项目中使用Simple Injector.
我无法弄清楚如何使用带有城堡代理拦截器的Simple Injector.
我们一直在使用Simple Injector取得了很好的成功,在一个相当实际的应用程序中.我们一直在为所有生产类使用构造函数注入,并配置Simple Injector来填充所有内容,而且一切都很好.
但是,我们没有使用Simple Injector来管理单元测试的依赖树.相反,我们一直在手动创新.
我花了几天时间完成了一次重大的重构,几乎所有的时间都是在我们的单元测试中修复这些手动构造的依赖树.
这让我感到疑惑 - 是否有人使用任何模式来配置他们在单元测试中使用的依赖树?对我们来说,至少在我们的测试中,我们的依赖树往往相当简单,但有很多.
任何人都有他们用来管理这些的方法吗?
c# unit-testing dependency-injection ioc-container simple-injector
已更新至AutoMapper 4.2.0,并按照此处提供的迁移指南进行操作:https://github.com/AutoMapper/AutoMapper/wiki/Migrating-from-static-API/f4784dac61b91a0df130e252c91a0efd76ff51de#preserving-static-feel.尝试将StructureMap的页面上的代码转换为Simple Injector.有人能告诉我这个代码在Simple Injector中的样子吗?
StructureMap
public class AutoMapperRegistry : Registry
{
public AutoMapperRegistry()
{
var profiles =
from t in typeof (AutoMapperRegistry).Assembly.GetTypes()
where typeof (Profile).IsAssignableFrom(t)
select (Profile)Activator.CreateInstance(t);
var config = new MapperConfiguration(cfg =>
{
foreach (var profile in profiles)
{
cfg.AddProfile(profile);
}
});
For<MapperConfiguration>().Use(config);
For<IMapper>().Use(ctx => ctx.GetInstance<MapperConfiguration>().CreateMapper(ctx.GetInstance));
}
}
Run Code Online (Sandbox Code Playgroud)
简单的注射器
?
Run Code Online (Sandbox Code Playgroud) Serilog允许创建一个上下文感知记录器:
Log.ForContext<T>()
我想用SimpleInjector注册Serilog,这T是消费者的类型,即它注入的是哪个类.
例如
public class Car
{
public Car(ILogger logger) <= would be injected using Log.ForContext<Car>()
{
}
}
Run Code Online (Sandbox Code Playgroud)
我可以看到这已经完成了AutoFac.
通过SimpleInjector文档,有一个非常有希望的重载RegisterConditional()(使用Func<TypeFactoryContext, Type>参数).
c.RegisterConditional(typeof (ILogger),
x => Log.ForContext(x.Consumer.ImplementationType), <= won't compile as expecting a Type
Lifestyle.Scoped,
x => true);
Run Code Online (Sandbox Code Playgroud)
但是,我不想告诉SimpleInjector 要构建哪个 Type,而是如何构建一个.
simple-injector ×10
c# ×9
asp.net-mvc ×3
asp.net ×1
automapper ×1
controller ×1
decorator ×1
serilog ×1
unit-testing ×1