Null !== Steve

Steve Gentile's Tech Blog – Thoughts and Musings

Archive for the category “General”

PropertyChangedEvent using Static Reflection (Expression)

Feel free to correct me if this is a bad way of going about using static reflection… but I had a change to remove a ‘magic string’ and figured I’d use static reflection as a way around it.

I was working on a Silverlight sample of Two-way binding and re-reading some of John Papa’s Data-Driven Services with Silverlight 2 book where he is showing that in order to use two-way binding, you must implement the INotifyPropertyChanged interface.

In his book he shows a way to wrap up the duplicating code into a base class, ie:

image

To use this, the following must be done to the entity:

image

The problem I had with this is the ‘PropertyChangedHandler’ taking in a string that is to be the property name.

I’m sure there is a better way to handle this, but I decided to use some static reflection to hand it the property name:

image

Now, I can use this to replace the above “magic string”:

image

IE 8 – Emulate IE 7

This is a good one – as I’m sure many, including my own applications, aren’t quite ready for IE 8.   The nice part of this is it’s possible to tell IE 8 to emulate with IE 7. 

A good post on this can be found here:

http://blogs.msdn.com/ie/archive/2008/06/10/introducing-ie-emulateie7.aspx

There are two ways to implement this tag:

On a per-site basis, add a custom HTTP header : X-UA-Compatible: IE=EmulateIE7

On a per-page basis, add a special HTML tag to each document, right after the <head> tag :

<meta http-equiv=”X-UA-Compatible” content=”IE=EmulateIE7″ />

 

I was able to use the per-page on some pages, which forces the IE Emulate.  Then later, I can go back and find out what craziness is going on and remove the tag.

This is much better than trying to add yet another browser check in javascript as far as I’m concerned!

Json.NET 3.0

Has been released!  I’m a big fan of this library, it’s one of those ‘it just works’ projects  :)

Of greatly added benefit is that he has now added support for Silverlight.

Awhile back when I was playing with Silverlight, I was working with Json serialization, and the support within Silverlight didn’t work, but this library would.

Thanks again to James Newton-King for this release!

Key features:

  • Silverlight Support
  • LINQ to JSON even easier to use
  • Serializer Improvements

Document Map Margin

So I saw this in my VS studio ‘news’:

http://blogs.msdn.com/charlie/archive/2008/08/15/future-focus-document-map-margin.aspx

Now, I had to laugh, as I hadn’t read the actually post yet, but the synopsis is:

Developers frequently work with huge source files that are difficult to navigate. The proposed DMM feature is designed to make it easier for you to find and track important features in your code and to visualize the overall structure of your file.

So… my first thought was: why don’t they refactor it so you don’t have ‘huge source files’  :)  

The real problem in my opinion is that rather than add a ‘document map’, how about we add better refactoring tools to help developers learn to get rid of these ‘code smells’ ?

ie. Resharper is good – but cost an arm and a leg as far as I’m concerned – meanwhile, the VS refactoring tools are kind of crappy – sure, it’s better than nothing, but they don’t even come close to what Resharper or Eclipse has.

Address the real issue  :)

Some String Formatting Examples with C#

First, check out this good blog post:

http://blog.stevex.net/index.php/string-formatting-in-csharp/

So, my example was that a flat file I’m parsing contains the social security number, but with no “-” dashes. 

ie. 111223333

(note this is a string in the flat file).

 

string ssn = String.Format("{0:###-##-####}", Int32.Parse(swipe.MemberID));
                

The format is expecting a number, so I convert the “111223333″ into number, then feed it into the String.Format.  Works like a charm…

 

EDIT: Bug fix alert! Although the above does work – it does not take into account of leading zero’s!  The fix is:

string ssn = String.Format("{0:000-00-0000}", Int32.Parse(swipe.MemberID));
 
This will preserve any leading zeros

 

Here is another fun one, I have a date in CCYYMMDD  (yyyyMMdd)

DateTime serviceDate = DateTime.ParseExact(swipe.TransactionDate,
 "yyyyMMdd", new CultureInfo("en-US"));

Last little gem:

taking a int of 0 or 1 and making it ‘yes’ or ‘no’ :

String.Format(”{0:yes;;no}”, value)

=> yes if value == 1
=> no if value == 0

That is handy  :)

 

John Sheehan has a nice ‘cheat sheet’ section of his website: .NET Format String Quick Reference

Service Pack 1 for .NET 3.5 and Visual Studio 2008

MSDN Link for Download

Jonathon Allen mentions some of the updates included:

 

“While called a “service pack”, the sheer number of new features makes this release as significant as .NET 3.0. These include the controversial ADO.NET Entity Framework and the much demanded improvements to C#’s real-time syntax checker.

