下面解释如何在代码中执行此操作: Unity将两个接口注册为一个单例
_container.RegisterType<EventService>(new ContainerControlledLifetimeManager());
_container.RegisterType<IEventService, EventService>();
_container.RegisterType<IEventServiceInformation, EventService>();
bool singleton = ReferenceEquals(_container.Resolve<IEventService>(), _container.Resolve<IEventServiceInformation>());
Run Code Online (Sandbox Code Playgroud)
如何在XML配置中执行此操作?
我有几个类依赖于类ILogger.实现ILogger需要知道它是记录器的类型,即ILoggerfor Foo将是new Logger(typeof(Foo)),因为Bar它将是new Logger(typeof(Bar)),等等.
我希望Unity自动注入合适的记录器; 换句话说,当我打电话时container.Resolve<Foo>(),我希望将a new Logger(typeof(Foo))注入到Foo实例中.
如何在Unity中进行设置?有没有办法将被解析的类型传递给依赖项?
(在我真正的代码,我其实有ILoggerFactory一个Create方法,这也需要一个类型作为参数,所以我可以在工厂只是传递给我的课,他们会叫Create自己能得到相应的记录仪,但它不是那么优雅正如我想要实现的那样)
一些代码使事情更清晰:
interface ILogger
{
...
}
class Logger : ILogger
{
private readonly Type _type;
public Logger(Type type)
{
_type = type;
}
...
}
class Foo
{
private readonly ILogger _logger;
public Foo(ILogger logger) // here I want a Logger with its type set to …Run Code Online (Sandbox Code Playgroud) 我创建了一个我输入的ISearchable接口,以便我可以检索结果的IEnumerable.
我有许多服务为不同的域对象实现ISearchable ...
Container.RegisterType<ISearchable<Animal>, AnimalService>();
Container.RegisterType<ISearchable<Fish>, FishService>();
Run Code Online (Sandbox Code Playgroud)
我想根据类型解决(通过Unity)一个ISearchable,但我正努力让它工作......
以下dos不能编译,但希望能够了解我正在努力实现的目标.
Type t = typeof(Animal);
var searchProvider = _container.Resolve<ISearchable<t>>();
Run Code Online (Sandbox Code Playgroud)
任何帮助感激地收到!
谢谢,
安迪
有没有办法让Unity将属性注入的对象作为拆解的一部分进行处理?
背景是我正在使用ASP.NET MVC 2,Unity和WCF的应用程序.我们编写了自己的MVC控制器工厂,它使用unity实例化控制器,并使用控制器公共属性的[Dependency]属性注入WCF代理.在页面生命周期结束时,调用控制器工厂的ReleaseController方法,并调用IUnityContainer.Teardown(theMvcController).此时控制器按预期处理,但我还需要处理注入的wcf代理.(实际上我需要对它们调用Close和/或Abort而不是Dispose但这是后来的问题.)
我当然可以覆盖控制器的Dispose方法并清理那里的代理,但我不希望控制器必须知道注入接口的生命周期,甚至是它们引用WCF代理.
如果我需要自己编写代码 - 什么是最好的扩展点?我很欣赏任何指针.
在一个新的WPF项目(VS2010)中,我第一次使用Unity 2.在这个项目中,我使用以下结构:
解
WPF项目
类库1
班级图书馆2
班级图书馆3 ....
使用Unity注册不同类型是在WPF Project中使用以下代码段完成的:
IUnityContainer container = new UnityContainer()
.RegisterType<IObjectContext, ObjectContextAdapter>()
.RegisterType<IConnectionStringProvider, ConnectionStringProvider>()
.RegisterType(typeof(IRepository<>), typeof(Repository<>));
Run Code Online (Sandbox Code Playgroud)
现在让我说我想<Orders在类库1中解析Repository > constructor-inject.显然,容器在其他项目中并不为人所知!
我该怎么做?
为我的MVC 2应用程序创建引导程序的最佳方法是什么?我正在使用Unity和AutoMapper,并希望尽可能地抽象它们的加载和配置.
这里有一个很好的例子(http://weblogs.asp.net/rashid/archive/2009/02/17/use-bootstrapper-in-your-asp-net-mvc-application-and-reduce-code-smell. aspx ),但UnityContainer实现了IDisposable,在这个例子中它永远不会被清除.这(在Bootstrapper中配置自动映射违反了开放 - 封闭原则?)也是一个不错的例子,但他也没有处理Unity/Disposable问题.
这是(http://www.dominicpettifer.co.uk/Blog/42/put-an-ioc-powered-bootstrapper-in-your-asp-net-mvc-application)另一个如何做Bootstrapper的好例子,但同样没有解决Unity/Disposable问题.
我想在一个静态变量中保持我的Bootstrapper对象,并使它实现IDisposable,但这听起来不对.
那里有没有好的UnityContainerExtension例子/教程?
请不要参考Unity Event Broker示例.
这更像是一个评论而不是一个问题,尽管反馈会很好.我的任务是为我们正在做的新项目创建用户界面.我们想使用WPF,我想学习所有现代UI设计技术.由于我是WPF的新手,我一直在研究可用的东西.我想我已经基本上决定使用MVVM Light Toolkit(主要是因为它的"可混合性"和EventToCommand行为!),但我也希望合并IoC.所以,这就是我想出的.我修改了MVVM Light项目中的默认ViewModelLocator类,以使用UnityContainer来处理依赖注入.考虑到我不知道这些术语的90%在3个月前是什么意思,我认为我走在了正确的轨道上.
// Example of MVVM Light Toolkit ViewModelLocator class that implements Microsoft
// Unity 2.0 Inversion of Control container to resolve ViewModel dependencies.
using Microsoft.Practices.Unity;
namespace MVVMLightUnityExample
{
public class ViewModelLocator
{
public static UnityContainer Container { get; set; }
#region Constructors
static ViewModelLocator()
{
if (Container == null)
{
Container = new UnityContainer();
// register all dependencies required by view models
Container
.RegisterType<IDialogService, ModalDialogService>(new ContainerControlledLifetimeManager())
.RegisterType<ILoggerService, LogFileService>(new ContainerControlledLifetimeManager())
;
}
}
/// <summary>
/// Initializes a …Run Code Online (Sandbox Code Playgroud) 我有一个ICallHandler,我想在我的所有Unity容器实例中注册.
例如,采用以下处理程序:
public class ProfilerHandler : ICallHandler
{
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
//start timer
IMethodReturn methodReturn = getNext()(input, getNext);
//stop timer
}
public int Order
{
get; set;
}
}
Run Code Online (Sandbox Code Playgroud)
以下是IoC容器构造函数:
public class IoCContainer : UnityContainer
{
public IoCContainer()
{
this.RegisterType<IUserService, UserService>(new ContainerControlledLifetimeManager());
this.RegisterType<IRepository<User>, UserRepository>(new ContainerControlledLifetimeManager());
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的就是使用所有这些类型注册此处理程序.
我可以用一些非常详细的代码来做这件事:
public class IoCContainer : UnityContainer
{
public IoCContainer()
{
this.AddNewExtension<Interception>();
this.RegisterType<IUserService, UserService>(new ContainerControlledLifetimeManager()).Configure<Interception>().SetInterceptorFor<IUserService>(new InterfaceInterceptor());
this.RegisterType<IRepository<User>, UserRepository>(new ContainerControlledLifetimeManager()).Configure<Interception>().SetInterceptorFor<IRepository<User>>(new InterfaceInterceptor());
}
}
Run Code Online (Sandbox Code Playgroud)
但是我不仅要在我的所有类型注册上编写相同的拦截代码(想象一下,如果我有100多个类型注册),但我也必须HandlerAttribute在每个接口上包含一个(再次,如果我有100多个接口,那就不好了)适用于此).
这是我唯一的选择,还是有办法在容器级别执行此操作以避免必须将其应用于每个单独的类型注册和界面?
我在Unity文档中看到的示例让您通过new LifetimeManager()内联指定生命周期管理器.所以我有这个代码:
container.RegisterType<ApplicationDbContext>(new PerRequestLifetimeManager());
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new PerRequestLifetimeManager(),
new InjectionConstructor(typeof (ApplicationDbContext)));
container.RegisterType<UserManager<ApplicationUser>>(new PerRequestLifetimeManager());
Run Code Online (Sandbox Code Playgroud)
很好,但我想知道为什么我要创建这么多实例.我有什么理由不这样写吗?
var lifetimeManager = new PerRequestLifetimeManager();
container.RegisterType<ApplicationDbContext>(lifetimeManager);
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(lifetimeManager,
new InjectionConstructor(typeof (ApplicationDbContext)));
container.RegisterType<UserManager<ApplicationUser>>(lifetimeManager);
Run Code Online (Sandbox Code Playgroud)
这看起来很明显但是通过PDF阅读所有的例子都是前一种风格而没有评论,所以我想知道我是否不理解它是如何工作的.
unity-container ×10
c# ×5
.net ×2
aop ×1
asp.net-mvc ×1
automapper ×1
bootstrapper ×1
idisposable ×1
mvvm-light ×1
wcf-proxy ×1
wpf ×1