RabbitMQ的使用Unity IOC容器在.NET [英] RabbitMQ with Unity IOC Container in .NET
问题描述
我使用Unity应用程序块作为我的IOC容器的WCF项目我的服务层。这工作得很好用Unity.WCF库将其插入每个WCF服务。
I am using Unity App Block as my IOC container for my service layer of a WCF project. This works quite well using the Unity.WCF library to plug it into each WCF service.
我最近推出的RabbitMQ到我的业务层和我目前使用的使用块连接,并添加到队列。我不喜欢这样,虽然,我期待使用 HierachicalLifetimeManager
来创建和销毁我的连接到RabbitMQ的,因为我需要他们?这听起来是否正确?
I recently introduced RabbitMQ into my service layer and I am currently using the "using" blocks to connect and add to the queue. I dont like this though and am looking to use the HierachicalLifetimeManager
to create and destroy my connection to RabbitMQ as I need them? Does this sound correct?
我在找这方面的一个样本,或最好的办法ATLEAST一些指导? (例如:我应该封装连接,并注入根据需要如何将封装的RabbitMQ消费等各项服务?)
I'm looking for a sample of this, or atleast some guidance on the best approach? (e.g. Should I encapsulate the connection and inject into each service as needed? How would I encapsulate RabbitMQ consumer etc?)
推荐答案
我会建议注册 IConnection
作为一个单身。
I would advise registering the IConnection
as a singleton.
要注册 IConnection
作为统一单身,你会使用 ContainerControlledLifetimeManager
,例如:
To register the IConnection
as a singleton in Unity you would use a ContainerControlledLifetimeManager
, e.g.
var connectionFactory = new ConnectionFactory
{
// Configure the connection factory
};
unityContainer.RegisterInstance(connectionFactory);
unityContainer.RegisterType<IConnection, AutorecoveringConnection>(new ContainerControlledLifetimeManager(),
new InjectionMethod("init"));
在 AutorecoveringConnection
例如,一旦解决了,第一次将生存下去,直到拥有 UnityContainer
设置。
The AutorecoveringConnection
instance, once resolved for the first time will stay alive until the owning UnityContainer
is disposed.
由于我们已经注册了连接工厂
与统一
,这将被自动注入到<$ C的构造$ C> AutorecoveringConnection 。
该 InjectionMethod
确保第一时间 AutorecoveringConnection
解决了,的init
方法被调用。
Because we have registered the ConnectionFactory
with Unity
, this will automatically be injected into the constructor of AutorecoveringConnection
.
The InjectionMethod
ensures that the first time the AutorecoveringConnection
is resolved, the init
method is invoked.
至于你是否应该从服务抽象掉RabbitMQ的,我的回答是肯定的问题,但我不会简单地创建一个 IMessageQueue
抽象。想想你使用的是什么样的目的消息队列,是它推动的状态?如果是这样,有一个具体的实施RabbitMQ的一个 IStatusNotifier
接口。如果是获取更新,有一个具体的实施RabbitMQ的一个 IUpdateSource
接口。你可以看到我要去的地方与此有关。
As for your question about whether you should abstract away RabbitMQ from your services, my answer would be yes, however I would not simply create an IMessageQueue
abstraction. Think about what purpose you are using your message queue for, is it to push statuses? If so, have an IStatusNotifier
interface with a concrete implementation for RabbitMQ. If it's to fetch updates, have an IUpdateSource
interface with a concrete implementation for RabbitMQ. You can see where I am going with this.
如果您为消息队列创建一个抽象的概念,你是限制自己只适用在所有的Message Queue实现的功能。通过具有不同的实施 IStatusNotifier
针对不同的消息队列的实现,你可以利用的特点,是独一无二的不同的技术,同时保持灵活性的情况下完全不同的技术有优势在今后的就业(如写入到SQL数据库或输出到控制台)。
If you create an abstraction for a Message Queue, you are limiting yourself to features only available across all Message Queue implementations. By having a different implementation of IStatusNotifier
for different Message Queue implementations, you are able to take advantage of features which are unique to different technologies while also remaining flexible in case completely different technologies are employed in future (e.g. Writing to a SQL database or outputting to a console).
这篇关于RabbitMQ的使用Unity IOC容器在.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!