Subscribe: Josh Schwartzberg high-fives the CLR
http://weblogs.asp.net/dotjosh/rss.aspx
Added By: Feedage Forager Feedage Grade B rated
Language: English
Tags:
code  configuration  database  file  frameworkelement  method  mvc  net  new  nhibernate  private  public  scrollviewer  var  view  void 
Rate this Feed
Rate this feedRate this feedRate this feedRate this feedRate this feed
Rate this feed 1 starRate this feed 2 starRate this feed 3 starRate this feed 4 starRate this feed 5 star

Comments (0)

Feed Details and Statistics Feed Statistics
Preview: Josh Schwartzberg high-fives the CLR

Josh Schwartzberg high-fives the CLR



and spouts random buzzwords



 



ASP.NET MVC for the php/asp noob

Tue, 20 Apr 2010 19:37:00 GMT

I was talking to a friend today, who's foremost a php developer, about his thoughts on Umbraco and he said "Well they're apparently working feverishly on the new version of Umbraco, which will be MVC... which i still don't know what that means, but I know you like it." I ended up giving him a ground up explanation of ASP.NET MVC, so I'm posting this so he can link this to his friends and for anyone else who finds it useful.  The whole goal was to be as simple as possible, not being focused on proper syntax. Model-View-Controller (or MVC) is just a pattern that is used for handling UI interaction with your backend.  In a typical web app, you can imagine the *M*odel as your database model, the *V*iew as your HTML page, and the *C*ontroller as the class inbetween.  MVC handles your web request different than your typical php/asp app.In your php/asp app, your url maps directly to a php/asp file that contains html, mixed with database access code and redirects.In an MVC app, your url route is mapped to a method on a class (the controller).  The body of this method can do some database access and THEN decide which *V*iew (html/aspx page) should be displayed;  putting the controller in charge and not the view... a clear seperation of concerns that provides better reusibility and generally promotes cleaner code. Mysite.com, a quick example:Let's say you hit the following url in your application: http://www.mysite.com/Product/ShowItem?Id=4 To avoid tedious configuration, MVC uses a lot of conventions by default. For instance, the above url in your app would automatically make MVC search for a .net class with the name "Product" and a method named "ShowItem" based on the pattern of the url.  So if you name things properly, your method would automatically be called when you entered the above url.  Additionally, it would automatically map/hydrate the "int id" parameter that was in your querystring, matched by name.Product.cspublic class Product : Controller{    public ViewResult ShowItem(int id)    {        return View();    }} From this point you can write the code in the body of this method to do some database access and then pass a "bag" (also known as the ViewData) of data to your chosen *V*iew (html page) to use for display.  The view(html) ONLY needs to be worried about displaying the flattened data that it's been given in the best way it can;  this allows the view to be reused throughout your application as *just* a view, and not be coupled to HOW the data for that view get's loaded.. Product.cspublic class Product : Controller{    public ViewResult ShowItem(int id)    {        var database = new Database();        var item = database.GetItem(id);        ViewData["TheItem"] = item;        return View();    }} Again by convention, since the class' method name is "ShowItem", it'll search for a view named "ShowItem.aspx" by default, and pass the ViewData bag to it to use. ShowItem.aspx           <%        var item =(Item)ViewData["TheItem"]       %>      

<%= item.FullProductName %>

     BUT WAIT! WHY DOES MICROSOFT HAVE TO DO THINGS SO DIFFERENTLY!?They aren't... here are some other frameworks you may have heard of that use the same pattern in a their own way: Ruby On Rails Grails Spring MVC Struts Django    [...]



Monitoring settings in a configsection of your app.config for changes

Wed, 24 Mar 2010 15:08:00 GMT

