inversion-of-control

Is it mandatory for DbContext to be injected .InPerRequestScope in ASP.NET application?

*爱你&永不变心* 提交于 2019-12-12 13:25:25
问题 I have been using Ninject IoC container in my ASP.NET MVC3 portal. Whenever I've been injecting Entity Framework DbContext in PerThread scope, my data wasn't consistent, changes would not get displayd for some time after I've made changes to Entities, etc. After I've switched the IoC configuration to resolve a fresh copy of my DbContext instance for each request ( PerRequestScope() ), all the problems were gone. So is it absolutely mandatory to use PerRequest injection strategy in MVC3

DI and repository pattern

为君一笑 提交于 2019-12-12 12:26:17
问题 Currently, my code is similar to this (shortened just to make a point): DAL Repository Interface public interface IRepository<TEntity, in TKey> { IList<TEntity> GetAll(); TEntity Get(TKey id); TEntity Add(TEntity item); TEntity Update(TEntity item); bool Remove(TKey id); } Base EF repository public class BaseEFRepository<TEntity, TKey> : IRepository<TEntity, TKey> where TEntity: class, IEntity<TKey> where TKey: struct { protected readonly DbContext _dbContext; public BaseRepository() {

In Laravel, any downside to using App::make('') rather than constructor injection?

好久不见. 提交于 2019-12-12 12:15:56
问题 Normally I would just inject dependancies via the constructor, but it gets to be very verbose when the parent class has dependancies and have to pass them through all the children. The alternative is to use $this->dependancy = App::make('Dependancy') in the parent class alone. Then both the parent and child constructors can be empty. Is there any downside to doing it this way? 回答1: There is a downside to your approach, doing what you propose will make your application less testable. What I

Proper way to inject parent class dependencies with Spring annotations

对着背影说爱祢 提交于 2019-12-12 11:08:45
问题 I have following code - Dao.java @Component public class Dao extends NamedParameterJdbcDaoSupport { } dbContext.xml <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${db.driver}" /> <property name="url" value="${db.jdbc.url}" /> <property name="username" value="${db.user}" /> <property name="password" value="${db.password}" /> </bean> applicationContext.xml <context:component-scan base-package="com.kshitiz" /

How do I manage object disposal when I use IoC?

﹥>﹥吖頭↗ 提交于 2019-12-12 10:53:28
问题 My case it is Ninject 2. // normal explicit dispose using (var dc = new EFContext) { } But sometimes I need to keep the context longer or between function calls. So I want to control this behavior through IoC scope. // if i use this way. how do i make sure object is disposed. var dc = ninject.Get<IContext>() // i cannot use this since the scope can change to singleton. right ?? using (var dc = ninject.Get<IContext>()) { } Sample scopes Container.Bind<IContext>().To<EFContext>()

spring: a bean that receives a list of Classes

拥有回忆 提交于 2019-12-12 10:44:29
问题 I want to define in my Spring XML context a bean that has a property of the type List of classes: i.e. List<Class<?>> classes How do I send that bean a number of classes, say java.lang.String and java.lang.Integer? The list needs not be reusable, i.e. I will not refer to it in another bean. 回答1: With Spring, the simplest possibility usually works..... <property name="classes"> <list> <value>java.lang.String</value> <value>java.lang.Integer</value> </list> </property> 回答2: <property name=

How to use Spring to inject an object in a setter that doesnt follow the Java bean specification?

喜你入骨 提交于 2019-12-12 09:37:41
问题 I am trying to use Spring and wx-xmlrpc together. The problem is that XmlRpcClient has a setConfig() method that doesnt follow the Java Bean spec : the setter and the getter dont use the same Class. So Spring complaints when I have the following context.xml : <bean id="xmlRpcClient" class="org.apache.xmlrpc.client.XmlRpcClient"> <property name="config"> <bean class="org.apache.xmlrpc.client.XmlRpcClientConfigImpl"> <property name="serverURL" value="http://example.net" /> </bean> </property> <

A .NET Unit Test without a parameterless constructor, to facilitate dependency injection

末鹿安然 提交于 2019-12-12 08:23:08
问题 I'm trying to have the unit tests not rely on calling container.Resolve<T>() for their dependencies. I'm currently using AutoFac 2.2.4, and tried xUnit.NET and NUnit , but both have this issue : No parameterless constructor defined for this object How do I get past this issue? Is it a particular unit testing framework that will support this, or just how said framework is configured? Should I not be doing this? Or can I set up the test class to work with the constructor that has it's only

Spring - is using new a bad practice?

匆匆过客 提交于 2019-12-12 08:12:51
问题 Is creating objects by hand , i.e. using new operator instead of registering Spring bean and using dependency injection considered bad practice? I mean, does Spring IoC container have to know about all objects in the application? If so, why? 回答1: You want Spring to create beans for classes that : you want/need to inject instance(s) in other beans you need to inject beans (or dependencies) in their own instances. you want them to benefit from Spring features (instantiation management,

What does AsSelf do in autofac? [duplicate]

谁说我不能喝 提交于 2019-12-12 07:41:16
问题 This question already has an answer here : What is self-binding in an IoC container? (1 answer) Closed 2 years ago . What is AsSelf() in autofac? I am new to autofac, what exactly is AsSelf and what are the difference between the two below? builder.RegisterType<SomeType>().AsSelf().As<IService>(); builder.RegisterType<SomeType>().As<IService>(); Thank you! 回答1: Typically you would want to inject interfaces, rather than implementations into your classes. But let's assume you have: interface