在某些情况下,单元测试不适用于项目.
我正在研究控制反转和依赖注入实用程序,我想知道是否有充分的理由使用它比使单元测试更容易.
--update
好吧,让我们分析一下引用的优点:减少耦合.从子类型中取出耦合,并将耦合添加到需要创建要注入的对象的处理程序类型.
没有单元测试,这种耦合传输的优势是什么(不是耦合消除).
正如在Autofac Wiki上所讨论的,为类自动注入log4net.ILog实现的最佳方法是使用LogInjectionModule.该模块的实现在wiki文章中给出:
public class LogInjectionModule : Module
{
protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration)
{
if (registration == null) throw new ArgumentNullException("registration");
registration.Preparing += OnComponentPreparing;
}
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 == typeof(ILog), (p, i) => LogManager.GetLogger(t))
});
}
}
Run Code Online (Sandbox Code Playgroud)
我将此模块与我自己的模块一起包括在内以配置一些组件:
var builder = new ContainerBuilder();
builder.RegisterModule(new Common.Logging.LogInjectionModule());
builder.RegisterModule(new DataLayer.DataLayerModule("connectionstring"));
builder.RegisterType<SomeServiceType>();
AutofacHostFactory.Container = builder.Build();
Run Code Online (Sandbox Code Playgroud)
里面DataLayerModule我构造类型,如下所示:
protected …Run Code Online (Sandbox Code Playgroud) 当使用IoC进行依赖注入时,最推荐的用法是构造函数注入(正如许多文章所述),我的问题是:
使用构造函数参数注入或通过构造函数传递IoC容器注入所需的类是什么更好对单元测试更有用吗?
所以,我一直在建立一个全新的网站来取代我们目前的经典asp网站.我建立了一个基础项目,并添加了我将要使用的所有不同技术.
Ninject Automapper PagedList EF 4.0
到目前为止,我有一个网站的工作原型.它从数据库中提取数据,并显示在我的页面上.我想我没有正确地遵循模式.因为我的控制器动作看起来很奇 我开始认为我需要更改注射以将我的服务注入我的viewmodel?将服务插入ViewModel是否常见?
public class ParcelDetailViewModel
{
public Property Property { get; set; }
public int CurrentYear { get; set; }
public IEnumerable<AltOwnership> AltOwnership { get; set; }
public Ownership Ownership { get; set; }
public TotalValues TotalValues { get; set; }
public SiteAddressViewModel SiteAddress { get; set; }
public Value CurrentValues { get; set; }
public Transfer LatestTransfer { get; set; }
public LegalDescription LegalDescription { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
public class …Run Code Online (Sandbox Code Playgroud) 我正在寻找Spring IoC容器的替代品.我喜欢Spring,但即使我只使用核心功能,它也非常重(罐子大小).这对WAR应用程序来说很痛苦,对Android设备来说绝对不能接受.
所以这就是我需要的:
我发现了木薯粉,但正如我所看到的,这是一个新项目并没有广泛使用,所以我担心它会出错,不是很有用,很快就会被抛弃.
那么,你有没有(最好的)用更轻的IoC容器替换Spring的经验,可以推荐一些东西吗?
是否类似于Java的java.util.ServiceLoader,它允许通过SPI动态查找和加载组件?
我正在寻找一些.NET的轻量级原生功能,它允许我轻松插入接口提供程序,而不是笨重的DI容器或自己编写它.
更新:这似乎不适用于.NET,因为没有像Java中那样本机内置的SPI功能.我接受了第一个答案,因为它最接近我所追求的.
.net dependency-injection inversion-of-control service-provider
我有一个大班级.当我的应用程序启动时,我初始化UnityContainer对象并进行配置.之后,我总是将它通过构造函数传递给层次结构中的另一个类.像这样的东西:
Unity容器将这些类作为注册:IClassA,IClassB,IClassC,IClassD
接口的所有具体实现都具有带IUnityContainer参数的构造函数.例如,
public class ClassA : IClassA
{
public ClassA(IUnityContainer unityContainer)
{
}
}
Run Code Online (Sandbox Code Playgroud)
因此,每当我创建某个类的新实例时,我必须传递IUnityContainer的对象.
我可以减少传递IUnityContainer对象的数量作为构造函数的参数吗?也许通过使用Dependency属性?
.net c# dependency-injection inversion-of-control unity-container
我有一个多租户Web应用程序,它有多个模块.我使用autofac作为IOC和Automapper来映射我的模型和实体.在下面的代码中,我能够将UserAuthenticationMapper配置文件添加到automapper,但无法添加CustomerDataMapper配置文件.Autofac仅向自动化MappingEngine添加一个映射器配置文件,并忽略其余部分.我在做什么错了?
这是我的UserAuthentication模块:
public class UserAuthenticationModule: Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<MappingEngine>().As<IMappingEngine>();
builder.Register(ctx =>
{
var configStore = new ConfigurationStore
(new TypeMapFactory(), MapperRegistry.AllMappers());
configStore.AddProfile<UserAuthenticationMapperProfile>();
return configStore;
})
.AsImplementedInterfaces()
.SingleInstance();
var assembly = Assembly.GetExecutingAssembly();
builder.RegisterAssemblyTypes(assembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.SingleInstance();
}
}
Run Code Online (Sandbox Code Playgroud)
CustomerData Modue:
public class CustomerDataModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<CustomerDataService>()
.As<ICustomerDataService>()
.InstancePerDependency();
builder.RegisterType<MappingEngine>().As<IMappingEngine>();
builder.Register(ctx =>
{
var configStore = new ConfigurationStore
(new TypeMapFactory(), MapperRegistry.AllMappers());
configStore.AddProfile<CustomerDataMapperProfile>();
return configStore;
})
.AsImplementedInterfaces()
.SingleInstance();
}
} …Run Code Online (Sandbox Code Playgroud) 我在服务层的应用程序中遇到依赖性问题.
我有以下课程:
<?php
class UserService{
private $userRepository;
private $vocationService;
private $roleService;
public function __construct(UserRepository $userRepository, VocationService $vocationService, RoleService $roleService)
{
$this->userRepository = $userRepository;
$this->vocationService = $vocationService;
$this->roleService = $roleService;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在注入的只有三个依赖项.假设,我想添加下一个依赖项,例如:NextService.我的构造函数会再次增长.
如果我想在构造函数中传递更多依赖项,该怎么办?
也许我应该通过传递IoC容器然后得到理想的类来解决这个问题?这是一个例子:
<?php
class UserService{
private $userRepository;
private $vocationService;
private $roleService;
public function __construct(ContainerInterface $container)
{
$this->userRepository = $container->get('userRepo');
$this->vocationService = $container->get('vocService');
$this->roleService = $container->get('roleService');
}
}
Run Code Online (Sandbox Code Playgroud)
但现在我的UserService类依赖于我注入的IoC容器.
如何通过良好实践解决问题?
问候,亚当
php dependency-injection inversion-of-control symfony symfony-2.5
我收到此错误:
An exception of type 'System.NullReferenceException' occurred in PubStuff.Intern.Web.Internal.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object
public class InternController : BaseController
{
IInternService _internService;
public InternController() { }
public InternController(IInternService internService)
{
_internService = internService;
}
// GET: Intern
public ActionResult Index()
{
object responseObject = null;
responseObject = _internService.GetAllSkills();
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
responseObject = _internService.GetAllSkills();函数,那么这一行就会抛出错误._internService为null
我该如何解决? 有什么问题?
更新 我最终遇到了StructureMap的问题,无论我是否添加了IInternUnitOfWork …
c# ×5
.net ×2
autofac ×2
android ×1
asp.net ×1
asp.net-mvc ×1
automapper ×1
interface ×1
java ×1
log4net ×1
php ×1
spring ×1
structuremap ×1
symfony ×1
symfony-2.5 ×1
unit-testing ×1