Blog Home  Home Feed your aggregator (RSS 2.0)  
Code to Live, Live to Code - Saturday, November 24, 2007
Randy Patterson's BLog
 
 Saturday, November 24, 2007

If you don't have an MSDN subscription and you can't wait to try out the new Visual Studio 2008 features you can download the express edition and learn the new features while you wait for the professional edition to be available in stores.

http://www.microsoft.com/express/

Saturday, November 24, 2007 9:57:43 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0]   C#  |  Trackback
 Monday, November 12, 2007

image

 When: 11/28/2007  6:30 PM - 8:00 PM

Abstract: You can run, you can switch jobs, and you can write unit tests, but invariably at some point you will run into legacy code. Legacy code hides in many forms - sealed classes, spaghetti and big-ball-of-mud code, data in disparate data sources (or incompatible schemas). As an architect, there are steps you can take to get ahead of these issues and begin to make your codebases something you actually want to change. In this talk, we will discuss concepts from Michael Feathers' work on Working Effectively with Legacy Code as well as Scott Ambler's work on refactoring databases. You'll see tips and tricks to model your legacy code and data, and hear about ways to begin to turn your legacy code into a usable base."

 

Speaker: Cory Foy is an agile developer passionate about languages such as C# and Ruby. He currently works for Microsoft as a Premier Field Engineer, has been a developer on the NUnit team, and is known to speak at code camps and user groups across the country. He lives just north of Tampa with his wife and 2 daughters.

Registration: here

Monday, November 12, 2007 6:14:04 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]   Events  |  Trackback
 Tuesday, October 02, 2007

Speaking in Tampa on October 18th - Repository Factory, Enterprise Library, Guidance Bundles

 

