Miguel A. Castro's Blog

# Thursday, November 29, 2012

I recently started working heavily with KnockoutJS and I gotta say it’s one of the coolest things I’ve ever used.

As an ASP.NET MVC developer, I rely quite a bit on the ability to set a layout page (or two) and have a master page/content page relationship in my sites. I encountered however that Knockout seems to flake out a little but when you have <script> sections scattered in your content pages. The one I have in the layout page works fine, but the ones I define in the content pages did not. The content of these script tags is obviously my view models and can be defined either inline or linking to a js file. Either way, no workie.

When viewing the page source on the rendered page, you can see the <script> section defined in the layout page up on top and the one for the content page down in the middle somewhere. For testing purposes, I moved the content page’s <script> content to the layout page and guess what, it all works fine.

Obviously in the case of having more than one ViewModel, you have to use the applyBindings method with the “root element” argument.

So after a little searching and finding nothing on this problem (I guess Knockout being so new, searches still lack quite a bit – yes, even StackOverflow), something occurred to me and it fixed my problem:

I can declare the content page’s <script> section inside a @section sectionname { }. Then in my layout page I used the @RenderSection command to render that section using the name I gave it. Now the scripts can be physically encapsulated within the content page to which they belong, but render up at the top of the layout page’s <script> section – best of both worlds.

I hope this posting reaches the search engines and helps others who may have run into this.

Until next time…

Thursday, November 29, 2012 2:59:49 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] - - Follow me on Twitter
ASP | Dev Stuff
# Thursday, October 04, 2012

I just finished a session on MVVM at DevReach in Sofia, Bulgaria and the concern came up about using a method to call the PropertyChanged event with a string for the property. Obviously this does not have compile-time safety and opens the door to a lot risk. In the real world, I use the Prism framework which has a facility to extract a property name from an expression so I pulled that piece out and am showing it here for those interest.

Here’s a very simple base class that encapsulates property change notification. Notice the overloaded method for raising the event. One uses a string argument and the other an expression.

   1:      public abstract class ObjectBase : INotifyPropertyChanged
   2:      {
   3:          public event PropertyChangedEventHandler PropertyChanged;
   4:   
   5:          protected internal void OnPropertyChanged(string propertyName)
   6:          {
   7:              if (this.PropertyChanged != null)
   8:                  this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
   9:          }
  10:   
  11:          protected virtual void OnPropertyChanged<T>(Expression<Func<T>> propertyExpression)
  12:          {
  13:              var propertyName = ExtractPropertyName(propertyExpression);
  14:              OnPropertyChanged(propertyName);
  15:          }
  16:   
  17:          static string ExtractPropertyName<T>(Expression<Func<T>> propertyExpression)
  18:          {
  19:              if (propertyExpression == null)
  20:              {
  21:                  throw new ArgumentNullException("propertyExpression");
  22:              }
  23:   
  24:              var memberExpression = propertyExpression.Body as MemberExpression;
  25:              if (memberExpression == null)
  26:                  throw new Exception();
  27:   
  28:              var property = memberExpression.Member as PropertyInfo;
  29:              if (property == null)
  30:                  if (memberExpression == null)
  31:                      throw new Exception();
  32:   
  33:              var getMethod = property.GetGetMethod(true);
  34:              if (getMethod.IsStatic)
  35:                  if (memberExpression == null)
  36:                      throw new Exception();
  37:   
  38:              return memberExpression.Member.Name;
  39:          }
  40:      }

 

Using this is easy. Instead of sending in the property name as a string when calling OnPropertyChanged, you use an expression and get full compile-time safety:

   1:          public ViewModelBase CurrentChild
   2:          {
   3:              get { return _CurrentChild; }
   4:              set
   5:              {
   6:                  if (_CurrentChild != null && _CurrentChild.Equals(value))
   7:                      return;
   8:   
   9:                  _CurrentChild = value;
  10:                  OnPropertyChanged(() => CurrentChild);
  11:              }
  12:          }

Until next time…

Thursday, October 04, 2012 9:49:59 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] - - Follow me on Twitter

# Wednesday, September 19, 2012

Hi all, I’ll be speaking at Visual Studio Live this December, including an all-day pre-con on Services where I cover both WCF and Web API.

You can click below and use the promotion code VOSPK6, and please stop by and say hi personally!

VSLOR12 Speaker Incentive_Castro

CYA THERE !

Wednesday, September 19, 2012 12:12:36 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] - - Follow me on Twitter

# Monday, June 18, 2012

