Fluent DateTime for .NET

 

A set of (Ruby inspired) C# Extension Methods for easier and more natural DateTime handling and operations in .NET.
Allows you to write code like this: Alpha

DateTime.Now + 1.Week() + 3.Days + 14.Minutes();
3.Days().Ago();
5.Days().After(new DateTime(2000, 1, 1));
DateTime.Now.NextDay();
DateTime.Now.WeekAfter();
DateTime.Now.Midnight();
DateTime.Now.SetTime(11, 55, 0);

Fluent DateTime – Home

Creating and consuming Rss/Atom syndication feed using WCF 3.5

 

The .NET Framework did not provide any built-in functionality for creating or consuming syndication feeds until version 3.5 with the introduction of the System.ServiceModel.Syndication namespace. The most germane class in this namespace is SyndicationFeed. As you can probably guess from its name, this class represents a syndication feed. It has properties like Title, Description, Links, and Copyright, which provide details about the feed. The content items that make up the feed are specified via the Items property, which is a collection of SyndicationItem objects. The SyndicationFeed class also has a static Load method that parses and loads the information from a specified RSS 2.0 or Atom 1.0 syndication feed.

In addition to the SyndicationFeed and SyndicationItem classes, the System.ServiceModel.Syndication namespace also includes two formatter classes, Atom10FeedFormatter and Rss20FeedFormatter. These classes take a SyndicationFeed object and generate the corresponding XML content that conforms to either the Atom 1.0 or RSS 2.0 specificiations.

More… How to create a syndication feed for your website

Reasons for using mocking objects

Reasons for use

In a unit test, mock objects can simulate the behavior of complex, real (non-mock) objects and are therefore useful when a real object is impractical or impossible to incorporate into a unit test. If an object has any of the following characteristics, it may be useful to use a mock object in its place:

  • supplies non-deterministic results (e.g. the current time or the current temperature);
  • has states that are difficult to create or reproduce (e.g. a network error);
  • is slow (e.g. a complete database, which would have to be initialized before the test);
  • does not yet exist or may change behavior;
  • would have to include information and methods exclusively for testing purposes (and not for its actual task).

For example, an alarm clock program which causes a bell to ring at a certain time might get the current time from the outside world. To test this, the test must wait until the alarm time to know whether it has rung the bell correctly. If a mock object is used in place of the real object, it can be programmed to provide the bell-ringing time (whether it is actually that time or not) so that the alarm clock program can be tested in isolation.

LINQ “Any” Keyword to check for existence of an element/attribute

One of the things you must do when writing a LINQ to Xml query definition is check for the existence of nonrequired elements and attributes before using them. For instance, a client application displays an image of the book in the ListBox control. The source URL of the image is grabbed from the SmallImage element’s URL element. If the SmallImage element does not exist, which is certainly a possibility if there is no image on record, the LINQ query definition must account for this.

For example,

<Item>
    <SmallImage>
        <URL>



        </URL>
    </SmallImage>
</Item>

 

Before grabbing a value, you can perform a simple check to make sure the element exists; you do this by checking the Elements collection’s Any method. The Any method returns a Boolean value that indicates whether the collection contains elements. If the element exists, you can grab the value of the element. If the element does not exist you can set alternatives. The following shows the logic that checks for the existence of the image’s URL; if it is found it grabs the value and sets it to the imageSource variable.

let imageSource = (book.Elements(ns + "SmallImage").Any()
    ? book.Element(ns + "SmallImage").Element(ns + "URL").Value
    : string.Empty)
p.s. for more on the usage of the keyword let, take a look at http://vincenthomedev.wordpress.com/2007/12/19/linq-let-keyword/
 

How to design a Fluent Interface (Method Chaining)

 

Martin Fowler coined the term "FluentInterface" to describe objects that expose an interface that flows, and is designed to be readable and concise.  The cost of this fluency is additional effort required to design the interface for your object and the slight increase in complexity.  These types of interfaces are often utilized to create configurations for your objects but can progress into an internal Domain Specific Language or DSL.

  1. Person person = new Person();  
  2. person.Set.FirstName("Pat").LastName("Frank").Age(30).IsActive();  

Create an internal class that is accessed through a Set property and exposes only the Fluent Interface.

  1. public class Person  
  2. {  
  3.          public Person() {  _set = new PersonFluentInterface(this);               }  
  4. private string _firstName;  
  5. private string _LastName;  
  6. private int _age;  
  7. private readonly PersonFluentInterface _set;  
  8. private bool _isActive;  
  9. public PersonFluentInterface Set   {   get { return _set; }               }  
  10. public string FirstName      {   get { return _firstName; }   set { _firstName = value; }               }  
  11. public string LastName    {   get { return _LastName; }   set { _LastName = value; }               }  
  12. public int Age   {   get { return _age; }   set { _age = value; }               }  
  13. public bool IsActive  {   get { return _isActive; }   set { _isActive = value; }               }  
  14.           public class PersonFluentInterface  
  15.           {  
  16. private readonly Person _person;  
  17. public PersonFluentInterface(Person person) {      _person = person;                   }  
  18. public PersonFluentInterface FirstName(string firstName)   {     _person.FirstName = firstName;   return this;                   }  
  19. public PersonFluentInterface LastName(string lastName)    {     _person.LastName = lastName;   return this;                   }  
  20. public PersonFluentInterface Age(int age)    {     _person.Age = age;   return this;                   }  
  21. public PersonFluentInterface IsActive()   {   _person.IsActive = true;   return this;                   }  
  22. public PersonFluentInterface IsNotActive()    {    _person.IsActive = false;   return this;                   }  
  23. }           } 

Code to Live, Live to Code – How to design a Fluent Interface

Jesse Liberty – Creating Silverlight Carousel Videos

 image

Jesse Liberty videos Creating a Carousel Part 1 and Creating a Carousel Part 2 are now available.  These videos cover the material that is also discussed in his two part blog entry (beginning here) including such advanced topics as

  • The Silverlight Layout System
  • Overriding MeasureOverride and ArrangeOverride
  • Matrix Transforms
  • Adding Attached Dependency Properties
  • Programmatic Animation