David Hayden ( Microsoft MVP C# ) is giving a  presenting to the Tampa .NET Developer Group on Thursday, October 18th at 6:30pm at the Microsoft Tampa Office.

 

The focus is on the new Repository Factory, which is a software factory from Microsoft Patterns & Practices that will generate a data access layer for your winform and web applications in minutes. It generates business entities, stored procedures, and repository classes from an existing database within Visual Studio. In addition he will also be showing off the following:

See you there! You can RSVP here.

 

 

 

I really enjoy David Hayden's Presentations and this topic is of special interest to me. 

Tuesday, October 02, 2007 6:26:40 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [1]   Events  |  Trackback
 Wednesday, September 26, 2007

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.

Configuration Fluent Interface Example: (sometimes called Method Chaining)

Order    
	.AddFreeShipping()    
	.IncludeItem(10)    
	.SetQuantity(2); 

 

DSL Fluent Interface Example:  (Rhino Mocks)

Expect    
	.Call(entityMock.GetID(12))    
	.IgnoreArguments()    
	.Repeat    
	.Once()    
	.Return(entityDataSet);

 

Show me the Code

Fluent interfaces are best explained by showing some code examples, so I'll take the rather prosaic Person object and create a fluent interface for it.   

Take a common Person object and write some code to instantiate and Initialize the object

       public class Person
        {
            private string _firstName;
            private string _lastName;
            private int _age;
            public bool _isActive;

            public Person(string firstName, string lastName, int age, bool isActive)
            {
                _firstName = firstName;
                _lastName = lastName;
                _age = age;
                _isActive = isActive;
            }

            public string FirstName
            {
                get { return _firstName; }
                set { _firstName = value; }
            }

            public string LastName
            {
                get { return _lastName; }
                set { _lastName = value; }
            }

            public int Age
            {
                get { return _age; }
                set { _age = value; }
            }

            public bool IsActive
            {
                get { return _isActive; }
                set { _isActive = value; }
            }
        }
 
This is a common pattern where the constructor is used to quickly set the properties of the class.  The initialization code would look something like this.

 

Person person = new Person("Frank", "Pat", 30, true);

 

Although this code is concise it is not very readable. Is "Frank" the first name or the last name? What does the value true represent? The readability issue becomes more problematic as the number of construction parameters increase.  One solution, and the point of this post, is to write a Fluent Interface for our Person class.  The idea here is to allow each property to be set through a method call and then have that method return a reference to itself so you can continue on with next method call (often called method chaining). 

person.SetLastName("Frank").SetFirstName("Pat");
 

 

This makes our code concise and far easier to read, we now know that "Frank" is the last name.  However, an obvious issue with this pattern is that we are now cluttering up our class with methods that, when taken out of context, make little sense.  We have a FirstName property and a SetFirstName method defined on our class that obfuscates the intent. A better approach is to create an internal class that is accessed through a Set property and exposes only the Fluent Interface.

public class Person
        {
            public Person()
            {
                _set = new PersonFluentInterface(this);
            }

            private string _firstName;
            private string _LastName;
            private int _age;
            private readonly PersonFluentInterface _set;
            private bool _isActive;

            public PersonFluentInterface Set
            {
                get { return _set; }
            }

            public string FirstName
            {
                get { return _firstName; }
                set { _firstName = value; }
            }

            public string LastName
            {
                get { return _LastName; }
                set { _LastName = value; }
            }

            public int Age
            {
                get { return _age; }
                set { _age = value; }
            }

            public bool IsActive
            {
                get { return _isActive; }
                set { _isActive = value; }
            }

            public class PersonFluentInterface
            {
                private readonly Person _person;

                public PersonFluentInterface(Person person)
                {
                    _person = person;
                }

                public PersonFluentInterface FirstName(string firstName)
                {
                    _person.FirstName = firstName;
                    return this;
                }

                public PersonFluentInterface LastName(string lastName)
                {
                    _person.LastName = lastName;
                    return this;
                }

                public PersonFluentInterface Age(int age)
                {
                    _person.Age = age;
                    return this;
                }

                public PersonFluentInterface IsActive()
                {
                    _person.IsActive = true;
                    return this;
                }

                public PersonFluentInterface IsNotActive()
                {
                    _person.IsActive = false;
                    return this;
                }
            }
        }

 

Now the code is clean, concise and quite readable.

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

Conclusion

This pattern for fluent interfaces is often seen in object configuration and setup.  It's fairly easy to design and the objects configuration options are readily discoverable using intellisense.  As the complexity of an objects setup increased, a fluent interface becomes a more attractive option to ease the initialization burden.

 

 

kick it on DotNetKicks.com
Wednesday, September 26, 2007 1:25:09 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [2]   C# | Patterns  |  Trackback
 Sunday, September 23, 2007

 IASA

This is the inaugural meeting for the Tampa chapter of IASA.  In this meeting we will discuss a number of topics about the group and what to expect going forward.  We are also fortunate to have a Microsoft Architect Evangelist coming into town to speak.

Speaker: Jeff Barnes, Microsoft Architect Evangelist

Topic:
The Role of the Software Architect

Abstract:
This session will explore the role of the Software Architect - from defining what the job role actually entails, what it takes to be successful at it, and what steps you can take to become a software architect (or a better one) today. This session will be highly interactive and should lead to lively group discussions and audience participation.

When:

10/30/2007

6:30 PM - 8:00 PM

Where:

Microsoft Corporation

3000 Bayport Drive

Suite 480

Tampa, FL 33607

USA

Click here to register for the event

Sunday, September 23, 2007 10:52:56 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0]   Events  |  Trackback
 Friday, September 21, 2007

This is one of the most useless unit tests I've encountered

/// <summary>
///A test for GetByLegalCaseID (int)
///</summary>
[TestMethod()]
public void GetTicketByLegalCaseID_Test()
{
    TicketEntityModel target = new TicketEntityModel();
    int ID = 1;
    TicketDataSet actual = target.GetByLegalCaseID(ID);
 
    Assert.IsNotNull(actual, "TicketDataSet was not returned.");
    Assert.IsNotNull(actual.Citation, "No Citation table was returned.");
    Assert.IsTrue(actual.Citation.Rows.Count > 0, "No Citation rows were returned.");
    Assert.IsNotNull(actual.Ticket, "No Ticket table was returned.");
    Assert.IsTrue(actual.Ticket.Rows.Count > 0, "No Ticket rows were returned.");
}

 

