Enterprise Library from MS Patterns & Practices. . . downloaded – level 200

Well, it’s finally out.  Download the lib here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/entlib.asp


I’ve downloaded it, and I’m excited to try out the contents.  Of course by now, my team and I have already developed a team framework with most of this functionality, so I’m interested to see how what we did stacks up to this.  I’ve been using the Data Access block since v1, so I also look forward to seeing what additions have been made. 


 


I think this is great that MS is making this stuff available for free to everyone.  This is a great addition to the .Net framework that will make developers more productive.  Real developers, not drag-n-droppers.

String concatenation discovery – not the usual += bit – level 300

We’ve all seen the demo where Stringbuilder blows away appending strings in a
loop where you just do a += or string = string + something.

 

I’m not going to cover that scenario, but I did compare 4 different “fast”
ways to concatenate strings, and one of them blows away stringbuilder by a big
margin  Here’s the test code:

            int numIterations = 10000000;

 

            DateTime startDate, endDate;

            string result = string.Empty;

            string h;

            h = “h”;

 

            startDate = DateTime.Now;

            for(int i = 0; i < numIterations; i++)
{

                result = “h”

                    + “a”

                    + “b”

                    + “c”

                    + “d”

                    + “e”

                    + “f”

                    + “g”

                    + “h”

                    + “i”

                    + “j”

                    + “k”;

            }

            endDate = DateTime.Now;

            Console.WriteLine(“All literal concat
12: ” + endDate.Subtract(startDate));

 

            startDate = DateTime.Now;

            for(int i = 0; i < numIterations; i++)
{

                result = “h”

                    + “h”

                    + h

                    + “h”

                    + h

                    + “h”

                    + h

                    + “h”

                    + h

                    + “h”

                    + h

                    + “h”;

            }

            endDate = DateTime.Now;

            Console.WriteLine(“+ 12: ” +
endDate.Subtract(startDate));

 

            startDate = DateTime.Now;

            for(int i = 0; i < numIterations; i++)
{

                string.Concat(“h”, “h”, “h”, “h”, “h”, “h”,
“h”, “h”, “h”, “h”, “h”, “h”);

            }

            endDate = DateTime.Now;

            Console.WriteLine(“String.Concat 12: ”
+ endDate.Subtract(startDate));

 

            startDate = DateTime.Now;

            for(int i = 0; i < numIterations; i++)
{

                StringBuilder x = new StringBuilder();

                x.Append(“h”);

                x.Append(“h”);

                x.Append(“h”);

                x.Append(“h”);

                x.Append(“h”);

                x.Append(“h”);

                x.Append(“h”);

                x.Append(“h”);

                x.Append(“h”);

                x.Append(“h”);

                x.Append(“h”);

                x.Append(“h”);

                x.ToString();

            }

            endDate = DateTime.Now;

            Console.WriteLine(“StringBuilder 12: ”
+ endDate.Subtract(startDate));

 

 

            Console.ReadLine();

 

Here we have 4 scenarios, and here is the console results:

All literal concat 12: 00:00:00.0400648
+ 12:
00:00:06.0497848
String.Concat 12: 00:00:06.4804814
StringBuilder 12:
00:00:06.2100440

 

I have other programs running, so my results aren’t laboratory-clean, BUT,
notice the first result.  The first method took only 4/100th of a second where
the other three were pretty close at 6 seconds.  These results are x 10 million
to get measurable results.  So I can only conclude that the C# or MSIL compiler
has some special optimization for situations where the strings I’m concatenating
are all literals and not a mixture of literals and string variables.

Very interesting, indeed.  What I understand from this test is that if I have
all my strings ready to go, using the good ol’ +…+….+….+…. works the
fastest and should NOT be replaced with StringBuilder.  Wow.  I learn something
new every day.

Update:  Wesner had some great feedback:

When you are concatenating just literals, then the compiler performs the
concatenation at compile-time instead of run-time.

result = “h” + “a” +
“b” + “c” + “d” + “e” + “f” + “g” + “h” + “i” + “j” + “k”;

is optimized
to

result = “habcdefghijk”;

This explains why the first case was the fastest.

Use .Net 2.0 code NOW – level 300

This topic isn’t for the newbie .Net-er.  We’ve all dabbled with ASP.NET 2.0 master pages, themes, and other goodies.  I’ve created a suite of master pages controls that started with Paul Wilson’s controls but was enhanced with ideas from the implementation of the 2.0 version, and now the controls suite (besides designer support) functions just about the same way.  The master page is settable from the config file as well as in code, and it supports an unlimited nesting of masters (user controls – after all the new MasterPage class just inherits from UserControl).  You can download it here.