The usage:public static void Main() { using(var configSectionAdapter = new ConfigurationSectionAdapter("MyConfigSectionName")) { configSectionAdapter.ConfigSectionChanged += () => { Console.WriteLine("File has changed! New setting is " + configSectionAdapter.ConfigSection.MyConfigSetting); }; Console.WriteLine("The initial setting is " + configSectionAdapter.ConfigSection.MyConfigSetting); Console.ReadLine(); } }  The meat: public class ConfigurationSectionAdapter : IDisposable where T : ConfigurationSection { private readonly string _configSectionName; private FileSystemWatcher _fileWatcher; public ConfigurationSectionAdapter(string configSectionName) { _configSectionName = configSectionName; StartFileWatcher(); } private void StartFileWatcher() { var configurationFileDirectory = new FileInfo(Configuration.FilePath).Directory; _fileWatcher = new FileSystemWatcher(configurationFileDirectory.FullName); _fileWatcher.Changed += FileWatcherOnChanged; _fileWatcher.EnableRaisingEvents = true; } private void FileWatcherOnChanged(object sender, FileSystemEventArgs args) { var changedFileIsConfigurationFile = string.Equals(args.FullPath, Configuration.FilePath, StringComparison.OrdinalIgnoreCase); if (!changedFileIsConfigurationFile) return; ClearCache(); OnConfigSectionChanged(); } private void ClearCache() { ConfigurationManager.RefreshSection(_configSectionName); } public T ConfigSection { get { return (T)Configuration.GetSection(_configSectionName); } } private System.Configuration.Configuration Configuration { get { return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); } } public delegate void ConfigChangedHandler(); public event ConfigChangedHandler ConfigSectionChanged; protected void OnConfigSectionChanged() { if (ConfigSectionChanged != null) ConfigSectionChanged(); } public void Dispose() { _fileWatcher.Changed -= FileWatcherOnChanged; _fileWatcher.EnableRaisingEvents = false; _fileWatcher.Dispose(); } }[...]



"And you wanted to blame NHibernate!?" -Ayende

Thu, 14 Jan 2010 05:04:00 GMT

With permission from Ayende, here is a great sound byte from one of the TekPub video series (which I highly recommend) on NHibernate. 

Feel free to send it to people who erroneously blame NHibernate, I know I will!

*MP3 FILE DOWNLOAD: "And you wanted to blame NHibernate?!"




My short answer to "If no other engineering discipline does it first, why should I TDD?"

Mon, 21 Dec 2009 18:15:00 GMT

I agree Software Development by the truest form of the word, is in fact Engineering, but not all parallels can be made to its hardware counterparts, especially in its current state of maturity.   We can build high amounts of complexity with few people in a very short period of time.  All of which relies on other systems that are relatively unproven and unstable, it's hard to have a proper engineering process in these conditions.

This is why we need better code that can safely react to change.  In my opinion, "better" code in statically typed languages is inherently easier to test.   Its more isolated/abstracted and cohesive for its purpose.. and so are its supporting unit tests.   This ensures small and large changes alike have minimum impact on the existing, unrelated code... without a nasty ripple effect.

TDD is just one ritual to writing code that I loosely term "better", it's definitely not an end-all.  Clearly it's easy to assume writing more tests is the only correlation in less buggy software, but what the paper(s) don't show in numbers are the increased agility in the software process as every day brings a new change to the design that can be made more safely. 

There is still a large human aspect to this whole process, and given short-term cost-benefit analysis', many times the code quality suffers; building rigid, new untested features that only cause growing pains and degrades the testing ritual that were initially put in place.  That said, the cost-benefit analysis' do have to be made, and I wouldn't say that all bugs are worth fixing or can be fixed for a timely release.   But you have to ask what the long-term cost is to a buggy system that becomes difficult to test or change without fear.

I'd like to hear everyones opinion on the matter.




Silverlight BringIntoView() extension method (with OnGotFocus behavior)

Thu, 05 Nov 2009 14:23:00 GMT

It all started because I couldn't find a way to automatically scroll any element into view in Silverlight (a feature that exists in WPF).  I take that back, I could get the job done with a ListBox's ScrollIntoView(ListBoxItem item) method, but I hardly wanted everything on my screen to be wrapped as a ListBoxItem; it feels as dirty as it sounds.   Anyways, here is the code./* Extension Methods */ public static class FrameworkElementExtensions { private const int ScrollPadding = 10; public static void BringIntoView(this FrameworkElement frameworkElement) { var parent = VisualTreeHelper.GetParent(frameworkElement); while(parent != null) { parent = VisualTreeHelper.GetParent(parent); var scrollViewer = parent as ScrollViewer; if(scrollViewer != null) { frameworkElement.BringIntoViewForScrollViewer(scrollViewer); break; } } } public static void BringIntoViewForScrollViewer(this FrameworkElement frameworkElement, ScrollViewer scrollViewer) { var transform = frameworkElement.TransformToVisual(scrollViewer); var positionInScrollViewer = transform.Transform(new Point(0, 0)); if (positionInScrollViewer.Y < 0 || positionInScrollViewer.Y > scrollViewer.ViewportHeight) scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + positionInScrollViewer.Y - ScrollPadding); } }   Bonus Behavior! (Behaviors?!? Here is an Introduction).This behavior was created ensure that as a user tabs through the screen, scrolling automatically takes place. Note: If you want to use behaviors in Silverlight, install the Blend 3 SDK and reference Microsoft.Expression.Interactions.dll /* Behavior class */ public class BringIntoViewOnFocusBehavior : Behavior { protected override void OnAttached() { base.OnAttached(); AssociatedObject.GotFocus += OnGotFocus; } protected override void OnDetaching() { base.OnDetaching(); AssociatedObject.GotFocus -= OnGotFocus; } private void OnGotFocus(object sender, RoutedEventArgs e1) { AssociatedObject.BringIntoView(); } }   [...]



