mirror of
https://github.com/skoelle/buildbroken-blog-archive.git
synced 2026-09-17 18:30:25 +00:00
fix code blocks
This commit is contained in:
@@ -8,137 +8,96 @@ author: "Thomas Christian"
|
||||
categories: ["Architektur", "Pattern", "Tipps"]
|
||||
---
|
||||
|
||||
Nach dem ich meinen vorherigen Artikel zum Thema [“Asynchrone Kommunikation mit dem Async-Pattern”](/posts/async/) vorgestellt hatte, hat Ralf Westphal auf seinem Blog [“One Man Think Tank Gedanken”](http://ralfw.blogspot.com/2010/05/asynchrone-kommunikation-mit-ebcs-statt.html) das Vorgehen zur Implementierung einer asynchronen Kommunikation mit Hilfe von Event-Based-Components vorgestellt, welches eine sehr gute Alternative zum Async-Pattern ist. Seinen Eintrag nehme ich zum Anlass, um meine Beispiel-Implementierung des Async-Pattern zu refaktorisieren, um eine bessere Trennung der Verantwortlichkeiten und somit eine bessere Lesbarkeit zu erreichen.
|
||||
Nach dem ich meinen vorherigen Artikel zum Thema ["Asynchrone Kommunikation mit dem Async-Pattern"](/posts/async/) vorgestellt hatte, hat Ralf Westphal auf seinem Blog ["One Man Think Tank Gedanken"](http://ralfw.blogspot.com/2010/05/asynchrone-kommunikation-mit-ebcs-statt.html) das Vorgehen zur Implementierung einer asynchronen Kommunikation mit Hilfe von Event-Based-Components vorgestellt, welches eine sehr gute Alternative zum Async-Pattern ist. Seinen Eintrag nehme ich zum Anlass, um meine Beispiel-Implementierung des Async-Pattern zu refaktorisieren, um eine bessere Trennung der Verantwortlichkeiten und somit eine bessere Lesbarkeit zu erreichen.
|
||||
|
||||
In das Form wird die Abhängigkeit “CalcProxy” injected.
|
||||
In das Form wird die Abhängigkeit "CalcProxy" injected.
|
||||
|
||||
```csharp
|
||||
static void Main()
|
||||
|
||||
{
|
||||
|
||||
CalcProxy calcProxy = new CalcProxy(new Calculator());
|
||||
|
||||
Application.EnableVisualStyles();
|
||||
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
|
||||
Application.Run(new Form1(calcProxy));
|
||||
|
||||
CalcProxy calcProxy = new CalcProxy(new Calculator());
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
Application.Run(new Form1(calcProxy));
|
||||
}
|
||||
```
|
||||
|
||||
Im Gegensatz zur vorherigen Version wird in der Form nun nicht mehr der Calculator direkt erzeugt und verwendet, sondern auf den injekteten CalcProxy zugegriffen.
|
||||
|
||||
```csharp
|
||||
public partial class Form1 : Form {
|
||||
private readonly ICalcProxy m_calcProxy;
|
||||
|
||||
private readonly ICalcProxy m\_calcProxy;
|
||||
public Form1(ICalcProxy calcProxy) {
|
||||
InitializeComponent();
|
||||
m_calcProxy = calcProxy;
|
||||
m_calcProxy.CalcCompleted += CalculatorCalcCompleted;
|
||||
}
|
||||
|
||||
public Form1(ICalcProxy calcProxy) {
|
||||
|
||||
InitializeComponent();
|
||||
|
||||
m\_calcProxy = calcProxy;
|
||||
|
||||
m\_calcProxy.CalcCompleted += CalculatorCalcCompleted;
|
||||
|
||||
}
|
||||
|
||||
private void Run\_Click(object sender, EventArgs e) {
|
||||
|
||||
int number;
|
||||
|
||||
if (Int32.TryParse(txbEingabe.Text, out number)) {
|
||||
|
||||
m\_calcProxy.CalcAsync(number, number);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void CalculatorCalcCompleted(object sender, CalcEventArgs eventArgs) {
|
||||
|
||||
lblCounter.Text = eventArgs.UserState.ToString();
|
||||
|
||||
}
|
||||
private void Run_Click(object sender, EventArgs e) {
|
||||
int number;
|
||||
if (Int32.TryParse(txbEingabe.Text, out number)) {
|
||||
m_calcProxy.CalcAsync(number, number);
|
||||
}
|
||||
}
|
||||
|
||||
void CalculatorCalcCompleted(object sender, CalcEventArgs eventArgs) {
|
||||
lblCounter.Text = eventArgs.UserState.ToString();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Der CalcProxy wiederum bekommt die Abhängigkeit zum Calculator injected und stellt für die Calculator.Calc-Methode sowohl eine synchrone als auch eine asynchrone Methode zur Verfügung.
|
||||
|
||||
```csharp
|
||||
public class CalcProxy : ICalcProxy {
|
||||
private readonly ICalculator m_calculator;
|
||||
public event CalcCompletedEventHandler CalcCompleted;
|
||||
private AsyncOperation m_asyncOperation;
|
||||
private bool m_isRunning;
|
||||
|
||||
private readonly ICalculator m\_calculator;
|
||||
public CalcProxy(ICalculator calculator) {
|
||||
m_calculator = calculator;
|
||||
}
|
||||
|
||||
public event CalcCompletedEventHandler CalcCompleted;
|
||||
public int Calc(int number) {
|
||||
return m_calculator.Calc(number);
|
||||
}
|
||||
|
||||
private AsyncOperation m\_asyncOperation;
|
||||
public void CalcAsync(int number, object userState) {
|
||||
lock (this) {
|
||||
if (m_isRunning) {
|
||||
throw new InvalidOperationException("Diese Operation wird bereits ausgeführt");
|
||||
}
|
||||
m_isRunning = true;
|
||||
m_asyncOperation = AsyncOperationManager.CreateOperation(userState);
|
||||
ThreadPool.QueueUserWorkItem(ExecuteCalc, number);
|
||||
}
|
||||
}
|
||||
|
||||
private bool m\_isRunning;
|
||||
|
||||
public CalcProxy(ICalculator calculator) {
|
||||
|
||||
m\_calculator = calculator;
|
||||
|
||||
}
|
||||
|
||||
public int Calc(int number) {
|
||||
|
||||
return m\_calculator.Calc(number);
|
||||
|
||||
}
|
||||
|
||||
public void CalcAsync(int number, object userState) {
|
||||
|
||||
lock (this) {
|
||||
|
||||
if (m\_isRunning) {
|
||||
|
||||
throw new InvalidOperationException("Diese Operation wird bereits ausgeführt");
|
||||
|
||||
}
|
||||
|
||||
m\_isRunning = true;
|
||||
|
||||
m\_asyncOperation = AsyncOperationManager.CreateOperation(userState);
|
||||
|
||||
ThreadPool.QueueUserWorkItem(ExecuteCalc, number);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void ExecuteCalc(object state) {
|
||||
|
||||
var result = Calc((int)state);
|
||||
|
||||
m\_asyncOperation.PostOperationCompleted(CalcCompletedSuccessful, result);
|
||||
|
||||
}
|
||||
|
||||
private void CalcCompletedSuccessful(object result) {
|
||||
|
||||
if (CalcCompleted != null) {
|
||||
|
||||
CalcCompleted(this, new CalcEventArgs(null, false, (int)result, result));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
private void ExecuteCalc(object state) {
|
||||
var result = Calc((int)state);
|
||||
m_asyncOperation.PostOperationCompleted(CalcCompletedSuccessful, result);
|
||||
}
|
||||
|
||||
private void CalcCompletedSuccessful(object result) {
|
||||
if (CalcCompleted != null) {
|
||||
CalcCompleted(this, new CalcEventArgs(null, false, (int)result, result));
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Nun enthält der Calculator nur noch die Methode die für den Calculator notwendig ist, nämlich die Calc-Methode.
|
||||
|
||||
```csharp
|
||||
public class Calculator : ICalculator {
|
||||
|
||||
public int Calc(int number) {
|
||||
|
||||
Thread.Sleep(10000);
|
||||
|
||||
return number \* number;
|
||||
|
||||
}
|
||||
|
||||
public int Calc(int number) {
|
||||
Thread.Sleep(10000);
|
||||
return number * number;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Kommentare (Archiv)
|
||||
|
||||
@@ -155,7 +114,7 @@ return number \* number;
|
||||
|
||||
[Mai 6, 2010 um 4:38 pm](/posts/async-refactored/)
|
||||
|
||||
[...] This post was mentioned on Twitter by .NET German Bloggers, DeveloperBlogs. DeveloperBlogs said: Asynchrone Kommunikation mit dem Async-Pattern (Refactored): Nach dem ich meinen vorherigen Artikel zum Thema “Asy… <http://bit.ly/9eV2kr> [...]
|
||||
[...] This post was mentioned on Twitter by .NET German Bloggers, DeveloperBlogs. DeveloperBlogs said: Asynchrone Kommunikation mit dem Async-Pattern (Refactored): Nach dem ich meinen vorherigen Artikel zum Thema "Asy… <http://bit.ly/9eV2kr> [...]
|
||||
3. **[Rainer Hilmer](http://dotnet-forum.de/blogs/rainerhilmer/default.aspx)**
|
||||
|
||||
[Juli 24, 2010 um 1:56 pm](/posts/async-refactored/)
|
||||
|
||||
Reference in New Issue
Block a user