Reading Scott Mitchell’s blog post about ASP.NET 2.0 prompted me to blog this.  I use Lutz’s Reflector to peek into  the 2.0 assemblies and get code ideas.  The best thing is that I can make sure my implementations are similar to the 2.0 implementations so that upgrading will be a breeze.  If you haven’t used Reflector to peek into assemblies and view code, I would encourage you to do so. 


Haven’t you had a discussion about the performance of two methods in the .Net framework?  Someone contends that calling one method is faster than calling two other methods in succession.  You can’t convince them otherwise. . . . until you peek at the assembly and show them that their “fast” method merely wraps the other two.  🙂

Coding standards from Microsoft – level 100

If you’ve ever tried to discuss coding standards, you’ve realized that we
have a long way to go before we actually have a widely-accepted standard.

Check out Brad
Abrams’ post
about coding standards in use in Microsoft internally.

 

it’s also interesting to use Reflector to peek into code to see the actual
standard used 🙂

Catch up on events – level 000

This weekend is my Army Reserve drill weekend for the month of January, and I finally have some time to catch up on blog reading.  Most of the unit is in the field, but I’m at the HQ because I’m outprocessing and preparing to go into the IRR.  I did a year stint in Iraq, and I can move on from the military as a proud veteran.

Anyway, here are some really interesting things happening in the blogosphere:
Blair Jennings comments on Rocky’s TSS article:
This got me thinking.  My company has a LOT of large applications, and we are slowly adapting them to communicate with one another.  We have used SOD (Service-oriented design) even before web services.  Instead of an xml node over http, we use message queues and batch files.  Even so, my team’s main application is said to “own” the database, but there are other smaller apps (that we control) that hit the database directly as well.  So if there is a bug in one of those apps, then it may affect our larger app because of data corruption.  This is really something to think about.  It makes me think about the reall need to have an application OWN the database and disallow any other write access.  Then if other apps need the data, the owning application could expose services (web service or other) that allow manipulation of the underlying data only after all business rules have been checked (since the same core business layer is actually doing the work).

John Wood provides a great look at C# off-spring, C Omega.
John provides code samples and explanations.  Wow, I can see great potential.  This might, indeed replace stored procedures because no longer would it be SQL code in a string in C# code, it would be C Omega code compiled running directly against the database connection.  Wow, a data tier with a real programming language!!  I am of the mind that the only code that should run on the database box is simple stored procedures if any.  This would be only CRUD sprocs, no SQL that would require debugging.  Any logic needs to be in a business object, not a procedure.  OOP.  Down with procedures!

Eric Wise gives interesting insight into methods of corporate America that destroys public corporations.
I agreed with a lot of what Eric put out.  No pension plan, decreasing medical coverage.  Just the term “human resources” irritates me to no end.  And the diversity campaigns?  I am very upset that of all the interns being hired this summer, the policy is that caucasion men and women are excluded from the internship program.  Period.  Not hiring whites.  As a rule.  Staffing will ONLY be hiring non-whites for summer interns.  And they call it “diversity”?  What a load of crap.  If I were the staffers, I’d have applications that didn’t include any demographics.  I would decide interviews based on the quality of the resume.  C’mon, let’s be fair.  Hire the most qualified. 

Wolf relates a client who was told that event-driven design was an extension of SOD.
This was a really great article.  I like wrapping my head around these design concepts that are being thrown around in discussion.  I do think that the terms are getting watered down and business people are starting to use them in meetings when they have no idea what they mean.  Or what about when your manager says to the development team: “My only requirement for this application is that it be n-tier and interoperable.”  What does that mean, anyway?  Sounds like some buzzwords strung into a sentence.

Steve Smith has returned safely from Iraq.
Welcome home, Steve!  Glad you back.  Wow, what a miracle that you only had to stay over there for 6 months.  I was over that for 1 year and 3 days with a 15 month mobilization in total.  I know how you feel because I got back from that dustbowl just a couple months before you left.  And for everyone reading this:  Stopping watch the large media now.  They report no good news from Iraq, only bad.  And they will try to twist anything to make it sound bad.  If you have been keeping up with what is going on, 1300 troops have died so far.  Less that any other war in our past. . . less that Houston’s traffic fatalities last year.  And the bad guys (Al Quaeda ) are blowing up Iraqis mostly.  We can pull out now, and Saddam won’t be able to go back, but Al Quaeda would rule.  Would that be a good thing?  Anyway, I’m glad you’re back, Steve.  Maybe we can swap Hadji stories at Tech Ed!