Silverlight 3 quietly released a day early

Thu, 09 Jul 2009 19:56:00 GMT

(image)  

I don't see any official word yet, but here are the links to the binaries:

I'm just hoping that I'll be able to multitarget for Silverlight 2 and Silverlight 3 apps now.




Visual Studio "Error of the Day"

Mon, 06 Apr 2009 14:28:00 GMT

 Seriously?

(image)  




MVC/JQuery meets X-Mas Music Project

Tue, 16 Dec 2008 02:26:00 GMT

My friend Dave and I embarked on a project a few weeks ago, initially just meant to be an audio CD for our family, that included some rockin' versions of everyone's christmas favorites.  As we started to record it, we got more and more excited about how it might turn out.  Being the uber code master that I am, I opted to make a website... then in a blink, a friend who is incredible at Flash offered to help. This turned out to be a great experience for all of us, and the feedback has been tremendous.  I'm very proud to share with you (make sure your speakers are on) www.metalxmas.com  The code-behindI wanted to have a spot for friends, family, and haters to post their honest ramblings, so I decided to go with the much-loved JQuery/ASP.NET MVC combo; what a joy as always.  One bug I ran into, that seems to have been noticed before me, is the ability to cache the root page of your ASP.NET MVC site using the OutputCache attribute.  According to this thread, the mvc team will have a fix soon.  For the time-being, I ended up caching at the repository level by just sticking the data response from disk into the HttpContext cache to ensure I only read and parse my xml file every 10 seconds.Nothing else too interesting came up. I added some spam protection, input validation (client and server-side), and HtmlEncoding to protect any hijacks.  All in all, another great experience.  I would share the code, but I have a certain Oxitephobia. ScalabilityIn the hopes (and delusions of grandeur) of this thing being picked up as even a semi-viral hit.  I wanted to make sure the site wouldn't go down.  I placed the flash file, the four mp3 files, the images, and the css file on a CDN.  I initially started with Amazon S3... but at 15cents/gig compared to SimpleCDN's 6cents/gig I had to switch.  I've been very happy with my experience, and they even include $15 worth of free credit when you first sign-up.  A friend of mine suggested I use the YSlow plugin to analyze my site; this ended up with the following configuration: HTTP Compression enabled on CDN (via url configuration) and IIS (c'mon, if you haven't done this yourself already, DO IT NOW) Set content expiration on all files on CDN to have 10 year expiration headers (I named the files themselves with version numbers to allow for forcefully expiring if needed) JQuery and SWFObject scripts hosted via google All scripts at the bottom XHTML 1.0 Strict validated by W3 This ended me with an A score with YSlow.  I felt accomplished. Parting thoughtsI hope you enjoy it, it's definitely meant to be funny - so don't take it too seriously.  Feel free to pass it along to your friends and family![...]



Hiring! Tampa Bay Developer

Mon, 31 Mar 2008 19:24:00 GMT

Are you a highly motivated .NET code monkey developer in the Tampa Bay area with a passion for software and the ability to learn quickly?

 (image)

if(not){return;}

Come join our top-notch team of developers who utilize the latest technologies (JQuery, NHibernate, CruiseControl, Nant, Resharper, etc.) to build the essential tools for the nation's largest and fastest-growing audiovisual firm.


The ideal candidate will have:

  • Minimum 2 years experience developing applications.
  • Minimum 1 year experience with C# in ASP.NET and/or Windows Forms.
  • Strong experience with MS SQL Server.
  • Solid skills and knowledge of HTML/DHTML, JavaScript and XML.
  • Excellent written and verbal communication skills.
  • Ability to learn quickly, as well as think and work independently.
  • Passion for learning new technology and implementation techniques
Apply now and come play with us



NHibernate querying without mapping inverse relationships

Thu, 28 Feb 2008 16:00:00 GMT

