Tag: dependency injection

Castle Windsor – 注入IActionInvoker实施问题

我正在尝试使用本文中的方法,但我遗漏了一些东西 – 我当前在WindsorControllerFactory.GetControllerInstance中尝试解析IActionInvoker时出现错误,因为WindsorActionInvoker依赖于IWindsorContainer。 鉴于WindsorControllerFactory已经有对IWindsorContainer的引用,我可以传入该引用吗? 如果是这样 – 怎么样? 我发现的唯一例子是将值类型作为构造函数参数传递,而不是引用类型。 我觉得我错过了一些明显的东西…… 当前设置如下:在Global.asax Application_Start中我调用以下方法: protected virtual IWindsorContainer InitializeServiceLocator() { IWindsorContainer container = new WindsorContainer(); ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container)); container.RegisterControllers(typeof(HomeController).Assembly); ComponentRegistrar.AddComponentsTo(container); return container; } ComponentRegistrar: public static void AddComponentsTo(IWindsorContainer container) { //add other components….. container.AddComponentLifeStyle(LifestyleType.PerWebRequest); } WindsorActionInvoker: public class WindsorActionInvoker : ControllerActionInvoker, IActionInvoker { readonly IWindsorContainer container; public WindsorActionInvoker(IWindsorContainer container) { […]

dependency injection和AppSettings

假设我正在为我的应用程序定义一个浏览器实现类: class InternetExplorerBrowser : IBrowser { private readonly string executablePath = @”C:\Program Files\…\…\ie.exe”; …code that uses executablePath } 这可能乍一看看起来像个好主意,因为executablePath数据靠近将使用它的代码。 当我尝试在另一台具有外语操作系统的计算机上运行相同的应用executablePath会出现问题: executablePath将具有不同的值。 我可以通过AppSettings单例类(或其中一个等价物)解决这个问题但是没有人知道我的类实际上依赖于这个AppSettings类(它违背了DI ideias)。 它也可能给unit testing带来困难。 我可以通过构造函数传入executablePath来解决这两个问题: class InternetExplorerBrowser : IBrowser { private readonly string executablePath; public InternetExplorerBrowser(string executablePath) { this.executablePath = executablePath; } } 但是这会在我的Composition Root (将要执行所有需要的类连接的启动方法)中引发问题,因为那个方法必须知道如何连接并且必须知道所有这些小设置数据: class CompositionRoot { public void Run() { ClassA […]

dependency injection(使用SimpleInjector)和OAuthAuthorizationServerProvider

对dependency injection的新手,所以这可能是一件简单的事情,但我已经尝试过并且无法弄明白,我正在使用Simple Injector。 我有一个完全使用SimpleInjector的WebApi,现在我想使用OAuth实现安全性。 为此,我开始学习本教程,这非常有用,但不使用Dependancy Injection Token Based Authentication using ASP.NET Web API 2, Owin, and Identity 我的global.asax文件看起来像这样,设置dependency injection(完美工作) protected void Application_Start() { SimpleInjectorConfig.Register(); GlobalConfiguration.Configure(WebApiConfig.Register); } 我创建了一个Startup.Auth.cs文件来配置OAuth public class Startup { public void Configuration(IAppBuilder app) { var OAuthServerOptions = new OAuthAuthorizationServerOptions() { AllowInsecureHttp = true, TokenEndpointPath = new PathString(“/token”), AccessTokenExpireTimeSpan = TimeSpan.FromDays(1), Provider = new […]

在unit testing中使用DI容器

我们一直在使用Simple Injector取得了很好的成功,在一个相当实际的应用程序中。 我们一直在为所有生产类使用构造函数注入,并配置Simple Injector来填充所有内容,而且一切都很好。 但是,我们没有使用Simple Injector来管理unit testing的依赖树。 相反,我们一直在手动创新。 我花了几天时间完成了一次重大的重构,几乎所有的时间都是在我们的unit testing中修复这些手动构造的依赖树。 这让我感到疑惑 – 有没有人有任何模式用于配置他们在unit testing中使用的依赖树? 对我们来说,至少在我们的测试中,我们的依赖树往往相当简单,但有很多。 任何人都有他们用来管理这些的方法吗?

Castle Windsor – 多个接口实现

在Castle Windsor中注册组件时,我们如何将接口的特定实现绑定到依赖于该接口的组件。 我事先知道组件需要使用哪种实现方式。 例如,我基于来自几个博客和教程的代码创建了一个示例控制台应用程序。 以下是代码。 public interface IReport { void LogReport(); } public interface ILogger { string Log(); } public class FileLogger : ILogger { public string Log() { return “Logged data to a file”; } } public class DatabaseLogger : ILogger { public string Log() { return “Logged data to a database”; } } […]

当密钥类需要Session(或其他特定于上下文的变量)时如何设置IoC

我试图弄清楚如何在依赖类可以根据应用程序中的某个变量(在本例中为Session状态)进行更改的情况下使用IoC。 例如,我们的每个客户端都有一个不同的数据库,因此与数据库的连接需要建立在其Session中存储的值上(特别是因为如果某些用户拥有多个业务,并且可以在数据库之间切换,则可能有多个数据库) 。 以下是我们当前如何设置此结构的一般示例: public class MyTestController : ControllerBase { Repository _rep; public MyTest(Repository rep) { _rep = rep; } public MyTest() { string connString = String.Format(“Server={0}; Database={1};” , SessionContainer.ServerName, SessionContainer.DatabaseName; var dc = new DataContext(connString); _rep = new Repository(dc); } public int SampleFn() { return _rep.GetCountOfEmployees(); } } public class Repository { DataContext _context; […]

ASP.NET MVC按请求注入

我需要为每个请求注入EF上下文。 有没有办法实现它?

如何在请求结束时释放资源并在ASP.NET 5 / Core中处理注入的服务?

我有一个服务,它使用ASP.NET Core的默认dependency injection容器注入控制器: public class FooBarService : IDisposable { public void Dispose() { … } } services.AddScoped(); 这会为每个请求创建一个实例。 如何确保框架在每个请求结束时处理FooBarService实例,而不依赖于析构函数和垃圾回收?

哪个DI容器会满足这个要求

这是我想要的DI容器: public class Class { public Class(IDependency dependency, string data) { } } var obj = di.Resolve(() => new Class(null, “test”)); 兴趣点: 可以在构造函数中解析依赖项和数据。 可以使用类型安全语法来传递构造函数参数(确切的语法可能会有所不同)。 是的我可以通过从(Expression.Body作为NewExpression)获取构造函数参数来自己完成 – 但我需要一种方法来检测容器中注册的参数。 另一个主要要求是我希望自动拾取我的组件,即我不想注册Class – 我希望IoC能够选择它,因为它知道如何解决IDependency。 此外,Property Injection有时也很有用,但这是可选的。 问题实际上是关于function的组合 – 拥有所有function – 类型安全,参数,自动拾取……很容易检查一个function,但除非一个人熟悉,否则它们的组合不容易validation特定容器并了解其function。 这样的问题。

如何使用构造函数dependency injectionunit testingasp.net核心应用程序

我有一个asp.net核心应用程序,它使用在应用程序的startup.cs类中定义的dependency injection: public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => options.UseSqlServer(Configuration[“Data:FotballConnection:DefaultConnection”])); // Repositories services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); // Services services.AddScoped(); services.AddScoped(); // new repos services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); 这允许这样的事情: [Route(“api/[controller]”)] public class MatchController : AuthorizedController { private readonly IMatchService _matchService; private readonly IMatchRepository _matchRepository; private readonly IMatchBetRepository _matchBetRepository; private readonly IUserRepository _userRepository; private readonly ILoggingRepository […]