惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Hacker News
The Hacker News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
C
CERT Recently Published Vulnerability Notes
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Tenable Blog
O
OpenAI News
NISL@THU
NISL@THU
AI
AI
T
Threat Research - Cisco Blogs
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
W
WeLiveSecurity
P
Privacy International News Feed
Cisco Talos Blog
Cisco Talos Blog
S
Secure Thoughts
D
Docker
博客园 - 三生石上(FineUI控件)
T
Troy Hunt's Blog
T
Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
Vercel News
Vercel News
H
Hacker News: Front Page
B
Blog
S
SegmentFault 最新的问题
H
Heimdal Security Blog
T
The Blog of Author Tim Ferriss
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
AWS News Blog
AWS News Blog
N
News and Events Feed by Topic
L
LINUX DO - 热门话题
小众软件
小众软件
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
Hacker News: Ask HN
Hacker News: Ask HN
Recent Announcements
Recent Announcements
Recent Commits to openclaw:main
Recent Commits to openclaw:main
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
PCI Perspectives
PCI Perspectives
I
Intezer
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed

博客园 - wzc998

Oracle Audit 关于"cannot perform a DDL, commit or rollback inside a query or DML" 错误 关于varchar2在pl/sql和schema级别的最大值 ORA-01031 权限不足错误的解决 通过代码备份存储过程,package body 和表数据 并还原 XSL ASP Log4net不同版本在app.config和web.config的配置区别 SQL*Loader FAQ 【转】NLS 视图的研究 oracle多语言环境下to_date时间转换问题 【转】如何使用 ADO.NET 和 Visual C# .NET 调用带参数的存储过程 【转】SQL SERVER 存储过程学习笔记 【转】代码复用原则:合理使用类组合和类继承 委托与接口的关系 IOC 【转】范式 【转】oracle范式与交叉表 oracle存储过程返回交叉表(使用存储过程/高级语言调用)
.net实现松耦合事件的三种方法
wzc998 · 2011-05-05 · via 博客园 - wzc998

松耦合事件实现方法有多种,本文重点描述使用spring.net来实现。并同时给出其他实现方案。

一,spring.net 

Spring.Objects.Events.IEventRegistry 接口封装了事件注册中心,spring容器通过Spring.Objects.Events.Support.EventRegistry对象代理实现了这个事件注册中心。于是我们可以使用如下几个方法把事件注册到容器中。 

void PublishEvents(object sourceObject);

void Subscribe(object subscriber);

void Subscribe(object subscriber, Type targetSourceType);

void Unsubscribe(object subscriber);

void Unsubscribe(object subscriber, Type targetSourceType);

使用 PublishEvents方法来发布主题,使用Subscribe对主题进行订阅并设定订阅主题类型(targetSourceType参数)。发布和订阅操作完全建立在主题和观察者互不了解的前提下进行的,所以实现了松耦合的事件机制。

            

using (IApplicationContext ctx = ContextRegistry.GetContext())

    {
                Provider provider = ctx.GetObject("Provider"as Provider;
                ctx.PublishEvents(provider);                Consumer consumer = ctx.GetObject("Consumer"as Consumer;
                ctx.Subscribe(consumer);                Console.WriteLine("Provider name: " + provider.ProviderName);
                Console.WriteLine("Consumer name: " + consumer.ConsumerName);                provider.Produce();                Console.WriteLine(consumer.ConsumerName + "'s HandledFlg is " + consumer.EventHandled.ToString());            }

观察者的订阅方式取决于使用的订阅方法:

Subscribe(object subscriber)观察者订阅事件中心所有能处理的事件。

Subscribe(object subscriber, Type targetSourceType)观察者订阅事件中心指定类型主题的所有能处理的事件。

观察者同样可以取消订阅:

void Unsubscribe(object subscriber);

void Unsubscribe(object subscriber, Type targetSourceType);

观察者甚至可以这样进行订阅和取消订阅:

Subscribe(object subscriber);观察者订阅事件中心所有能处理的事件。

void Unsubscribe(object subscriber, Type targetSourceType); 取消对某类型主题的订阅,保留对其他类型主题的订阅。

二,使用企业库实现松耦合事件

1,首先抽象一个事件中心接口,包含中心可处理的事件,主题和观察者都将实现该接口

public interface IEvSink
{
    
void OnEvent1();
    
void OnEvent2();

2,主题,从ServicedComponent继承,并声明类特性。主题和观察者之间的交互通过标记为EventClass类由Com+服务来处理,所以实现了松耦合的事件机制。

[EventClass]
public class MyPublisher : ServicedComponent, IEvSink
{
    
public MyPublisher()
    {
        
// 
        
// TODO: Add constructor logic here.
        
// 
    }
    
public void OnEvent1()
    { }
    
public void OnEvent2()
    { }

观察者,从ServicedComponent继承,实现事件处理

public class MySubscriber : ServicedComponent, IEvSink
{
    EventLog ev 
= new EventLog("Application");public void OnEvent1()
    {
        ev.Source 
= "ClassLibLCE";
        ev.WriteEntry(
"OnEvent1 Fired");
    }
public void OnEvent2()
    {
        ev.Source 
= "ClassLibLCE";
        ev.WriteEntry(
"OnEvent2 Fired");
    }

这个例子取自 http://support.microsoft.com/kb/318185/en-us

三,http://www.codeproject.com/KB/cs/DynamicInvoking.aspx