In my neverending quest to keep my domain minimal, I ran into a query that I wanted to perform with NHibernate that seems to be impossible without adding an additional property and hbm mapping definition. Note: I still consider myself rather new to NHibernate, so this might have an obvious answer. Let's say I have following two classes:public class School : EntityBase{     public string Name { get; set; }     public IList Students { get; set; }}public class Student : EntityBase{     public string Name { get; set; }} The School.hbm.xml file contains this in the body:         The database representation of the above looks something like this: Without making any changes to my classes/mappings, I can perform this SQL query to retrieve all Students who's name contains 'Powers' that belong to a school who's name contains 'Middle':SELECT * FROM StudentsWHERE      Students.Name LIKE '%Powers%'     AND     Students.SchoolId IN (SELECT School.Id FROM Schools WHERE Schools.Name LIKE '%Middle%') So - Here is the question, how can I do this in an HQL query without making any changes to my domain (which would be specifically adding a School property to the Student class and defining the relationship in the Student.hbm.xml file)?[...]



Inheriting code (that smells)

Fri, 14 Dec 2007 19:23:00 GMT

Josh: I am excited to be furthering the development of your tremendously popular website.  How may I gain access to the source control repository?
Customer: Give me a second... *You've got mail! - sourcecode.zip*

Wow, this isn't my ideal way of getting my hands dirty in a project.  But, this will be easy to fix.

Opening this .NET 1.1 ASP.NET app, I find three projects and no solution file.  Confused, I open the projects individually and realize there is a huge circular reference issue that was circumvented by compiling the projects seperately and then referencing their output assemblies. Yuck. There are also heaps of bugs in the code causing all sorts of unneeded memory usage that's causing the application to freeze and recycle several times a day.  Finally, I'm told that all of the latest features created for the site have been causing problems everywhere else.  I think it's time for a change.

After several weeks of unit tests, refactoring, SVN, and CC.NET configuration.

  • The site no longer crashes.
  • Features can be implemented faster and more effectively.
  • Source control provides a source history with much safer storage.
  • A new version of the site can be deployed with the click of a button.
 There is still a lot of work to be done.  But, I think this goes to show the power and reason for being agile. 



Windows Live and Windows Media Center are becoming friends.

Thu, 26 Jan 2006 16:26:00 GMT

It appears that the new Windows Live services portal that Microsoft has been pushing has finally caught my eye.  A new service that will allow me to communicate with Windows Media Center over the Internet. The interactions allow users to remotely record television shows, find programming information, and discover viewing preferences from buddies in their MSN Messenger social network.

Now they aren't saying yet if this service will allow for remote viewing, but lets hope we hear more about that soon. Kris Barton at Microsoft provided this screenshot of this sexy service in action.



Web Authoring Statistics from Google

Wed, 25 Jan 2006 21:20:00 GMT

Google has posted a very interesting analysis of the code/authoring techniques of over one billion documents here
It seems much of the data they collected was pretty obvious (ex: the abundance of  the "a" and "img" element).   But, it's still an interesting read.



New Vista Website

Thu, 15 Sep 2005 17:12:00 GMT

I'm liking the new vista website design.

http://www.microsoft.com/windowsvista/default.mspx




iPod Nano Picture

Thu, 08 Sep 2005 19:00:00 GMT

Wow this thing really is small.  But, where is the headphone jack?

(image)



10 Free PC Games for download

Wed, 24 Aug 2005 17:24:00 GMT

CNet has a list of 10 PC Games available for free download.
  • America's Army: Special Forces
  • Battlecruiser 3000 A.D.
  • Grand Theft Auto & Grand Theft Auto 2
  • Hidden & Dangerous Deluxe
  • The Marathon Trilogy
  • Return to Castle Wolfenstein: Enemy Territory
  • Starsiege: Tribes
  • Wild Metal Country
  • The Zork Trilogy







Insane Ukulele skills...

Sun, 07 Aug 2005 17:06:00 GMT

http://www.ukuleledisco.com/jakeconcert

Here's another one: http://movies.collegehumor.com/items/2005/06/collegehumor.159572.mov

I've never heard anyone play a ukulele like this... his name is Jake Shimabukuro jakeshimabukuro.com




Live 8 Performances

Thu, 14 Jul 2005 18:26:00 GMT

Here's a link to every Live 8 performance (Good quality too)



Free GMail - First come first serve

Fri, 02 Jul 2004 18:01:00 GMT

I've got 5 invites left on my GMail account.  The first 5 people to send me a request message (please include your email address) will get one.

 

**UPDATE! I'm all out of Gmail Invites now sorry!  When I get more invites, I'll make them available again!**