As part of the ACT Fly-In this year, I was recently given the opportunity to go to Washington and meet with some of our law makers to discuss several issues in the IT arena. One of them is an issue I’ve been discussing with people for a while way before this event, and that’s the issue of spectrum shortages. I had some good discussions with several congressmen, senators, and staffers and thought it’d be good to touch base on it here. For those of you that think this may not affect you, let me tell you that it already has and you’ve even made it every-day topics of discussion and argument. Our daily discussions of dropped calls, cellular dead-zones, and slow wi-fi is certain proof that the problem of bandwidth is alive and well and most certainly getting worse. As we move into the 4G era, towers are needing to be upgraded and even new ones built.  But the development of the physical infrastructure is useless without the invisible commodity that rides on it, and this is spectrum. Put simply, spectrum is a frequency range that is used by wireless communication. Also known as Wireless Spread Spectrum, it consists of the frequency range 3ghz and 300ghz with its availability is limited and its usage assigned through licensing. It’s considered a national resource, and because of that, subject to governmental administration. Such administration and management is necessary to limit what’s known as radio spectrum pollution. Since 1934, authority for spectrum management is granted to the President for any federal use and to the FCC for all domestic use. Companies with spectrum assignment are the regulators for that frequency and have been known to “sit on it” even when not in use. This lead to congress passing the Spectrum Act requiring spectrum licensees to share spectrum and participate in wireless broadband spectrum actions. As I stated earlier, though considered a national resource, it unlike water and gas because it is reusable.

The government incentive auctions have allowed for unused spectrum, much of it held by over-the-air broadcasters after the switch-to-digital, to be released and “purchased” by licensees willing to light it up. This is a win-win situation because the current licensees of unused spectrum profit from the auction proceeds, and it opens up new business opportunities for new services. These “new services” are us, the app developers. The apps we’re writing today are more connected than ever. It’s funny how for a long time, I taught developers how to write architect apps for disconnected use, but that’s becoming more and more difficult with the requirements users (and customers) are placing on “the cloud”. We’re going to see our bandwidth problems gets worse before they get better as more and more applications that require the internet in their core usage are developed. This is ever-so-evident in today (and tomorrow’s) tablets. Nearly every application uses the cloud in one way or another. Media streaming from cloud storage versus local storage will be the next big challenge. Devices are offering no on-board memory increase in favor or more and more cloud storage. Apple’s iPad is on its third iteration and its maximum capacity has remained the same since its introduction. Apple themselves say the play to open the iTunes Match service to video by the end of this year. This will result in a enormous weight on the carrier lines, making the need for more spectrum activation even more critical.

The incentive auctions should continue but not hindered by unnecessary regulation in the interest of a “level playing field”. And the FCC should readily approve spectrum deals that are in front of them, like Verizon’s bid to buy out and build out spectrum from the cable companies. It shouldn’t lay dormant. If purchasers of unused spectrum have the capacity to contribute to the marketplace, provide innovation, better coverage, and more jobs, they should be allowed to do so. Getting in the middle of this can literally bottleneck the Internet and the cell-lines, and today this is not a trivial thing. There is a way to get involved and get your opinion heard on topics that are important to you. Gone are the days where we need to run to the library to use the encyclopedia to research something. Everything we need is literally at our fingertips, but to remain at all our fingertips, the technology must be allowed to grow and progress and not be stalled by people with incomplete information. ACT offered me the unique opportunity to actually discuss this with the people that make our laws. What this taught me was that we don’t have to be a “cast the vote and walk away” people.

Time will tell if our discussions will result in success, but I for one was impressed for how I was received; even in offices for which I was not a constituent. I try to be optimistic in our system of government, despite the partisan ship that does and will always take place, and despite whether or not I agree with what certain parties are doing. My frequent travels abroad constantly reaffirm that optimism. This one topic was of particular fascination to me when I first educated myself on it, and became a constant interest as I followed the legislative actions covering it. I do feel it is one that affects everyone in the technology business and when it comes to infrastructure construction, innovation, and application development, it affects the country as a whole.

Monday, June 18, 2012 12:01:20 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] - - Follow me on Twitter

# Wednesday, February 01, 2012

Funny how 10 years ago, I was reading publications and listening to sessions by many of the people that today I have the honor and pleasure of calling my friends. It’s been a hell of a ride being active in the .NET community and I owe a lot of the beginnings of that to INETA. Traveling around the country, speaking at user groups and meeting many, many talented developers (some of whom have become friends of mine) has been one of the highlights of my community involvement. It was another good friend of mine, Bill Wolff, that introduced me to INETA and since then there have been a number of good people involved. Nancy, Scott, Rob, Chris, Trish, Lori, and many others. All of them made the organization a big priority in their lives and careers and it thrives today because of that collective effort. And it’s not just about the fun I and others have had as speakers; many user groups owe a lot to INETA for their support. I hope the organization continues to be the glue for the symbiotic relationship between those who run the groups and those who speak at them.

So a lot has evolved and changed over the years in the formula that defines the interaction between the groups and the speakers, but one thing has remained. INETA is still the primary user group support mechanism out there. I still get to travel around and speak to great developers around the country and sample what they try to pass of as pizza (they forget I’m from Jersey).

Happy anniversary INETA; may the next 10 years be even better, even though pizza outside of Jersey will probably get worse.

Until next time…

Wednesday, February 01, 2012 11:26:58 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0] - - Follow me on Twitter

Search
Me & My Flair

Read all about me here.
Download my Resume here.

Check out where I am here.
 
Click on logos above for profiles.
Archive
<April 2014>
SunMonTueWedThuFriSat
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
Statistics
Total Posts: 40
This Year: 0
This Month: 0
This Week: 0
Comments: 93