Infragistics ASP.NET controls

ASP.NET MVC Basics: How to create a HtmlHelper

 ASP.NET MVC Basics: How to create a HtmlHelper Hello! And welcome to another tutorial on refactorthis.net.  This is the first tutorial in a series on ASP.NET MVC development.  This first installment will cover how to create an HtmlHelper.   What is ASP.NET MVC? I apologize if I'm stating the obvious, however, this is a tutorial on the basics so I'm approaching the tutorial with the assumption that the reader has no knowledge of ASP.NET, MVC, or ASP.NET MVC.  I'm sure you noticed that I made three references to the technology.  That's because ASP.NET MVC is more than one technology.  First we have ASP.NET, which is a server side web development technology created by Microsoft that utilizes the .NET framework.  Simply put, ASP.NET allows you to write dynamic web pages in .NET language of your choice (C#, VB.NET, etc..).  ASP.NET is an open source technology and it can be developed using the .NET framework or the Mono runtime.  You can find all sorts of wonderful information at http://www.asp.net/ . ASP.NET comes in two "flavors".  ASP.NET WebForms which is the original flavor, and ASP.NET MVC.  ASP.NET MVC is made up of two concepts.  First and foremost there is the MVC design pattern.  The MVC or Model-View-Controller design pattern was introduced by Trygve Reenskaug in the 1970's.  The design pattern has also be explained by Martin Fowler in his wonderful book on design patterns called Patterns of Enterprise Application Architecture . The idea behind the MVC pattern is that you have data that you wish to display (Model), you have a presentation layer in which you wish to display your Model (View), and you have a class that handles the interaction between the View and the Model (controller).  The MVC design pattern is an excellent example of a design with proper separation of concerns.  Applications that implement the MVC design pattern are generally loosely coupled, and easy to test (unit testing).  In ASP.NET MVC, the View is a web page, the model can be any class that holds data that you want to display in the View, and a controller class. Now that I've explained ASP.NET and MVC, I'll now explain ASP.NET MVC.  You see, not only is ASP.NET MVC an implementation of the Model-View-Controller design pattern, it's also a framework built by Microsoft to support the implementation of the MVC design pattern.  You will see as you begin working with MVC that the framework is a large part of what makes ASP.NET MVC what it is.  You could implement ASP.NET MVC without the framwork, however, I don't recommend it.  The framework will create controller's for you, create Views that are bound to a strongly typed model of your choice, and much more.  Using the ASP.NET MVC framework, you can create a basic CRUD web application in a very short amount of time. I hope that this gives you a nice overview of the ASP.NET MVC framework and design pattern. HtmlHelper: What they are and how to create them I'll explain HtmlHelpers by taking you through implementing one..  We'll start by firing up Visual Studio and create a new ASP.NET MVC 4 project (note: I'm using VS 2012, however, if you have an earlier version installed, simply choose whichever ASP.NET MVC version that you have available to you.  If you don't have any ASP.NET MVC templates, you can use the Web platform installer to install ASP.NET MVC on your system.       The next dialog allows you to choose which type of ASP.NET MVC application that you'd like to create.  We will pick an Internet Application, with the Razor View engine and a unit testing project as shown below.     Now click OK and your project will be created for you.  The ASP.NET MVC Framework will create a lot of boiler plate code behind the scenes.  This includes "Forms based Authentication" in the web.config file as well as an AccountController which is a controller to handle authentication to the site.  There is also a default controller called HomeController. We are going to create an html helper that creates an HTML table.  This is merely an example and is not particularly useful, however, it will show you how to create and use an HTML Helper.  An HTML helper is nothing more than an extension method which returns a string of HTML.  Here is the official definition of an extension method from MSDN: Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type. Creating the HtmlHelper  Create a folder called "Helpers" in your solution.  In the folder, create a static class called TableExtensions.cs .  The class will look like this: TableExtensions.cs using System; using System.Text; using System.Web.Mvc; namespace HtmlHelpers.Helpers { public static class TableExtensions { public static MvcHtmlString Table(this HtmlHelper helper, string id, string name, int rows, int columns) { if (string.IsNullOrEmpty(id)) throw new ArgumentNullException("id"); if(string.IsNullOrEmpty("name")) throw new ArgumentNullException("name"); if (rows <= 0) throw new IndexOutOfRangeException(); if (columns <= 0) throw new IndexOutOfRangeException(); StringBuilder tableBuilder = new StringBuilder(); tableBuilder.Append(string.Format("<table id=\"{0}\" name=\"{1}\">", id, name)); for (int rowCounter = 0; rowCounter < rows; rowCounter++) { tableBuilder.Append("<tr>"); for (int columnCounter = 0; columnCounter < columns; columnCounter++) { tableBuilder.Append("<td>"); tableBuilder.Append(rowCounter.ToString()); tableBuilder.Append("</td>"); } tableBuilder.Append("</tr>"); } tableBuilder.Append("</table>"); return new MvcHtmlString(tableBuilder.ToString()); } } } As you can see, we've created a static class and a static method. The the first method parameter is  this HtmlHelper helperThis is where the magic happens. This allows us to use the method from the View like all other HTML helpers.The other methods specify the number of rows and columns that should be built in the HTML table.Here is an example of how to use the helper: @using HtmlHelpers.Helpers; @{ ViewBag.Title = "Index"; } <h2>Index</h2> @Html.Table("myTable", "myTableName", 4, 4)The output looks like this: And there you have it folks.  I hope you enjoyed this tutorial.  If you have any questions please don't hesitate to leave a comment. Thanks for reading.


Xamarin 2.0 Product Review - Android and iOS development in C# just got easier

Xamarin 2.0 : The prayers of mobile developers have been answered   Hello, and welcome to my latest article on refactorthis.net.  This article is a product review of the latest release by Xamarin called Xamarin 2.0. The state of mobile development We are experiencing a revolution unlike any in the history of software development.  Thanks to mobile devices, most of the world's population holds more computing power in their pocket than I could have dreamed about when I first started writing programs back in the days before the internet.  I remember when we dialed into BBS or Bulletin Board Systems which were communities that ran on your neighbor's computer and allowed us to send email and play text based door games to pass the time.  My how times have changed.  And with this change brings a new paradigm for software developers to learn in order to keep up with the now hyper speed pace of devices and technology.  We now live in a world where our TV's have IP addresses, and our children have cell phones.  At the forefront of this new way of software development is Xamarin.  On 02/20/2013 Xamarin announced the release of Xamarin 2.0.  The Xamarin 2.0 product release includes many ground breaking changes to Xamarin's mobile development tools for Android and iOS development with C#.  The Xamarin 2.0 release includes: A new IDE called Xamarin Studio which is an IDE aimed specifically at mobile development using C#. A new component store with a catalog of free and paid libraries to help developers create better mobile applications faster. A new pricing tier which includes a free starter edition for developers starting out with mobile development. Visual Studio integration that makes developing iOS applications in Visual Studio possible. I've been extremely excited about the release of Xamarin 2.0.  This article is a product review of the new features of Xamarin 2.0 and my experience with the new features. Xamarin.iOS for Visual Studio I'm a .NET developer and for the past 10 years, Microsoft related technologies have put food on my table.  C# is one of the most popular programming languages around today.  Xamarin has lead the way for C# developers interested in developing mobile applications using C#.  The release of Xamarin 2.0 has provided what many thought to be impossible.  iOS development from within Visual Studio.  Here is my experience with Xamarin.iOS and Visual Studio. One thing to understand is that even though Xamarin.iOS allows you to develop iOS applications from within Microsoft Visual Studio, you still need a machine running Mac OS X, the iPhone SDK, and XCode to allow this integration to work. Due to Apple's licensing, you can't build iOS applications on anything other than an Apple device.  The Xamarin 2.0 release includes Xamarin.iOS which allows us to write our code in C# using Visual Studio, then build or deploy the application using a build server which is a system running Mac OS X and XCode. Xamarin.iOS will search the network for a build server that meets the requirements to build iOS applications.  Visual Studio will prompt the user with a list of all machines on the network that meet the build server requirements and allow you to choose which build server to use. Since I don't own a Mac, I asked my brother-in-law if we could use his OS X machine to test the integration. We ran into an issue at first in which the Xamarin.iOS host prompt helped me to diagnose.  It turned out that we needed to change the firewall settings to open port 5000 to allow the two machines to communicate to build the application. After my initial testing at my brother-in-law's place, I decided to continue my research from home.   I wanted to see if there were any other alternatives to buying a Mac in order to be able to develop iOS applications.  I ran across a service called MacInCloud which allows you to rent a Mac development server in the cloud!  They offer several options regarding payment plans.  They even offer the ability to pay for use by the hour.  Xamarin studio is found in the list of supported software on their features page. This is a great option for developers like me who wish to develop iOS applications but aren't ready to run out and buy a Mac just yet. My final thoughts on the Visual Studio Xamarin.iOS development integration are as follows. Is the process 100% transparent?  No.  Though I don't believe it will ever be possible to make a completely transparent experience.  That would require running Visual Studio on OS X, or Apple removing their restrictions regarding iOS development.  EDIT It's been brought to my attention that since you will need to run a Mac system anyway, some people find it useful to run Visual Studio inside of a Virtual machine on the Mac.  This will make the integration even better.  You may also run the two machines side by side and use a program like VNC Server/Viewer to switch between the two environments. As I've stated earlier, you can also run the Mac build server in a virtual machine by using a service such as Macinthecloud.  If you find a useful combination that isn't mentioned here, feel free to share in the comments! Is this the best option that .NET developers have regarding developing iOS applications using Visual Studio on in Microsoft Windows?  Yes.  If you are a .NET developer and you are interested in developing applications for iOS, download Xamarin 2.0 and give it a try.  Your mileage may vary. Xamarin Studio — Its like MonoDevelop on steroids One of the greatest parts of the Xamarin 2.0 release has got to be the addition of their new IDE, Xamarin Studio.  Xamarin Studio is a multi-platform IDE that is aimed specifically at mobile development using .NET.  The IDE is based on the older MonoDevelop IDE, however, there are new features that make this a completely different product. MonoDevelop had a look and feel about it that suggested that it was aimed at Linux developers.  The new Xamarin Studio has moved the focus away from Linux developers and is geared more toward the Mac OS X crowd.  The motivation behind the new Xamarin Studio IDE was very much geared toward User Experience and productivity.     The IDE looks sleek and has a very minimalist design.  The IDE exposes its features based on the context of the current developers actions.  For instance, you don't see debugging buttons unless you are in debug mode.  You don't have useless windows hanging around when they are aren't usable in the current context.  IDE development has been geared toward developers so they've often tried to add more features at the price of sacrificing user experience.  Xamarin understands that developers are users too and that providing a perfect blend of UX and functionality is the best approach to use when designing an IDE.  I think that this was a brilliant move on Xamarin's part and I wouldn't be surprised to see some of the other well known IDEs following this design in future releases.     The IDE's search bar is another great addition to the Xamarin Studio IDE.  The search bar can be found on the start screen as well as while developing an application.  The search offers an auto complete type of functionality which provides results for everything from key shortcuts to code specific namespaces and classes.   The search bar provides context specific search results based on your current location within the IDE. The IDE offers other great tools such as: A regex builder An XML designer Integrated Source Control Unit testing capabilities When ran from an OS X machine, Xamarin studio offers iOS specific project templates with the ability to create, build, and deploy iOS applications. Xamarin 2.0 also offers project templates for creating Android applications.  One of the great tools offered by the Xamarin Studio is an Android user interface designer that allows the developer to drag and drop Android user interface widgets to allow the developer to visually create the UI for the Android application.  The UI designer in Xamarin Studio is more intuitive and feature rich than the original designer included in the eclipse IDE!  You also get the same Android UI designer from within Visual Studio when using MonoDroid for Android application development.     Xamarin Studio was built with the user in mind.  The IDE is built to increase productivity by providing world class user experience.  I'm very happy with the Xamarin Studio and I believe that I will probably use it more than Visual Studio when developing mobile applications. Component Store The component store is a wonderful resource for developers using Xamarin to develop Android and iOS applications using C#.  The component store ties directly into the Xamarin Studio IDE and also has a web interface for obtaining components that will greatly increase your productivity in developing professional looking mobile applications.  The Component store offers controls, frameworks, themes, web services, and other components that will make your life as an Android or iOS developer much easier.  They offer free and paid components to assist in your mobile development efforts.  You can even create your own libraries and submit them to the component store for review to be included in the list of components.  I believe Xamarin's component store will be a great resource for anyone developing Android and iOS applications using Xamarin's development tools.       Starter Edition In the past, Xamarin offered a free trial for developers to try out their product.  The free trial simply limited the functionality of the product.  One of the biggest limitations in my opinion was the inability to deploy to a physical device to test your applications.  When I first began testing Xamarin for Android development, this limitation was a show stopper for me.  I have had no luck with the Android emulator and this seems to be a problem with many Android developers.  The emulator is slow and unstable.  So when I heard that Xamarin had changed their licensing and now offers a free starter edition, I was thrilled.  The starter edition is free and allows testing an Android application on the developer's Android device.  You can even deploy the application to the Android App stores.  There are limitations on Android applications that are created under the Starter edition.  They can’t call to third party native libraries (i.e., P/Invoke) and they are capped in size at a max of 32k of IL code.  However, we as developers at least have the option to deploy to our devices and test the applications so we can make an informed decision regarding upgrading to their new Independent developer license which at $299 is the cheapest license that Xamarin has offered to date!  So if you are like me and you've put off trying Xamarin because of the restrictions that bound you to the horrible Android emulator, there should be nothing stopping you from giving the new Xamarin 2.0 release a try.   Conclusion This concludes my review on Xamarin 2.0.  I'd like to say that I'm very excited about the possibilities that these enhancements provide for .NET developers that wish to develop applications for iOS and Android using C# and .NET. The free starter edition provides a solution to my biggest issue with the Xamarin tool set, and that's the ability to test your applications built with MonoDroid on your device instead of the horrible emulator. The Xamarin.iOS integration with Microsoft Visual Studio is the best option to allow developers to develop iOS applications while using their favorite IDE to do it. The Xamarin Studio IDE is a beautiful, feature rich product that will change the way that we look at what an IDE should be. If you are a .NET developer and you are looking into mobile development, now is the time to check out Xamarin 2.0.  I'm anxious to see what the next release will bring.  Oh and don't forget about the Xamarin Evolve 2013 world wide developer conference in Austin, Texas from April 14-17 2013.  I hope to see you there! Thanks for reading! Buddy James


Mono 3.0.4 is out! Features include Improved garbage collection, Async bug fix, and Xamarin studio support.

Mono 3.0.4 released Greetings to all of you open source patrons out there! I've just received news of the latest release of Mono (3.0.4). The new release includes several major improvements and bug fixes. In this article, I'd like to provide a brief overview highlighting the major changes in the latest release of Mono. So without further ado, here is a quick overview of what's offered in this version of the Mono project. Improved garbage collection The GC implementation has been given a makeover. These changes include: A new approach called "cementing" has been added to the SGen concurrent garbage collector. Mono allocates all new small objects in a defined memory space referred to as the nursery. When a collection occurs, the surviving objects become root objects and are copied to the major heap. Typically, few references that are allocated to the nursery survive to become roots, so the majority of the objects are instantly collected which leaves plenty of allocation space for new objects. These nursery collections minimize the work that must be done by the collector. One of the problems with the garbage collection in previous versions of mono involved instances in which objects are "pinned" in the nursery (due to managed/unmanaged references or other operations). Objects that are "pinned" cannot be moved to the major heap. Typically the collector must keep track of these "pinned" objects (and their relationships) and it rescans them on each collection attempt to try to see if they have been released and are able to be moved. This approach was an inefficient practice of the collector. This is where cementing comes in to play. Cementing is a process by which references in the nursery that are pinned are simply marked as root objects, but they remain in the nursery since they can't be moved to the heap. This dramatically reduces overhead related to pinned nursery objects and their relationships. There are also several bug fixes related to garbage collection including #9928 pointer free deadlock problem and bugs in mono_gc_weak_link_get Improved StreamReader/StreamWritter asynchronous operations The asynchronous operations have been rewritten to resolve bug #9761. Which caused the operations to fail on subsequent calls. OSX Homebrew installation conflict resolution Mono no longer installs a /usr/bin/pkg-config file on OSX, which resolves an issue that effected Homebrew installations. The installation only contains the new Gtk+ stack that allows the new Xamarin Studio to run on OSX with 3.0. This is exciting news! Conclusion (for now) Well that about wraps it up.  Oh, one more thing..   In case you haven't heard, Xamarin has released Xamarian 2.0 which includes iOS development from within Visual Studio, a brand new IDE called Xamarin studio that is geared toward developing mobile apps for Android, and iOS. The IDE runs on Windows, Linux and OSX! I would like to mention that I will be delivering a detailed refactorthis.net product review on the new and exciting features of Xamarin 2.0. So check back for my review and thanks for reading! Buddy kick it on DotNetKicks.com  


dot42 Android development with C#. All the best parts with less restrictions!

Check out dot42 and Xamarin 2.0 forr developing Android applications using the .NET framework.
Check out dot42 and Xamarin 2.0 forr developing Android applications using the .NET framework. [More]


About the author

My name is Buddy James.  I'm a Microsoft Certified Solutions Developer from the Nashville, TN area.  I'm a Software Engineer, an author, a blogger (http://www.refactorthis.net), a mentor, a thought leader, a technologist, a data scientist, and a husband.  I enjoy working with design patterns, data mining, c#, WPF, Silverlight, WinRT, XAML, ASP.NET, python, CouchDB, RavenDB, Hadoop, Android(MonoDroid), iOS (MonoTouch), and Machine Learning. I love technology and I love to develop software, collect data, analyze the data, and learn from the data.  When I'm not coding,  I'm determined to make a difference in the world by using data and machine learning techniques. (follow me at @budbjames).  

Related links

Month List