|
|
Goals to demonstrate:
Prerequisites:
Steps:
using System;
using System.Collections.Generic;
using System.Text;
namespace DTO
{
[Serializable]
public class Book
{
public string Title { get; set; }
public List<Author> Authors { get; set; }
public DateTime YearOfPublishing { get; set; }
public int NumberOfPages { get; set; }
public bool HighQuality { get; set; }
public Book(string title, DateTime yearOfPublishing, int numberOfPages, bool highQuality)
{
Authors = new List<Author>();
this.Title = title;
this.YearOfPublishing = yearOfPublishing;
this.NumberOfPages = numberOfPages;
this.HighQuality = highQuality;
}
public override string ToString()
{
var sb = new StringBuilder();
sb.AppendLine(String.Format("Title: {0}", Title));
sb.Append("Authors: ");
foreach (Author a in Authors)
sb.AppendFormat("{0}, ", a);
sb.AppendLine();
sb.AppendLine(String.Format("Number of pages: {0}", NumberOfPages));
sb.AppendLine(String.Format("High Quality: {0}", HighQuality ? "Yes" : "No"));
return sb.ToString();
}
}
}
using System;
namespace DTO
{
[Serializable]
public class Author
{
public string Name { get; set; }
public string Surname { get; set; }
public string Nationality { get; set; }
public Author(string name, string surname, string nationality)
{
this.Name = name;
this.Surname = surname;
this.Nationality = nationality;
}
public override string ToString()
{
return String.Format("{0} {1} - {2}", Name, Surname, Nationality);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (Author)) return false;
return Equals((Author) obj);
}
public bool Equals(Author obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return Equals(obj.Name, Name) && Equals(obj.Surname, Surname) && Equals(obj.Nationality, Nationality);
}
public override int GetHashCode()
{
unchecked
{
int result = (Name != null ? Name.GetHashCode() : 0);
result = (result*397) ^ (Surname != null ? Surname.GetHashCode() : 0);
result = (result*397) ^ (Nationality != null ? Nationality.GetHashCode() : 0);
return result;
}
}
}
}
using System;
namespace DTO.Exceptions
{
public class BookListException : Exception
{
private readonly string message;
public override string Message
{
get { return message; }
}
public BookListException(string message)
{
this.message = message;
}
}
}
public partial class BookFlow : SequentialWorkflowActivity
{
public List<Book> BookList { get; set; }
public Book CurrentBook { get; set; }
public Author CurrentAuthor { get; set; }
public List<Author> AuthorList { get; set; }
public BookListException Ble { get; set; }
}
private void SetBookListExceptionCode(object sender, EventArgs e)
{
Ble = new BookListException("Book list empty or non-existant");
}
private void ReplicatorCondition(object sender, ConditionalEventArgs e)
{
e.Result = ((ReplicatorActivity) sender).AllChildrenComplete;
}
private void BookListChildInit(object sender, ReplicatorChildEventArgs e)
{
CurrentBook = (Book)e.InstanceData;
}
private void BookListInit(object sender, EventArgs e)
{
AuthorList = new List<Author>();
}
private void PrintBookCode(object sender, EventArgs e)
{
Console.WriteLine("Book:");
Console.WriteLine(CurrentBook);
Console.WriteLine();
}
private void AuthorsChildInit(object sender, ReplicatorChildEventArgs e)
{
CurrentAuthor = (Author) e.InstanceData;
}
private void MangeAuthorsCode(object sender, EventArgs e)
{
if (!AuthorList.Contains(CurrentAuthor))
AuthorList.Add(CurrentAuthor);
}
private void PrintAuthorsCode(object sender, EventArgs e)
{
Author a = AuthorList[0];
Console.WriteLine(a);
AuthorList.RemoveAt(0);
}
private static void WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e)
{
if (e.Exception is DTO.Exceptions.BookListException)
{
DTO.Exceptions.BookListException ble = e.Exception as DTO.Exceptions.BookListException;
Console.WriteLine(
String.Format(
"There was a problem with a book list provided as input: '{0}'", ble.Message));
Console.WriteLine("Workflow Runtime Event: Workflow instance terminated.");
}
else
{
Console.WriteLine(
String.Format(
"Workflow Runtime Event: Workflow instance terminated. " +
"Reason: '{0}'", e.Exception.Message));
}
waitHandle.Set();
}
public static Guid RunBookFlow(List<Book> bookList)
{
Dictionary<string, object> parameters = new Dictionary<string, object> { { "BookList", bookList} };
return Hosting.InvokeWorkflow(typeof(BookFlow), parameters);
}
static void Main(string[] args)
{
List<Book> bookList = new List<Book>();
Book b = new Book("Programming Perl", new DateTime(2000, 1, 1), 1006, false);
b.Authors.Add(new Author("Larry", "Wall", "USA"));
b.Authors.Add(new Author("Tom", "Christiansen", "USA"));
b.Authors.Add(new Author("Jon", "Orwant", "USA"));
bookList.Add(b);
b = new Book("Algorithms in Perl", new DateTime(2003, 1, 1), 674, true);
b.Authors.Add(new Author("Jon", "Orwant", "USA"));
b.Authors.Add(new Author("Jarkko", "Hietaniemi", "Finland"));
b.Authors.Add(new Author("John", "Macdonald", "USA"));
bookList.Add(b);
b = new Book("Perl Cookbook", new DateTime(2004, 1, 1), 1056, true);
b.Authors.Add(new Author("Tom", "Christiansen", "USA"));
b.Authors.Add(new Author("Nathan", "Torkington", "USA"));
bookList.Add(b);
Invoker.RunBookFlow(bookList);
Invoker.WaitHandle.WaitOne();
Console.ReadLine();
}