Saturday, April 07, 2007

Finding your Solution/Discoverability for DotNet Developers.

   
I was listening to .Net Rocks today and they where interviewing Dan Appleman on discoverability. I thought when I herd this title it was going to be a show on how to make you application features get noticed, but this show was even better. This show was on how to make your life as a developer easier, easier by tipping you on how to find solutions to your problems that have already been solved. The best tip was on Google custom search this allows you to create your own seach that only searches sites that you specify. Dan Appleman has already done this work for us .Net Developers. He gathered up his favorite reference sites and created the site SearchDotNet. Try it out!


#     Comments [0]  
kick it on DotNetKicks.com
 Friday, April 06, 2007

Wow, Polita Paulus


I was listening to HanselMinutes last podcast and he and Rory Blyth where running around Microsoft building 42 trying to interview devs. They found Polita Paulus, she is the developer on the ASP.NET team who wrote the GridView, DetailsView, and FormView not only that created Blinq in her spare time. My first thought was this girl is the Queen of geeks and has got to be ugly (I know that is a terrible first thought). But to my surprise not only is she smart(like Genius level smart) and geeky(like writes Blinq in her spare time geeky) she is also very attractive. If I wasn't attached and totally intimidated by her I would probly be a stalker. I bet she doesn't date geeks, I could imagine the first date. So, what did you do today?  Oh nothing I just finished some tweaks with my new app that allows developers to create enterprise level applications with just one click. And you? I... wrestled a lion with my bare hands... Check Please!


#     Comments [0]  
kick it on DotNetKicks.com
 Thursday, April 05, 2007

Switching to C# from VB.Net

    My current job requires me to jump on the band wagon and start coding in the C# syntax for .Net. For the last year I have been coding in VB.Net and doing all my blog examples in VB.Net and I really started to enjoy that language. The My namespace allowed for quick access to a number of common functions, key words such as AndAlso and OrElse eliminated the need for extra if statements, the background compiler was great finding errors instantly, the VB.Net code is formatted automatically for you (in C# I find my self having to hit ctr+K+D a lot and some times still have to format braces my self), and the lack of {}.[], <>, and () makes the language easy to read.
    My switch to C# was relatively easy I can't quite code as fast as I am able to in VB.Net and I still try to write Dim, forget semicolons, and other little syntax issues like that. I like that C# seems to hide less from you than VB.Net one example is just having the namespace automatically at the top of the page. C# is also less wordy than VB.Net and I can see more of the code that is important to me at one time. Maybe its just because I am new to the language but C# seems to have less ambiguous ways of doing things as well, witch seems to make code from programmer to programmer more consistent. Example of this is casting int from a string in VB you can use Cint(str), Ctype(str,int), Integer.Parse(str), and Val(str). There are a lot of other instances of this in VB.Net I think it has a lot to do with helping the VB 6.0 coders with there transition to .Net.
    All and all it is a refreshing change and I like going back to C++ derived syntax I believe to progress as a programmer you really need to experience in multiple languages.

#     Comments [0]  
kick it on DotNetKicks.com
 Wednesday, April 04, 2007

The Strategy Pattern

    The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. What does this mean?
    From my understanding of the strategy pattern, you are separating the behavior from an object with the use of an interface. The interface defines the family of algorithms and you encapsulate each behavior with a class that implements this interface. With this use of the interface contained in the class you have the ability to reference different behaviors at runtime.
    To better understand this I created a simple app that is a form with a button. When this button is clicked it uses Reflection to load a .dll that contains a class that defines the behavior of the button.



The first thing I did to implement the strategy pattern was to create my interface that defines my family of behaviors/Algorithms.
namespace Patterns.Strategy.PluginInterface
{
public interface ICrazyBehavior
{
void GoCrazy();
}
}

In order to assign a concrete behavior I created a class that implemented this interface.

using Patterns.Strategy.PluginInterface;
namespace MyUsefullPlugin
{
class MyUseFullPlugin : ICrazyBehavior
{
public void GoCrazy()
{
System.Windows.Forms.MessageBox.Show("This Plug-in is out of controll");
}
}
}

Finally I wrote the class that contains the interface and all other content I needed.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using Patterns.Strategy.PluginInterface;
namespace Patterns.Strategy
{
public partial class PlugableStrategy : Form
{
private List<ICrazyBehavior> _crazyBehavior = new List<ICrazyBehavior>();
public PlugableStrategy()
{
InitializeComponent();
}
private void btnGoCrazy_Click(object sender, EventArgs e)
{
GetPlugins();
foreach (ICrazyBehavior thisBehavior in _crazyBehavior)
{
thisBehavior.GoCrazy();
}
}
private void GetPlugins()
{
_crazyBehavior.Clear();
string path = Application.StartupPath + "/Plugins";
string[] files = Directory.GetFiles(path, "*.dll");
foreach (string file in files)
{
try
{
Assembly asm = Assembly.LoadFrom(file);
Type[] types = asm.GetTypes();
foreach (Type type in types)
{
Type[] interfaces = type.GetInterfaces();
foreach (Type currentInterface in interfaces)
{
if (currentInterface.Name == "ICrazyBehavior")
{
if (currentInterface.Namespace == "Patterns.Strategy.PluginInterface")
{
object obj = asm.CreateInstance(type.Namespace + "." + type.Name);
_crazyBehavior.Add((ICrazyBehavior)obj);
}
}
}//interface
}//type
}
catch (Exception) { }
}//file
}
}
}

   

So, putting this all together I have my form/class that contains a button when some one clicks this button it calls GetPlugins(). The method GetPlugins() uses Reflection to find any .dlls in the plugins folder that contains the ICrazyBehavior interface with the name space of Patterns.Strategy.PluginInterface. If such thing exists in the folder it creates an instance of this object and adds it to my list of _crazyBehaviors once the list is filled the click event continues, it iterates through the list calling the method GoCrazy().

    Using the strategy pattern allowed me to decouple the context class and its behavior enabling me to implement a very "useful" pluggable application.

Dowload The Complete Code Below:

Patterns.zip (83.64 KB)
#     Comments [3]  
kick it on DotNetKicks.com
 Tuesday, April 03, 2007

Long Time No Post

Its been a wile sense my last post but I have a good excuse. My blog was contending with finals from last quarter and a new job. I am now working for The PBA as a C# application developer and I love it.  It is a great opportunity and I get to work with great people. But now that I have finally settled in too my new job and new quarter of school I can continue my quest for mastering patterns I have already started working on a good example of the strategy pattern and will have it completed and posted by the end of the week. Cheers all I have to catch my bus.

#     Comments [0]  
kick it on DotNetKicks.com