Other features include:

ASP.NET Dynamic Data: This wizard auto-generates database-backed web sites without the need for any hand-written code. From there developers can use it as-is or customize it as if it were any other ASP.NET based project.

ADO.NET Data Services: This framework makes it easier for developers to create REST-style interfaces for querying and update data. A key advantage over Web Services is that is has a URL-based query language is built directly into the framework, eliminating the need to invent a new one for each application.

.NET Client Framework: This drastically smaller version of the .NET Framework contains only the APIs that client applications tend to use. Tools built into Visual Studio will determine if your application is a candidate for the version. This feature is most suited to ClickOnce style deployments where the user is expected to download the framework over the Internet.

There are also numerous enhancements to the IDE in areas such as Website deployment, JavaScript/AJAX coding, and WPF form design.

This release also includes .NET 2.0 SP2 and .NET 3.0 SP2, which means regression testing is in order.”

Edit:

Also available : With the release of SP1 for Visual Studio 2008 today, the Silverlight team has also updated their tools for Silverlight 2 Beta 2

Learn more here

Ankhsvn 2.0

I’m installing Ankhsvn 2.0 today – I had tried earlier versions but the new one looks good.  Here are some screenshots.

Check out the 2.0 final release comments to learn more of the changes, features, etc…

Ruby and Visual Studio – Sapphire Steel

http://www.sapphiresteel.com/-Blog-

This might be the occasion to really jump in and try Ruby.  Sapphire Steel has Ruby integration with Visual Studio, including drag and drop UI for building forms, etc…

 

Update:  I didn’t realize this wasn’t opensource.  I will pass on it for now.  If anyone has a good recommendation for a ruby ide let me know

Posting comments and WordPress Themes

I’ve switched back to more of an original wordpress theme, it appears the one I was using has problems with comments.

I know the problem with the default is that the text runs off to the right, so I’ll have to investigate that next.

So, you might see the themes change as I experiment with WordPress  :)

Thanks

Ayende on “A maintainable environment – anti corruption as a way of life”

Ayende has a post that exemplifies principles that I see as extremely important to a good development environment.

Some key points:

    • Infrastructure Ignorance – If I have to deal with my infrastructure explicitly, then I need to have developers dealing with it on a regular basis. That doesn’t mesh well with the idea of moving all the complexity to the infrastructure.
    • Fizz Buzz concepts – the code that a developer should write should be simple and easy to follow.
    • Follow good design principals – the code written should naturally lean toward single responsibility principal, separation of concerns, etc
    • Easy to figure out – It should be simpler than the alternative for any non trivial scenario. Ideally, you can explain how to solve most scenarios using this approach in half an hour or less (ignoring the infrastructure part, which may be very complex).
    • No training wheels – one of the things that drives me crazy is having two ways to do things. One for the average developer, the second for the architect. This usually comes out of putting training wheels on the first approach that make it hard to deal with in more advance scenarios. I see this as a sign of contempt for the rest of the members of the team. If you expect the rest of the team to work in a certain way, make damn sure that this is a way that this is a way that you can live with.

“The code that a developer should write should be simple and easy to follow”

There is so much about this that needs to be expounded upon. I really think the Single Responsibility Principle(SRP) is vital to this concept. I’ve seen some pretty ugly looking code all over the place where I’m at right now. Very large functions that are procedural in nature. I’d personally rather have many classes than classes stuffed with tons of code. Also, too much code in asp.net code behind pages is a major ‘no-no’. For myself, I’ve had to do much refactoring to get code back into business/presenter classes and out of the code behind. (This is one reason I like the Monorail/MS MVC approach). I could go on and on about this one.

“Easy to figure out”

Whew, amen here. Here is my code smell for this one: When you have no code in the business layer and all it is a pass through. Why do I say this ? Typically this means all the logic is in the UI or in the data layer. This is why I’m a big fan of a domain driven design with clean POCO business objects. Separation of Concerns is important here. I know it’s always debatable in the MS developer world, but when I see datasets I cringe. I think datasets tend to make developers think just in terms of the data layer. They are confusing to see the domain picture. So for me ‘Easy to figure out’ == well defined domain model.

I should add, I think creating a rules engine (IRules) and/or validation objects that can be kept away from the actual entity objects is important. This allows me to see the domain without clutter of all the rules (ie. different sets of business rules for a each client – the entity would hold a collection of ‘IRule’ objects associated to it’s particular instance)

The struggle is working in environments where principles aren’t openly discussed. I think ‘best practices’ should be complemented with developers all speaking a common language of good OO principles. On some teams I really enjoyed working with, stressing these principles made us a successful team, it was a very growing experience to me as a developer.

Post Navigation

Follow

Get every new post delivered to your Inbox.