Tag: dependency injection

Azure服务结构actordependency injection

有没有办法将依赖项注入Azure Service Fabric Actor的构造函数?

.net-coredependency injection

我有一个Generic存储库,我想注册DI,它实现了一个接口IRepository。 通常我会像这样创建一个它的实例: IRepository repo = new Repository(); 但是我试图在发布之前加速.net 5,并希望将其与DI一起使用,我采用了以下方法: services.AddTransient<DAL.IRepository, DAL.Repository>(); 但这感觉不对,我不希望在我的模型中的每个类中都有50多行… 我在网上找不到任何关于此的内容,我知道它可能与其他ioc容器有关..但由于这是一个学习项目,我不想使用另一个容器,我的目标是用.net5s本机容器来完成所有这些。

使用dependency injection框架时的抽象工厂

我想知道如何在使用DI框架时正确使用抽象工厂,并且该工厂中的一个参数是应该由DI框架处理的依赖项。 我不确定是否让我的抽象工厂完全省略参数然后使用我的DI容器连接它或者是否应该将依赖项传递给对象。 例如,我有一个TcpServer,它使用Session.Factory来创建套接字。 Session对象实际上在其构造函数中使用了Processor。 我应该将处理器传递给TcpServer然后将它传递给Session.Factory还是让我的DI容器进行接线? 如果我要让DI容器进行接线,它将如下所示: class Session : ISession { public delegate ISession Factory(string name); … public Session(string name, Processor processor) { … } } class TcpServer : ITcpServer { private readonly Session.Factory _sessionFactory; public TcpServer(Session.Factory sessionFactory) { this._sessionFactory = socketFactory; } … public void OnConnectionReceived() { … var session= _sessionFactory(ip.LocalEndPoint()); … } } […]

使用日志注入模块时,在Autofac中显式解析ILog

我使用以下代码为所有需要它的类注册log4net。 public class LogInjectionModule : Module { private readonly string _configPath; public LogInjectionModule(string configPath) { _configPath = configPath; } protected override void AttachToComponentRegistration(IComponentRegistry registry, IComponentRegistration registration) { XmlConfigurator.Configure(new FileInfo(_configPath)); registration.Preparing += OnComponentPreparing; } private static void OnComponentPreparing(object sender, PreparingEventArgs e) { var t = e.Component.Activator.LimitType; e.Parameters = e.Parameters.Union(new[] { new ResolvedParameter((p, i) => p.ParameterType == […]

用.net反转控制

我很少听到有人使用.Net 控制反转(Ioc)原理。 我有一些使用Java的朋友在Spring和PicoContainer中使用了更多的Ioc。 我理解从代码中删除依赖关系的原则……但我怀疑它是否更好。 为什么.Net程序员不使用(或使用更少)这些类型的框架? 如果你这样做,从长远来看,你真的发现了积极的影响吗?

使用dependency injection注入dependency injection器

对dependency injection很新,我试图弄清楚这是否是反模式。 假设我有3个组件: Foo.Shared – this has all the interfaces Foo.Users – references Foo.Shared Foo.Payment – references Foo.Shared Foo.Users需要一个在Foo.Payment中构建的对象,而Foo.Payment也需要来自Foo.Users的东西。 这会产生某种循环依赖。 我在Foo.Shared中定义了一个接口,它代理我正在使用的dependency injection框架(在本例中为NInject)。 public interface IDependencyResolver { T Get(); } 在容器应用程序中,我有一个这个接口的实现: public class DependencyResolver:IDependencyResolver { private readonly IKernel _kernel; public DependencyResolver(IKernel kernel) { _kernel = kernel; } public T Get() { return _kernel.Get(); } } 配置如下所示: […]

Workflow Foundation 4中的dependency injection/ IoC

是否可以在工作流程活动中使用DI? 如果是,怎么样? 例如,如果您有类似的活动 public sealed class MyActivity : CodeActivity { public MyClass Dependency { get; set; } protected override void Execute(CodeActivityContext context) { Dependency.DoSomething(); } } 我该如何设置Dependency ? (我正在使用Spring.Net)

在Ninject中注入接口数组

请考虑以下代码。 public interface IFoo { } public class Bar { public Bar(IFoo[] foos) { } } public class MyModule : NinjectModule { public override void Load() { Bind().ToConstant(new IFoo[0]); // ToConstant() is just an example } } public class Program { private static void Main(string[] args) { var kernel = new StandardKernel(new MyModule()); var bar […]

Autofac解决CQRS CommandDispatcher中的依赖关系

我正在尝试实现一个简单的CQRS应用程序示例。 这是我的“命令”部分的结构: public interface ICommand { } //base interface for command handlers interface ICommandHandler where TCommand: ICommand { void Execute(TCommand command); } // example of the command public class SimpleCommand: ICommand { //some properties } // example of the SimpleCommand command handler public class SimpleCommandHandler: ICommandHandler { public void Execute(SimpleCommand command) { //some logic } […]

这是“混蛋注射反模式”的一个很好的例子吗?

我看到首席开发人员编写这样的代码并在阅读Mark Seemann的书“.NET中的dependency injection”时,我想知道特定的“新”是否是“外来的”,因此“Bastard Injection”? public class SessionInitServiceManager { protected readonly ICESTraceManager _traceManager; protected readonly ILogger _logger; protected readonly IAggregateCalls _aggregator; protected readonly IMultiCoreRepository _repository; public SessionInitServiceManager(ICESTraceManager traceManager, ILogger logger, IAggregateCalls aggregator, IMultiCoreRepository repository) { _traceManager = traceManager; _logger = logger; _aggregator = aggregator; _repository = repository; } public SessionInitServiceManager() : this(new CESTraceManager(), new Logger(“BusinessServices.authenticateUser”), new […]