dependency-injection

How to use log4net with Dependency Injection

*爱你&永不变心* 提交于 2019-12-17 15:19:07
问题 I'm trying to figure out what the right patter and usage of log4net is with a dependency injection framework. Log4Net uses the ILog interface but requires me to call LogManager.GetLogger(Reflection.MethodBase.GetCurrentMethod().DeclaringType) in each class or method where I need to log information. This seems to go against IoC principles and couples me to using Log4Net. Should I somehow put in another layer of abstraction somewhere? Also, I need to log custom properties like the current user

What is the difference between Strategy pattern and Dependency Injection?

别说谁变了你拦得住时间么 提交于 2019-12-17 15:05:02
问题 Strategy pattern and Dependency Injection both allow us to set / inject objects at run time. What is the difference between Strategy pattern and Dependency Injection? 回答1: DI and Strategy work in the same way, but Strategy is used for more fine-grained and short-lived dependencies. When an object is configured with a "fixed" Strategy, for example when the object is constructed, the distinction between Strategy and DI blurs. But in a DI scenario it is more unusual that the dependencies of

How do I handle classes with static methods with Ninject?

女生的网名这么多〃 提交于 2019-12-17 12:42:55
问题 How do I handle classes with static methods with Ninject? That is, in C# one can not have static methods in an interface, and Ninject works on the basis of using interfaces? My use case is a class that I would like it to have a static method to create an unpopulated instance of itself. EDIT 1 Just to add an example in the TopologyImp class, in the GetRootNodes() method, how would I create some iNode classes to return? Would I construct these with normal code practice or would I somehow use

How to resolve Autofac InstancePerHttpRequest

社会主义新天地 提交于 2019-12-17 10:46:33
问题 I have registered a component like this in my Global.asax.cs: ContainerBuilder builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterType<WebWorkContext>().As<IWorkContext>().InstancePerHttpRequest(); IContainer container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); // This is where my error happens, not sure why? var workContext = container.Resolve<IWorkContext>(); WebWorkContext class:

PHP Dependency Injection

谁都会走 提交于 2019-12-17 10:44:42
问题 I'm trying to get my head around Dependency Injection and I understand it, for the most part. However, say if, for some reason, one of my classes was dependent on several classes, instead of passing all of these to this one class in the constructor, is there a better, more sensible method? I've heard about DI Containers, is this how I would go about solving this problem? Where should I start with this solution? Do I pass the dependencies to my DIC, and then pass this to the class that needs

How to get bean using application context in spring boot

冷暖自知 提交于 2019-12-17 10:32:48
问题 I am developing a SpringBoot project and I want to get the bean by its name using applicationContext . I have tried many solution from web but could not succeed. My Requirement is that I have a controller ControllerA and inside the controller I have a method getBean(String className) . I want to get instance of registered bean. I have hibernate entities and I want to get an instance of the bean by passing the name of class only in getBean method. Please help if someone know the solution. 回答1:

Difference between Dependency Injection and Mocking framework (Ninject vs RhinoMock or Moq)

断了今生、忘了曾经 提交于 2019-12-17 10:18:40
问题 So what is the difference between Ninject & a mocking framework like RhinoMock or moq? I google'd this but it is still unclear. 回答1: Ninject is Dependency Injection for .NET. RhinoMocks and Moq are both mocking frameworks. Now both have nothing to do with each other. I really had trouble understanding both so here I go trying to explain. Dependency Injection : is an implementation (lets call it) of Inversion of Control. You don't confuse the two. You are taking the control of creating an

Dependency injection and named loggers

你。 提交于 2019-12-17 10:08:32
问题 I am interested in learning more about how people inject logging with dependency injection platforms. Although the links below and my examples refer to log4net and Unity, I am not necessarily going to use either of those. For dependency injection/IOC, I will probably use MEF as that is the standard that the rest of the project (large) is settling on. I am very new to dependency injection/ioc and am pretty new to C# and .NET (have written very little production code in C#/.NET after the past

How to use a DI / IoC container with the model binder in ASP.NET MVC 2+?

南笙酒味 提交于 2019-12-17 09:55:52
问题 Let's say I have an User entity and I would want to set it's CreationTime property in the constructor to DateTime.Now. But being a unit test adopter I don't want to access DateTime.Now directly but use an ITimeProvider : public class User { public User(ITimeProvider timeProvider) { // ... this.CreationTime = timeProvider.Now; } // ..... } public interface ITimeProvider { public DateTime Now { get; } } public class TimeProvider : ITimeProvider { public DateTime Now { get { return DateTime.Now;

Accessing Guice injector in its Module?

試著忘記壹切 提交于 2019-12-17 09:22:35
问题 I am extending Guice's AbstractModule and inside of the extending class I need access to Guice's injector. It that possible, if yes, how? 回答1: This is an unusual request. Modules are more like config files than logic files: The Module is read to create the Injector, and then as soon as the Injector is created the module has done its job. For a simple Module, the Injector literally doesn't exist until the Module is ready to be discarded. In any case, rather than requesting an Injector to get