AppDomains之间的通信(Communication between AppDomains)
我们正在构建一个应用程序(WinForms,.NET 3.5),它将“插件”DLL加载到辅助AppDomain中。 辅助AppDomain需要偶尔与第一个AppDomain通信(更具体地说,从主AppDomain中创建的对象调用或获取数据)。
我已经阅读了有关AppDomains的大部分内容以及它们之间的通信。
到目前为止,我见过的唯一简单的解决方案是从MarshalByRefObject继承并将TransparentProxy传递到第二个AppDomain,在Proxy上调用方法。
此方法有其缺点(例如,在框架类型的情况下,或者从已经从其他类继承的类型,静态字段/类等等,并不总是可以从MBRO继承)。
由于当前的通信点非常不变(只有2-3个需要通信的场景),我考虑创建一个具有以下属性的简单Mediator类:
- 将在1st(Main)AppDomain中创建。
- 只能作为主要AppDomain中创建的“Real”对象的“消息传递器”。
- 将继承自MBRO,并将其代理引用发送到第二个AppDomain。
将调用此代理对象上的方法,然后调用第一个AppDomain中“真实”对象上的方法。
我的问题 -
- 这看起来像是一个逻辑设计吗?
- 更重要的是,WCF或任何其他通信框架中是否已存在调解器/消息传递器类? 它似乎是一个通用的概念,我想知道是否有类似的东西。
We are building an app (WinForms, .NET 3.5) that loads "Plugin" DLLs into a secondary AppDomain. The secondary AppDomain needs to communicate occasionally with the 1st one (more specifically, call or get data from objects that are created in the main AppDomain).
I have read most of the material about AppDomains and communication between them.
So far, the only easy solution i've seen was inheriting from MarshalByRefObject and passing a TransparentProxy into the 2nd AppDomain, calling methods on the Proxy.
This method has its drawbacks (not always possible to inherit from MBRO in case of framework types for example, or types that already inherit from another class, static fields/classes and so on).
Since the current communication points are pretty constant (only 2-3 scenarios that require communication), i have considered creating a simple Mediator class with the following properties:
- Will be created in the 1st (Main) AppDomain.
- Would function only as a "message-passer" to the "Real" objects that are created in the main AppDomain.
- Will inherit from MBRO, and a proxy reference to it will be sent to the 2nd AppDomain.
Methods on this proxy object would be called, which in turn will call the methods on the "real" objects in the 1st AppDomain.
My questions --
- Does this seem like a logical design?
- More importantly, does a mediator/message passer class already exist in WCF or any other communcation framework? it seems like a generic concept and i am wondering if there is something similar.
原文:https://stackoverflow.com/questions/8972352
最满意答案
您必须在实际调用之前设置方法期望,因此您的测试应如下所示:
context "Get #show" do subject { get :show, school_id: school.id, id: user.id } it "calls +authorize+ befor action" do expect(controller).to receive(:authorize) subject end end
查看文档https://github.com/rspec/rspec-mocks#message-expectations
You must set method expectation before actual call, so your test should look like:
context "Get #show" do subject { get :show, school_id: school.id, id: user.id } it "calls +authorize+ befor action" do expect(controller).to receive(:authorize) subject end end
Check the documentation https://github.com/rspec/rspec-mocks#message-expectations
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
Rails测试控制器方法,在过滤器之前设置实例变量(Rails Testing controller methods with instance variables set in before filters)[2021-10-23]
过滤器和回调很难测试和调试。 尽可能避免使用它们。 在这种情况下,我不认为你的before_filter是必要的,因此不需要测试它。 这种方法的最佳家园是模型。 检查我的refacoring: class User < ActiveRecord::Base delegate :inbox, :sentbox, :trash, to: :mailbox end class ConversationsController < ::ApplicationController def index ... -
belongs_to在ActiveRecord::Associations定义,它是ActiveRecord一部分。 您手动包含ActiveModel::Model ,它不提供任何与关联相关的功能。 包含使用不同ActiveModel模块的对象与ActionPack交互所需的接口。 它包括模型名称内省,转换,翻译和验证。 除此之外,它允许您使用属性哈希初始化对象,就像ActiveRecord一样。 假设您不需要整个ActiveRecord数据库持久性功能但需要关联样式,那么您需要手动处理。 因此,您必须定义 ...
-
before_filter将为控制器的每个请求调用该方法。 您可以将其设置为仅针对某些操作运行: before_filter :authorize, :only => :delete 甚至阻止它在特定的动作中运行: before_filter :authorize, :except => [:index, :show] 顺便说一下, before_filter的新语法是before_action before_filter will call the method for every request t ...
-
如何在rspec控制器测试期间记录rails控制器过滤器(How to log rails controller filters during rspec controller tests)[2022-11-23]
这就是我如何解决同样的问题。 我确信下面的代码仍然可以进行很多改进,并且可能放入它自己的模块中。 改进很好。 您需要将以下代码放入顶部ActionController,通常是ApplicationController或您需要跟踪的控制器: class ApplicationController < ActionController::Base def self.before_filter_with_logging(*arguments, &block) filters, conditions ... -
使用来自控制器的操作作为轨道中另一个控制器的过滤器(Use action from a controller as filter in another controller in rails)[2022-07-01]
@NickM在他的评论中已经涵盖了这一点......让OtherController从PermissionController继承 class PermissionController < ApplicationController def authorize ... end end class OtherController < PermissionController before_filter :authorize end 但是我注意到你的authorize方法有一个参数? 您 ... -
您必须在实际调用之前设置方法期望,因此您的测试应如下所示: context "Get #show" do subject { get :show, school_id: school.id, id: user.id } it "calls +authorize+ befor action" do expect(controller).to receive(:authorize) subject end end 查看文档https://github.com/rspec/rspe ...
-
而不是使用关注点在多个位置包含相同的代码,这似乎是服务对象的一个很好的用途。 class CollectionFilter def initialize(filters={}) @filters = filters end def results model_class.find(ids) end def ids return [] unless @filters[:id] @filters[:id] ...
-
从 @keywords = Keyword.where(keywordable_id == @document.id) 至 @keywords = Keyword.where(keywordable_id: @document.id) 另一方面,我建议你设置关联 class Document < ActiveRecord::Base has_many :keywords, foreign_key: 'keywordable_id' end class Keyword < ActiveRecord::B ...
-
这通常由使用格式的单个index方法处理(在您的情况下为html或csv)。 过滤只是列表中的另一个范围 - 渲染由方法中的块处理,如: respond_to do |format| format.html format.csv { render text: @products.to_csv } end 像往常一样--Railscasts已经涵盖了这个并且是一个很好的资源: http://railscasts.com/episodes/362-exporting-csv-and-excel?vie ...