This Thursday, David W. will be putting on an MSDN event in Austin, TX at the Gateway cinema.  The topics will cover ASP.NET, WinForms and Team System.  This will be my third event, and David has been kind enough to slip our Austin .Net User Group info into his slide deck.

I have been swamped at work, so I’m a little behind on reading/writing blogs.  I’ve been slinging a lot of code.  We’re in a testing push toward deployment next month, and we’re setting the foundation for may projects to come with a few shared assemblies.  It’s exiting stuff and the first reall OO programming we’ve done as a team.  I say that because it seems like our previous .Net projects have used the .Net framework, but we didn’t incorporate OO in our design, just our implementation.  I think we’re on a better road now.

On Dec 22, I earned my MCAD.Net credential, and I hope to be MCSD by Tech Ed.  I plan on taking the WinForms with C# test next week, and then I’ll just have 70-300 to go.  Any book suggestions for 70-300 would be welcome.

Logging using the Trace and Debug classes in the .Net framework – level 300

I’ve seen several homemade logging mechanism used in various applications, and they all do their job, but every one of them seems to be very tightly coupled, and the source that does the logging must to changed to add a new logging destination.  I’d like to share a method that my team uses in our code.  When we wanted to get the information to another destination, the code doing the logging doesn’t have to know about it.  First, we need an object to hold our logging information.  I’ll call it TraceEntry:



using System;


using System.Collections;


using System.Diagnostics;


using System.Text;


 


namespace ConsoleTestHarness


{


    public class TraceEntry


    {


        protected string m_message = string.Empty;


        protected DateTime m_createdOn = DateTime.Now;


        protected TraceLevel m_entryLevel = TraceLevel.Info;


        protected Type m_caller;


 


        public string Message{


            get { return m_message; }


        }


        public DateTime CreatedOn{


            get { return m_createdOn; }


        }


        public TraceLevel EntryLevel{


            get { return m_entryLevel; }


        }


 


        public TraceEntry(string message, TraceLevel entryLevel, Type caller){


            m_message = message;


            m_entryLevel = entryLevel;


            m_caller = caller;


 


            Trace.Write(this);


        }


 


        // For the default trace listeners, this method will be called implicitly.


        public override string ToString() {


            StringBuilder builder = new StringBuilder();


 


            builder.Append(m_createdOn.ToString().PadRight(22));


            builder.Append(m_caller.Name.PadRight(15));


            builder.Append(m_entryLevel.ToString().PadRight(9));


            builder.Append(m_message);


 


            return builder.ToString();


        }


 


    }


}


 

Next, we need code to use this class to log information:


using System;


using System.Diagnostics;


using System.IO;


using System.Reflection;


using Dell.Titans.Framework.Diagnostics;


 


namespace ConsoleTestHarness


{


    class TestHarness


    {


        [STAThread]


        static void Main(string[] args)


        {


            Trace.Listeners.Add(new CustomTraceListener());


            new TraceEntry(“This is a message”, TraceLevel.Verbose, typeof(TestHarness));


            Console.ReadLine();


        }


    }


}


Notice that all we do is create the object.   The object, when created, sends itself to Trace.Write.  This is a great feature of the .Net framework that allows “listeners” to be attached to listen for messages.  Here we are sending a message, and our CustomTraceListener “hears” the object being sent to it.  Here’s the source for our listener:


using System;


using System.Diagnostics;


 


namespace ConsoleTestHarness


{


    public class CustomTraceListener : TraceListener


    {


        public override void Write(object o) {


            // If we know what this object is, we can work with it.


            if(o is TraceEntry){


                TraceEntry entry = o as TraceEntry;


 


                // Pull out the pieces of the object and log them where you wish.


                Console.WriteLine(entry);


            }


        }


        public override void WriteLine(object o) {this.Write(o);}


        public override void Write(object o, string category) {this.Write(o);}


        public override void WriteLine(object o, string category) {this.Write(o);}


 


        // abstract method implementations.


        public override void Write(string message) {}


        public override void WriteLine(string message) {}


    }


}


And there you have it.  Our listener receives this object.  It tests it to make sure it is of the type it understands, and then it can  use the different pieces of the object to log to fields in a database, log to a file, or anywhere else.  Here, we spit it out to the console, and notice how the ToString() overridden method was implicitly called.

 

A great way to extend this method is to add a listener to send a warning email to support personnel whenever a message with type “Error” is logged.

 

Happy coding.