I'm going to start a series of posts tagged [Beginning], to try and help new programmers, or anyone with an interest in programming understand concepts using real-world examples.
The first in this series, value types and reference types.
So, what is a value type?
msdn says:
Variables that are based on value types directly contain values.
more value types at msdn
And, what is a reference type?
msdn says:
Variables of reference types, referred to as objects, store references to the actual data.
more reference types at msdn
that's like defining the word the word, right? It seems like that's what everyone says for both... I think we can do better.
In simplest real-world forms:
value types are like money, cash, dinero, coin, dough...
you got the cash in your hand, now go buy an LCD TV!
reference types are like personal checks...
you got something in your hand, but you have to walk to the bank and turn that piece of paper into real money. (Now you can get your LCD TV)
Here's a quick little one-liner I use all the time.
It will create a concrete instance that implements 'I' interface. This has a string as a parameter, but there are lots of other possibilities (and overloads for Type and CreateInstance).
But, be careful with this, you can easily get into trouble, as the magic happens at runtime, so watch your speeling.
public class Activator<I> { public static I Create(string s) { return (I)System.Activator.CreateInstance(Type.GetType(s)); } }
Here are my favorite Firefox add-ons
Web Developer tool bar (gotta love Ctrl+Shift+f)
Firebug (the best javascript debugger and more)
Html Validator (great for seeing all your awesome htmlz c0dez)
Sqlite Manager (the version for Firefox 3.5 is much improved)
Greasemonkey (custom scripts)
Modify Headers (I use to test other user agents)
Live Page Rank (get google page rank for the site you're on)
Search Status (lots of SEO goodies)
Add n Edit Cookies (easily add and/or modify cookies)
Remove Cookies for Site (removes cookies only for the site you're on)
ColorZilla (eyedropper tool to get any color on a web page, in an image, or html)
refspoof (change the referring url)
YSlow (figure out why your site is slow :( )
Hackbar (test your site with various sql injection strings)
So, to begin, the following has NOT happened to me, but I wanted to make sure people understand why public computers and certain browser features can be bad... very bad for you.
Let's say you sign in to your email account on a public, or unfamiliar computer. The machine asks you to save the password for that site, and silly you, chooses to accept. OK. So, now you've finished your work,you haven't erased your tracks, and the next person comes along... if the next person comes to the same email provider that you have visited, they'll see something like this :
| username | |
| password |
yikes
So yeah, they can login as you, see all your mail, perhaps other sensitive information in emails...in short, they can find out a lot. If they were really naughty, they would just do this in the address bar and get your password :
javascript:alert(document.getElementById('Pass').value);
without looking at the src code on this page, you can try it here and see what I've set as the password above.
What can you do? Well, here are 5 safety tip for using a public computer.
Year after year you know you have a product that will be updated at a[round] a certain month. It happens a lot with my business domain. Rather than changing lots of text, this should work nicely for my needs. (I didn't use any timezone or culture considerations)
the pivot month is the int that decides when we flip to next year.
/// <summary> /// Figures out if should use this year ///or next based on a defined pivot month. /// </summary> /// <returns>4 digit year</returns> public int YearPivot(DateTime dt, int pivotMonth) { return (dt.Month <= pivotMonth) ? dt.Year : dt.AddYears(1).Year; }
couple tests showing it in action...
DateTime dt = new DateTime(2009, 10, 1); //returns 2009 Assert.AreEqual(2009, m.YearPivot(dt, 10)); //returns 2010 Assert.AreEqual(2010, m.YearPivot(dt, 5));
I work for a fabulous company, I've been with them for about 3 years. They are incredible about making sure developers have what they need to get the job done, whether it's software or hardware (or classes, or books). In fact, I'm on my third development machine! Most software requests are usually also granted.
So what does your gear look like? What development software has your company gotten for you to help better your work?
This is what it looks like for me:(as of posting)
Hardware:
- Windows XP 64, core 2 quad @ 2.5 GHz, 8 gigs of ram
- 3 19 inch widescreen monitors
- microsoft natural ergo keyboard
- trackball mouse
- physical server 03 box [for testing]
- various XP/03 virtual machines [more testing]
Software:
- Resharper 4
- dotTrace
- NDepend
- NCover complete
- RegexBuddy
- BareTail Pro
- All the microsoft dev. stuff (VS 03,05,08, SQL server 05,08)
- Snag it
- ultramon
What about you?
Here's a better version of string.IsNullOrEmpty(). Why is it better you say? Well my friend, this one checks the string for spaces... It also returns a positive statement (true if it is NOT null and NOT empty)
public static class Extensions { public static bool IsSafe(this string s) { return (s != null && s.Trim().Length > 0); } }
And some unit tests...
[Test] public void IsSafeStringTest() { string cheese = "is good"; Assert.IsTrue(cheese.IsSafe()); cheese = ""; Assert.IsFalse(cheese.IsSafe()); cheese = " "; Assert.IsFalse(cheese.IsSafe()); cheese = null; Assert.IsFalse(cheese.IsSafe()); //------------------------------- cheese = "yum"; Assert.IsFalse(string.IsNullOrEmpty(cheese)); cheese = ""; Assert.IsTrue(string.IsNullOrEmpty(cheese)); cheese = null; Assert.IsTrue(string.IsNullOrEmpty(cheese)); // rut roh... // string.IsNullOrEmpty() and spaces are not nice together. cheese = " "; // should be true! Assert.IsFalse(string.IsNullOrEmpty(cheese)); }
While the following might be nice, we know that we can't do it because of the null reference exception we might get (if cheese were to be null)...
string.IsNullOrEmpty(cheese.Trim());
Anyway, while we're doing some extension methods, here's a similarly named one I also use when working with generic lists.
public static bool IsSafe<T>(this List<T> t) { return (t != null && t.Count() > 0); }
And some unit tests...
Yo yo howdy-doody. I'm doing a talk on NUnit (and friends) with VS 2008 at CTDOTNET's second CodeCamp -- June 13th, 2009.
It's an intro to unit testing, but I'll talk about reasons why testing is beneficial, how to setup a unit testing environment, and tools (friends) to use along the way.
Get the deets about the day.
Hope to see you there!
This post will allow you to unit test objects that have a dependency on a SqlDataReader by mocking/stubbing a data reader.
Here I've made a 'wrapper' of sorts for the good ol' Microsoft SqlHelper class, which is ancient now, ya? Since the SqlHelper.ExecuteReader's method class implements the IDataReader interface we're in luck.
public interface IFetch { //add members as necessary IDataReader ExecuteDataReader(string connectionString, string storedProcedureName,SqlParameter[] parameters); } public class Fetch : IFetch { public IDataReader ExecuteDataReader(string connectionString, string storedProcedureName, SqlParameter[] parameters){ return SqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure,storedProcedureName, parameters); } }
Next we create our object from the results of our IDataReader. The SqlHelper class removes the worry about mocking SqlCommand and SqlConnection classes. All you need to worry about are the results (SqlDataReader).
(You can make your own RockStar object, fo sho.)
Whether you call it constructor initialization or constructor chaining, we have it here, and will allow us to inject the correct dependency, in this case, an interface that defines an ExecuteReader method. (I've never gotten into DI/IoC frameworks, should I, and which??)
private readonly RockStar rs; private readonly IFetch fetch; //here's the magic sauce for the unittests public FetchCms(IFetch fetch, RockStar rs){ this.fetch = fetch; this.rs = rs; } //production code calls this which calls //the other constructor. mmm. public FetchRockStar() : this(new Fetch(), new RockStar()) { } public RockStar GetRockStar(string urlToLookup){ try { var parms = new[] { new SqlParameter("@urlName", urlToLookup) }; using (IDataReader reader = fetch.ExecuteDataReader("localhell", "rockstar_page_get",parms)){ if (reader != null && reader.Read()){ rs.Name = reader["name"].ToString(); rs.Guitar = reader["guitar"].ToString(); //add some logic... and so on... } } } catch //swine flu {} return fs; }
ok, so now your production code is hawt, but how do you 'fake' a reader, or 'fake' its results? Good question, but never fear, Phil Haack has you covered with his totally rock-tacular StubDataReader class. Get it.
so now, in your UnitTest project...
I'm creating some fake results that I can use to test my expectations, using the StubDataReader class you've downloaded above and added to your UnitTest project.
private static StubDataReader getData_Kerry_King(){ var resultSet = new StubResultSet("name", "guitar"); resultSet.AddRow("Kerry King", "B.C. Rich"); return new StubDataReader(resultSet); }
I'm using the Moq framework to mock my interfaces. (it's from an older release, hence the Expect calls rather than the Setup calls [anyone: what does the new version get me?])
[Test] public void Execute_DataReader_Test() { var m = new Mock<IFetch>(); //here i'm expecting that my ExecuteReader implementation //return my 'fake' data m.Expect( x => x.ExecuteReader( It.IsAny<string>(), It.IsAny<string>(), It.IsAny<SqlParameter[]>() ) ).Returns(getData_Kerry_King()); var rockStar = new RockStar(); // pass IN the interface using the special constructor. var fetch = new FetchRockStar(m.Object, rockStar); rockStar = fetch.GetRockStar("kerry-king"); //assert Assert.AreEqual("Kerry King",rockStar.Name); Assert.AreEqual("B.C. Rich",rockStar.Guitar); }
Even Kerry thinks you should be testing!

















