我有一个统一容器,我正在这样注册类型:
IUnityContainer container = new UnityContainer()
.RegisterType<ITaxAuthorityRateService, TaxAuthorityPopulationRateService>( "PopulationRate" )
.RegisterType<ITaxAuthorityRateService, TaxAuthorityBusinessLicenseRateService>( "BusinessLicenseRate" );
Run Code Online (Sandbox Code Playgroud)
然后我还要注册2个不同的服务,这些服务在其构造函数中包含一个ITaxAuthorityRateService变量.两种服务都需要一个派生自ITaxAuthorityRateService的不同类.我该如何处理这种情况?
我有一个要解析的接口,其中一个映射对象的依赖项有一个属性,我想设置一个值,我只有在解析顶级对象时才可用.
该属性没有有效的默认值.如果未设置,则它应为null,并且只有在解析时可用的值不为空时才应设置它.
这种有条件的财产注入可能吗?
我试过这个......
container.RegisterType<ProductInstanceValidatorBase, CartItemPurchaseTypeValidator>("CartItemPurchaseTypeValidator", new InjectionProperty("AccountEntity", null);
Run Code Online (Sandbox Code Playgroud)
...但它说我不能使用空值!
我也试过这个决心......
container.Resolve<ProductInstanceValidatorBase>(new PropertyOverride("AccountEntity", value));
Run Code Online (Sandbox Code Playgroud)
...但是当值为null时抛出异常.它说,
参数类型推断不适用于空值.使用正确配置的InjectionParameter或InjectionParameter类实例显式指示参数类型.参数名称:parameterValue
基本上我想要注册一个只用覆盖设置的属性,然后才能覆盖值为非null.有任何想法吗?当然,从语义的角度来看,属性注入应该是可选的.
干杯,伊恩.
public object GetService(Type serviceType)
{
object resolvedObject = null;
try
{
resolvedObject = _unityContainer.Resolve(serviceType);
}
catch(ResolutionFailedException e) { }
return resolvedObject;
}
Run Code Online (Sandbox Code Playgroud)
这是捕获异常并返回null的最佳实践吗?默认情况下,mvc尝试解析IControllerFactory和IViewPageActivator,即我得到两个catched异常.我不喜欢这个解决方案.
我的朋友建议检查serviceType IControllerFactory,IViewPageActivator如果引发异常 - 捕获并记录错误.但就我而言 - 它不是最好的解决方案,它就像硬编码一样.
asp.net-mvc dependency-injection unity-container asp.net-mvc-3
我正在考虑在我的项目中使用DI和Unity.我有一个问题:如何减少耦合?从我的观点来看,它是耦合增加,因为:
我需要创建UnityContainer并在那里注册所有类型.这意味着我需要引用程序集中创建此容器的所有程序集.
IUnityContainer UnityContainer;
//....
IUnityContainer UnityContainer= new UnityContainer();
UnityContainer.RegisterType<IMyService, CustomerService>();
Run Code Online (Sandbox Code Playgroud)我需要使用Resolve创建我的服务实例,但这意味着我需要对容器的程序集进行引用.
var service = SomeClassWithContainer.UnityContainer.Resolve<IMyService>();
Run Code Online (Sandbox Code Playgroud)我误解了某些东西,或者实际上它是否正在增加耦合?
我在ASP.NET应用程序中配置了Unity,并在Application_BeginRequest中收到第一个请求时加载配置.然后Unity容器作为属性存储在Global.ascx中,以便我的其他类可以访问它:
public static IUnityContainer ContainerHolder { get; set; }
IUnityContainer IContainerAccessor.Container
{
get { return ContainerHolder; }
}
Run Code Online (Sandbox Code Playgroud)
ContainerHolder,跨应用程序保存容器实例,Container属性允许在每个会话中访问此属性.
然后我有一个UnityLocator类,它允许我跨应用程序访问此属性:
public static class UnityLocator
{
private static IUnityContainer Container
{
get
{
return ((IContainerAccessor)HttpContext.Current.ApplicationInstance).Container;
}
}
}
Run Code Online (Sandbox Code Playgroud)
一切正常!
我还有一种从Unity访问实例的方法:
UnityLocator.GetInstance<IThemeManager>();
protected Repository(ICustomCacheManager customCacheManager)
{
this.Cache = customCacheManager;
}
protected Repository()
: this(UnityLocator.GetInstance<ICustomCacheManager>())
{
}
Run Code Online (Sandbox Code Playgroud)
这已在我的应用程序中使用,以便我可以从Unity检索现有实例,以便我可以将其注入其他类.例如,我的视图(asp.net页面)将此作为依赖项注入其Presenter类.
现在,我想配置我的单元测试运行.
我怎么能这样做?!global.ascx显然不存在,所以我认为我应该创建一个BaseTest类,让我的所有测试都继承它.然后在这个BaseTest类的构造函数中,我构建了我的实例.这是正确的方法吗?
现在如何使用Unity配置单元测试?
谢谢
更新:UnityLocator.GetInstance添加.
Jason Dolinger在他的视频中(现在热门)www.lab49.com/files/videos/Jason%20Dolinger%20MVVM.wmv(从0.59到1.04)使用这样的代码:
public partial App: Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
IUnityContainer container = new UnityContainer();
RandomQuoteSource source = new RandomQuoteSource();
container.RegisterInstance<IQuoteSource>(source);
WatchList window = container.Resolve<WatchList>();
window.Show();
}
}
Run Code Online (Sandbox Code Playgroud)
他使用我无法找到的IUnityContainer类.据我所知,我们只是创建了一个窗口(所以container.Resolve调用可以替换new WatchList(...,也可以用某种方式RandomQouteSource作为实现关联IQouteSource,但是我不清楚如何在以后使用它.
问题是:
我正在使用Unity 1.1版本,我无法注入构造函数.我的代码是:
在global.asax中注册依赖项Application_Startup方法:
Core.Instance.Container.RegisterType<ICartBusiness, CartBusiness>();
Run Code Online (Sandbox Code Playgroud)
注入构造函数:
private ICartBusiness _business;
public FooController(ICartBusiness business)
{ _business = business; }
Run Code Online (Sandbox Code Playgroud)
mvc抛出此异常:
没有为此对象定义的无参数构造函数.
PS:我不能使用任何新版本的统一,因为我使用太过引用的旧dll所以我不能使用unity.WebApi或unity.Mvc3 dlls.
这该怎么做?
我们有一个类(实际上很多)有一个静态初始化函数,它需要在依赖注入容器中的对象上执行(使用Unity).
public class MyClass
{
public static void InitializeMappings(IMapper mapper)
{
// Do stuff to mapper
}
}
Run Code Online (Sandbox Code Playgroud)
我想InitializeMappings在Unity容器实例化新的IMapper实例时调用该函数.
我需要帮助:
InitializeMappings从容器中调用该函数mapper函数调用的参数InitializeMappings到IMapper实现的生命周期我将如何实现/定义这个?
我有一个ASP.Net应用程序,它引用一个带有一些可重用代码的程序集(常用工具,数据访问等).装配参考IBM.Data.DB2.dll.但是,我没有在我的应用程序中使用DB2,IBM.Data.DB2.dll它只是一个依赖项(如果应用程序需要连接到DB2).最近,我遇到了以下错误:
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.ReportingServices.Interfaces, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.ReportingServices.Interfaces, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'
System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeModule.GetTypes()
at System.Reflection.Assembly.GetTypes()
at Unity.AutoRegistration.AutoRegistration.<ApplyAutoRegistration>b__5(Assembly a)
at System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext()
at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
at Unity.AutoRegistration.AutoRegistration.ApplyAutoRegistration()
Run Code Online (Sandbox Code Playgroud)
整个应用程序中包含Microsoft.ReportingServices.Interfaces的唯一文件是该IBM.Data.DB2.dll文件.在部署应用程序时,我不需要SQL Server或本地安装SQL Server的开销少得多.请记住,我需要使用具有依赖性的程序集,IBM.Data.DB2.dll并且此错误在过去没有发生过,它似乎是最近的.
我尝试过绑定重定向,通过Nuget安装Microsoft.ReportingServices.Interfaces都无济于事.
有谁知道为什么会出现这个错误,更重要的是......如何解决它?
我无法想象如何将以下结构图实现转换为统一.
public AutoMapperRegistry()
{
var profiles =
from t in typeof (AutoMapperRegistry).Assembly.GetTypes()
where typeof (Profile).IsAssignableFrom(t)
select (Profile)Activator.CreateInstance(t);
var config = new MapperConfiguration(cfg =>
{
foreach (var profile in profiles)
{
cfg.AddProfile(profile);
}
});
For<MapperConfiguration>().Use(config);
For<IMapper>().Use(ctx => ctx.GetInstance<MapperConfiguration>().CreateMapper(ctx.GetInstance));
}
Run Code Online (Sandbox Code Playgroud) unity-container ×10
c# ×8
.net ×3
asp.net ×1
asp.net-mvc ×1
automapper-4 ×1
db2 ×1
mvvm ×1
unit-testing ×1
wpf ×1