Unit tests in the Entity Model should concentrate on testing business logic, not Data Access Layer (DAL) logic. Unit tests for DAL objects test data access issues.   Furthermore,  this Unit Test simply checks to see if the current database has a record with an ID of 1 and, therefore, is non-deterministic. It can fail for many reasons that have nothing to do with the business logic. If GetByLegalCaseID has any business logic in it then unit tests should to be written to test that logic and the dependencies on the DAL would be mocked so a previously setup DataSet is returned with known values. This falls under the "What Burns Me" category. 

Friday, September 21, 2007 8:10:15 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [5]   What Burns Me  |  Trackback
 Tuesday, September 18, 2007

The very flexible generic collection List<T> contains several methods that take a predicate as it's parameter.  Coupled with Anonymous Methods this provides powerfully concise code for filtering, searching and sorting your collections.

 

List<Person> People = new List<Person>();
 
People.Add(new Person("Randy", "Patterson", 40));
People.Add(new Person("John", "Smith", 15));
People.Add(new Person("Caity", "Johnson", 13));
People.Add(new Person("Jody", "Patterson", 39));
People.Add(new Person("Chloe", "Dog", 30));
People.Add(new Person("Corey", "Patterson", 18));

 

For Example,  to filter the collection and display only my family members requires a single line of code

IList<Person> family = People.FindAll(delegate(Person person) { return person.LastName == "Patterson"; });
 

And produces the following output

image

To remove the teenagers (not a bad proposition) also requires a single line

People.RemoveAll(delegate(Person person) { return person.Age > 12 && person.Age < 20; });

Sorting

Sorting requires a Comparison<T> delegate instead of Predicate<T> but operates in much the same fashion.

For example, sorting by last name requires this single line of code:

People.Sort(delegate(Person x, Person y) {return x.LastName.CompareTo(y.LastName) ;});

a slight modification will sort the list in descending order:

People.Sort(delegate(Person x, Person y) { return y.LastName.CompareTo(x.LastName); });

To sort by Last Name then First Name would look something like this:

People.Sort(delegate(Person x, Person y)
    {
        if (x.LastName == y.LastName)
            return x.FirstName.CompareTo(y.FirstName);
        else
            return x.LastName.CompareTo(y.LastName);
    });

 

Conclusion

I've just scratched the surface on what the generic List collection is capable of providing. There are several other generic methods that take delegates as parameters, such as ForEach and ConvertAll, that open up interesting possibilities in your code.   Overall I find the Anonymous Method syntax a bit kludgy but C# 3.0 promises to reduce the syntactical noise and give us true (almost) Lambda Expressions

kick it on DotNetKicks.com

Tuesday, September 18, 2007 8:40:57 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [8]   C#  |  Trackback
 Saturday, September 15, 2007

Not really related to software development but Instant Conference is one of those gems occasionally found while surfing the tubes.

image 

You just give them an email address and you get back a conference number and an access code........that's it! The dial in number and access code are dedicated to you and never expire.  While in the conference there are many advanced features such as Conference Recording and Presentation Mode.

image

I've used my number several times and found the quality to be pretty good and the features easy to use.  If you record your conference you'll get an email in an hour or so that will allow you to download an MP3 file.  A nice feature for verifying your notes.

Saturday, September 15, 2007 7:50:29 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0]   Deals  |  Trackback

Welcome back to my Blog.  I've had a blog for awhile but, due to a heavy work load,  I haven't posted anything in over a year.  In the past year I've implemented many of the principles of GTD to better manage my professional, personal and online time.  This will, hopefully, allow me to Work, Live and Play without the stress involved in miss-managing time.

I've also removed my previous Blog and blog postings and started fresh with a new version of DasBlog and new postings. It was a pleasant surprise to see the maturity of the blog engines as well as the software to help you post your content.  I am currently using Windows Live Writer for content publishing, which has a very active community for Plug-in's.

Saturday, September 15, 2007 4:09:49 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0]    |  Trackback
Copyright © 2008 Randy Patterson. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.