我不知道这是不是一个特定的问题,如果可能的话,但是我不得不移植一个使用Castle Windsor到Unity的应用程序,这样就不会依赖非Microsoft认可的库了.我知道我知道但是你打算做什么.
无论如何我已经设法了,但我对我所拥有的东西不满意.在温莎我有这个:
Register(
AllTypes.Of(typeof(AbstractPresenter<>)).FromAssemblyNamed("Links.Mvp"),
AllTypes.Of(typeof(IView)).FromAssemblyNamed("Links.WinForms").WithService.FromInterface());
Run Code Online (Sandbox Code Playgroud)
我已经团结一致地转变为这个
RegisterType<IMainView, MainView>();
RegisterType<IConfigureLinkView, ConfigureLinkView>();
RegisterType<IConfigureSourceView, ConfigureSourceView>();
RegisterType<IConfigureSinkView, ConfigureSinkView>();
RegisterType<MainPresenter, MainPresenter>();
RegisterType<ConfigureLinkPresenter, ConfigureLinkPresenter>();
RegisterType<ConfigureSourcePresenter, ConfigureSourcePresenter>();
RegisterType<ConfigureSinkPresenter, ConfigureSinkPresenter>();
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我必须注册每一件事,而不是能够使用某种自动配置.所以我的问题是:在团结中有更好的方法吗?
谢谢,
亚当.
我对Unity和依赖注入有些新意.我正在尝试编写一个类似于下面的单元测试:
[Test]
public void Test()
{
UnityContainer container = new UnityContainer();
DynamicMock myMock = new DynamicMock(typeof(IMyInterface));
container.RegisterInstance(typeof(IMyInterface), myMock.MockInstance); //Error here
// Continue unit test...
}
Run Code Online (Sandbox Code Playgroud)
执行此测试时,容器会在RegisterInstance方法中使用该消息抛出ArgumentNullException Value cannot be null. Parameter name: assignmentValueType.
堆栈跟踪的顶行是at Microsoft.Practices.Unity.Utility.Guard.TypeIsAssignable(Type assignmentTargetType, Type assignmentValueType, String argumentName).
为什么我不能在UnityContainer中注册MockInstance,我该如何解决这个问题呢?
nunit dependency-injection mocking ioc-container unity-container
我正在尝试使用带有实体框架的Unity Framework.让我解释一下这个场景.假设我有一个包含5个表的数据库.我将有5个接口,每个接口对应于数据库中的一个表,其中每个表的字段作为成员.现在我希望我的Entity Framework生成的类实现相应的表接口.所有导航属性都应将对象作为接口引用返回.这应该允许我使用Unity框架解析这些实体,以允许任何人在不破坏所需代码的情况下扩展EF(数据)实体.这可能吗?
c# entity entity-framework inversion-of-control unity-container
我有一个实现两个接口的类,我想对类的方法应用拦截.
我遵循Unity中的建议将两个接口作为一个单独的接口,但我对结果感到惊讶.简而言之,似乎我的CallHandler被调用了两次.我有一个最简单的例子是:
public interface I1
{
void Method1();
}
public interface I2
{
void Method2();
}
public class C : I1, I2
{
[Log]
public void Method1() {}
public void Method2() {}
}
public class LogAttribute : HandlerAttribute
{
public override ICallHandler CreateHandler(IUnityContainer container)
{
return new LogCallHandler();
}
}
public class LogCallHandler : ICallHandler
{
public IMethodReturn Invoke(
IMethodInvocation input, GetNextHandlerDelegate getNext)
{
Console.WriteLine("Entering " + input.MethodBase.Name);
var methodReturn = getNext().Invoke(input, getNext);
Console.WriteLine("Leaving " + …Run Code Online (Sandbox Code Playgroud) 我有一个代表聊天室的类,并且依赖于记录器.它与具有横切关注点的系统级记录器不同,而是与特定聊天室相关联的记录器.它将该聊天室中的所有活动记录到其唯一的日志文件中.创建聊天室时,我想打开日志文件,当它被销毁时,我想关闭日志文件.
这是我正在使用的相关代码.
public interface IChatroomLogger
{
void Log(ServerPacket packet);
void Open();
void Close();
}
public class ChatroomLogger : IChatroomLogger
{
// chatroom name will be used as a file name
public ChatroomLogger(string chatroomName) { ... }
public void Log(ServerPacket packet) { ... }
}
public class Chatroom
{
public Chatroom(string name, IChatroomLogger logger)
{
this.name = name;
this.logger = logger;
this.logger.Open();
}
public IChatromLogger Logger { get { return this.logger; } }
}
public interface IChatManager
{
Chatroom …Run Code Online (Sandbox Code Playgroud) 给定以下构造函数签名:
public MyClass(Class1 arg1, params Class2[] arg2)
Run Code Online (Sandbox Code Playgroud)
如何使用InjectionConstructor在Unity中注册MyClass传递特定的命名类型.
我尝试过以下方法:
unityContainer.RegisterType<IMyClass, MyClass>(new InjectionConstructor(typeof(Class1), new ResolvedParameter<IClass2>("name1"), new ResolvedParameter<IClass2>("name2")))
Run Code Online (Sandbox Code Playgroud)
但得到一个例外,说没有签名的构造函数:
public MyClass(Class1 arg1, Class2 arg2, Class2 arg3)
Run Code Online (Sandbox Code Playgroud)
我通过重载MyClass构造函数解决了这个问题.有没有办法在不重载构造函数的情况下执行此操作?
这是一个我几个小时都在苦苦挣扎的难题.这是背景:
Unity IoC在MVC4网络应用程序中使用ApiControllers (IHttpController)和一堆常规控制器(IController)IControllers很好IHttpControllers已经运行了好几个月,现在因类型解析错误而失败,其中包含详细信息" The type InjectionPolicy cannot be constructed. You must configure the container to supply this value"ICustomerService依赖关系的依赖关系,该ICustomerRepository依赖关系又具有IClient依赖关系,但这些依赖关系都不是新的或不同的.ICustomerService从ApiController解决问题中删除依赖性(但该接口已明确配置并在其他地方工作)HttpControllerActivatorUnity中注册并拦截了解决IHttpControllers它的调用- 它在容器上失败.解析,即使查看容器,控制器显然也在那里.我搜索了这个特定的错误并没有找到任何结果.有没有人见过它或它是否给你任何关于可能发生的事情的线索?
非常感谢!
更新:我已将其缩小到ICustomerRepository分辨率(再次,在所有其他流程中工作).错误是:
Resolving Xxx.CustomerRepository,(none) (mapped from Xxx.ICustomerRepository, (none))
Resolving Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior,(none)
Resolving parameter "policies" of constructor Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior(Microsoft.Practices.Unity.InterceptionExtension.CurrentInterceptionRequest interceptionRequest, Microsoft.Practices.Unity.InterceptionExtension.InjectionPolicy[] policies, Microsoft.Practices.Unity.IUnityContainer container)
Resolving Microsoft.Practices.Unity.InterceptionExtension.InjectionPolicy[],(none)
Run Code Online (Sandbox Code Playgroud) ioc-container inversion-of-control unity-container asp.net-mvc-4
我正在使用ASP.Net MVC 4,EF和Unity for DI.还使用UnitOfWork模式.试图找出实现这一目标的最佳方法.我有如下代码.我遇到的问题是Business和Repository层中的Dispose()现在永远不会被调用,只有两个中的Destructor被调用,所以对象似乎永远不会被处理掉.请回答以下问题
我是否真的需要Business和Repository层中的IDisposable实现(如果Unity已经在处理它)
我应该怎么做才能调用Dispose()(我应该将它添加到Controller中还是Dispose所有其他对象或使用某些特定的LifeTime管理器)
我是否应该使用每个的Singleton实例,或者将它放在每个请求中,就像它在Web环境中一样.
的Global.asax.cs:
private static IUnityContainer _unityContainer;
protected void Application_Start()
{
_unityContainer = UnityBootstrapper.SetupUnity();
_unityContainer.RegisterType<IController, ProductController>("Product");
DependencyResolver.SetResolver(new Microsoft.Practices.Unity.Mvc.UnityDependencyResolver(_unityContainer));
}
Run Code Online (Sandbox Code Playgroud)
UnityBootstrapper.cs:
public class UnityBootstrapper
{
public static IUnityContainer SetupUnity()
{
UnityContainer container = new UnityContainer();
container.RegisterType<IProductDbContext, ProductDbContext>()
.RegisterType<IUnitOfWork, UnitofWork>(new InjectionConstructor(new ResolvedParameter(typeof(IProductDbContext))))
.RegisterType<IProductRepository, ProductRepository>()
.RegisterType<IProductBusiness, ProductBusiness>();
}
}
Run Code Online (Sandbox Code Playgroud)
ProductController.cs:
public class ProductController : ControllerBase
{
private readonly IProductBusiness _productBusiness;
public ProductController(IProductBusiness productBusiness)
{
_productBusiness = productBusiness;
}
//No Dispose for this
}
Run Code Online (Sandbox Code Playgroud)
ProductBusiness.cs:
public class ProductBusiness …Run Code Online (Sandbox Code Playgroud) c# dependency-injection idisposable unity-container asp.net-mvc-4
我想用需要参数的工厂方法在Unity容器中注册一个类型。这些参数应统一解决,但只能在运行时解决。
工厂方法代码:
public static IApp Create(IOne, ITwo) {...}
Run Code Online (Sandbox Code Playgroud)
注册代码:
container.RegisterType(typeof(IApp), new InjectionFactory(f => App.Create(???, ???)));
Run Code Online (Sandbox Code Playgroud)
我需要什么来替换“ ???” ?
更多信息:
我的Unity配置分为两个阶段。第一阶段(应用程序正在启动),我注册了除一个对象之外的所有对象:
container.RegisterType<IOne, One>();
container.RegisterType<ITwo, Two>();
container.RegisterType<IApp, App>();
// ...
Run Code Online (Sandbox Code Playgroud)
第二阶段(用户登录),我中庸之道注册谁在我所有的类的构造函数中使用的上下文对象的实例(One,Two,App,...):
var childContainer = container.CreateChildContainer();
childContainer.RegisterInstance<AppEnvironment>(new AppEnvironment(userName));
Run Code Online (Sandbox Code Playgroud)
这是我的代码,不使用InjectionFactory。工作正常。现在,我必须多次注册IApp接口,并每次都调用一个不同的静态方法。静态方法示例:
public static IApp Create(IOne one, ITwo two, AppEnvironment env)
{
_one = one;
_two = two;
_env = env;
}
Run Code Online (Sandbox Code Playgroud)
如果我向IApp注册此代码:
container.Register(typeof(IApp), new InjectionFactory(f => App.Create(container.Resolve<IOne>(), container.Resolve<ITwo>(), container.Resolve<AppEnvironment>()));
Run Code Online (Sandbox Code Playgroud)
然后没有设置我的env变量。
但是,如果我注册了我的env实例(仅出于测试目的,我不能这样做),它就可以工作。
我有两个问题。我安装了Unity和Unity MVC软件包开始对其进行调查。通过注册一些类型,我得到了一些简单的控制器构造函数注入,因此运行顺利。我注意到,由于以下原因,甚至在global.asax被击中之前,它就已经开始了:
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(example.org.UnityMvcActivator), nameof(example.org.UnityMvcActivator.Start))]
Run Code Online (Sandbox Code Playgroud)
问题1:此语法的名称甚至是什么?某种将类型/方法附加到其他地方的嵌入式汇编引用?我以前从未见过。
问题2:UnityMvcActivator具有与上面的[assembly]行相对应的Start()和Shutdown()方法,其中的后一项仅在容器上进行处理。Start()的核心元素是调用:
DependencyResolver.SetResolver(new UnityDependencyResolver(UnityConfig.Container));
Run Code Online (Sandbox Code Playgroud)
是否存在一个令人信服的原因(例如dispose()这样的UnityMvcActivator存在),或者它可能需要尽早启动,或者只是为了让我开始而做的简单脚手架,我也可以将SetResolver()行放入其中我的global.asax,然后删除此类?
unity-container ×10
c# ×7
.net ×2
asp.net-mvc ×1
entity ×1
factory ×1
idisposable ×1
mocking ×1
nunit ×1