mceranski

Stories kicked by mceranski

Using Ninject with MVVM Light(www.codecapers.com)

submitted by mceranskimceranski(345) 6 months, 21 days ago

A short tutorial on how to utilize Ninject in a WPF MVVM Application. read more...

add a comment |category: |Views: 38

tags: another

Adding Export Capabilities to the Razor WebGrid(www.codecapers.com)

submitted by mceranskimceranski(345) 8 months, 23 days ago

A tutorial on how to add export to CSV functionality to the Razor WebGrid read more...

add a comment |category: |Views: 15

tags: another

Automatic Resource File Translation via Google Translate(www.codecapers.com)

submitted by mceranskimceranski(345) 1 year, 8 months ago

One of the features that I support in WeBlog is localization. That means that all the labels, buttons and text within the application are stored in a resource file. So if you switch from English to Spanish, the text will be automatically displayed in the right language. For more information about how this magic happens, read this article. Since I am only fluent in English I depend on Google translator to make the translations for me. After about 10 minutes of manually translating a spanish resource file, I decided that I needed a way to automate the process. I did a quick Google search and discovered that the translator service utilizes a JSON based API. A little more searching and I found a class library written by Alex Meyer-Gleaves which takes care of making the service calls to the translator API and parsing the results. So the only work I had to do was read the resource file, invoke the translator via Alex’s C# library and save the translated data to a new resource file. read more...

1 comment |category: |Views: 160

tags: another

Simple Dependency Injection using a Custom MVC Controller Factory(www.codecapers.com)

submitted by mceranskimceranski(345) 1 year, 8 months ago

Dependency injection is an important concept to grasp if you plan on doing unit testing. In this example, I demonstrate how a proper constructor signature coupled with a a custom controller factory can make it easy to swap out repository objects for testing purposes. http://www.codecapers.com/post/Simple-Dependency-Injection-using-a-Custom-MVC-Controller-Factory.aspx read more...

add a comment |category: |Views: 45

tags: another

Unit Testing Secure Controller Actions with Moq(www.codecapers.com)

submitted by mceranskimceranski(345) 1 year, 9 months ago

One of the hardest things to unit test in MVC is security. Security is tough to test because there is a lot of setup involved in mocking the HttpContext, the Principal and the Identity. For example, in WeBlog I am using the following code in the Edit Post action. Post post = Repository.FirstOrDefault<Post>(x => x.ID == id); if (post == null) return View("NotFound"); if (!HttpContext.User.CanEditPost(post)) return View("PermissionDenied"); In order to make sure this code works properly I need to test it with an authorized and unauthorized user. Unfortunately, the HttpContext.User will not automatically be created for your tests so you have to mock one for each test that your perform. So lets start this journey by reviewing the code required to mock the HttpContext using the popular opensource library Moq. This code is a combination of code I discovered on Stackoverflow and Scott Hanselman’s MvcMockHelpers... read more...

add a comment |category: |Views: 21

tags: another

Looking for an MVC Grid Control? Try MVC Contrib!(www.codecapers.com)

submitted by mceranskimceranski(345) 1 year, 10 months ago

Like most .NET Web Developers I was ecstatic when MVC was released. To put it plainly, I hate WebForms. However I do find myself missing some of the great WebForm controls like the DataGridView. The DataGridView was present in every WebForm application that I wrote. I really appreciated all the subtle bells and whistles that Microsoft added to the grid over the years. I wrote my own grid control for classic ASP and I know firsthand that it is a significant undertaking to make a grid control that if feature rich and flexible enough to handle complex situations. Therefore I was not crazy about taking on the task again… Initially, I adopted jqGrid as my new de facto grid control. From a end user’s perspective, jqGrid provides a top-notch user experience. Unfortunately, the control is heavily dependent on JavaScript so its not always the best solution for Mobile websites. In addition, jqGrid does require a fair amount of plumbing. Although it’s not difficult to implement it does take time and does not provide the rapid development experience that I grew accustomed to with the DataGridView control. For about the last 12 months I went old school. Yes, I have manually been coding my tables by looping over a enumerable list and creating rows and columns. Just like I did ten years ago! Its ridiculous. Luckily, I recently stumbled upon the MVC Contrib Grid. In approximately 10 minutes, I downloaded the library, added a reference to the assembly and fully implemented a grid with paging and sorting! You will not believe how easy it is. read more...

add a comment |category: |Views: 48

tags: another

How to Build a Custom View Engine with Theme Support(www.codecapers.com)

submitted by mceranskimceranski(345) 2 years ago

In case you don’t know, the developers of ASP.NET MVC went to great lengths to make their framework completely flexible. By default, when you create a new MVC project you are using the Web Forms view engine. However, you can rip out the default view engine and register your own. As a matter of fact, there are already a variety of view engines available to us: * Spark – The Spark view engine claims to be HTML friendly and uses the philosophy of less is more. I wrote an article about the Spark view engine before Louis Desjardin accepted a job with Microsoft. * NHaml - Contributed by Andrew Peters. NHaml provides an internal DSL for XHTML. * NVelocity - Adapted by Hammet and added to MvcContrib with permission. * Brail. The Brail view engine from MonoRail has been ported to ASP.NET MVC and added to MvcContrib. This view engine lives in MonoRail, but with MvcContrib, it can now be used with ASP.NET MVC Framework controllers. Ported by Bill Pierce. * XSLT - Contributed by Ivan Porto. Since the view engines listed above are open source it was easy to find code to tailor my custom view engine after. In addition, I also found a great article titled Creating Your First MVC ViewEngine by Nick Berardi. In any case, the first step in creating a view engine is defining your search locations. If you have been working with MVC for any length of time, then you know that MVC uses a series of search paths when finding a view. So if you have a view named Index in your Post controller, MVC will look first in the Views\Post folder for the Index.aspx file. If it is not found there, then the Web Forms view engine will look in the Views\Shared folder for the view. Since we are trying to implement themes we have a few more locations that we want MVC to search in. These would be \Themes\{SomeTheme}\Views\Post and \Themes\{SomeTheme}\Shared. In order to add these new search locations we need to set the MasterLocationFormats, ViewLocationFormats and PartialLocationFormats in the constructor of our custom view engine... read more...

add a comment |category: |Views: 70

tags: another

How to Localize an ASP.NET MVC Application(www.codecapers.com)

submitted by mceranskimceranski(345) 2 years ago

hile working on WeBlog this week I decided that I needed to start thinking about localization. If you have never heard the term “localization” before then its just a fancy way of saying that I want my application to be multi-lingual. In order to localize an application in .NET, you generally need to create a separate resource files for each language you want to support. In an ASP.NET MVC application, the resource files should be placed in a folder called App_GlobalResources. The folder can be created by right clicking on your project and selecting Add –> Add ASP.NET Folder –> App_GlobalResources. The resource files follow a naming convention. The first part of the name is the user defined part, for WeBlog we called it “Strings” but it could be whatever you want. The second part of the string is the Culture. For English the culture is “en”, for French the culture is “fr”, and so on and so forth... Read the full article: http://www.codecapers.com/post/How-to-Localize-an-ASPNET-MVC-Application.aspx read more...

add a comment |category: |Views: 39

tags: another

Display and Editor Templates in ASP.NET MVC 2(www.codecapers.com)

submitted by mceranskimceranski(345) 2 years ago

In the first version of ASP.NET MVC, I found myself creating a lot of HTML Helpers and Partial Views in order to promote code re-use and to standardize formatting of certain data types across views. For example, let's say that I want every date in my application to use the formatting of “MM/dd/yyyy”. In MVC 1.0, I would have made the following HTML Helper: public static string FormatDate(this HtmlHelper helper, DateTime value ) { return value.ToString( "MM/dd/yyyy" ); } Once the HTML Helper was ready, I would add the markup to my views: <%= Html.FormatDate( Model.Date ) %> Although this code is reusable it can be hard to maintain when you have a large application. It is especially painful to implement when you already have dozens of views and you need to replace the old "HTML.Encode( Model.Date )" code with the new helper method. Fortunately, there is a better way! MVC 2.0 introduced the concept of Display and Editor Templates. Read the full story at codecapers: http://www.codecapers.com/post/Display-and-Editor-Templates-in-ASPNET-MVC-2.aspx read more...

add a comment |category: |Views: 98

tags: another

Building an RSS Feed in ASP.NET MVC(www.codecapers.com)

submitted by mceranskimceranski(345) 2 years, 1 month ago

When building a website it is common to expose an RSS/ATOM feed for your content. Feeds serve two main purposes. The first, is that it allows other sites to consume your content for syndication. For example if you write .NET articles, there may be other partner sites that subscribe to your feed and dynamically pull in your content. Secondly, feeds allow users to subscribe to your site so they can get notifications when your content is updated. This is especially relevant for sites with irregular content updates such as a personal blog. In the .NET 3.5 framework there is a namespace titled System.ServiceModel.Syndication which very few people seem to know about. The classes contained in this library allows you to create and consume RSS feeds with minimal effort. I recently created a feed for my WeBlog application. I was astonished about how little time it took to implement an RSS Feed. Instead of weighing you down with all the details...I'll let the code do the talking.... read more...

add a comment |category: |Views: 246

tags: another

Dynamic Casting with .NET(www.codecapers.com)

submitted by mceranskimceranski(345) 2 years, 1 month ago

I recently was working on a project where I needed to store settings or various data types into a database table. Since most of the settings were singular I ended up with a table that had two columns which were titled name and value. The name and value field of the Setting table are both defined as varchars ( or strings). By defining the value field as a string, I can serialize any object to the table by using the ToString() method. Over time my application could grow to have dozens of settings of varying data types. So I needed a mechanism to load the settings from the table and assign them to an object. While loading each setting, I need to dynamically determine the data type of each field and dynamically cast it to the correct type. read more...

add a comment |category: |Views: 10

tags: another

First Encounters of the MEF Kind(www.codecapers.com)

submitted by mceranskimceranski(345) 2 years, 2 months ago

One of the newest additions to the .NET 4 framework is MEF. MEF stands for the Managed Extensibility Framework and is the answer to your prayers if you are building an app that allows developers to extend its functionality by building plugins. MEF makes the task of loading assemblies and utilizing their functionality very simple. Before we go any further there are a few basic terms that you must understand before you start using MEF: The Catalog is responsible for loading assemblies. There are several types of catalogs to chose from such as DirectoryCatalog and AssemblyCatalog which we will discuss in more detail later on. The Container holds one or more catalogs. The Export attribute is used to decorate objects so they can be consumed by MEF. The Import attribute lets MEF know that an object is the target for one of more exported objects. A part is any object that is exported or imported. So in order to start using MEF, you need to create one or more catalogs, add them to a container and then call a method named “ComposeParts” which will search the assemblies in the catalog for “parts” which are marked with export or import attributes. In order to help understand the process better, lets look at some code.. read more...

add a comment |category: |Views: 8

tags: another

Goodbye Http Handler, Hello FileResult(www.codecapers.com)

submitted by mceranskimceranski(345) 2 years, 2 months ago

If you have been developing applications in ASP.NET MVC then you are probably familiar with the ActionResult class. The ActionResult is the most common type of object returned from an action. When building MVC apps, most of time you will use the ActionResult class. Last week while I was working on my open source project WeBlog, I built an HTTP Handler to serve up images. I started using an HTTP Handler for images because I needed a mechanism to prevent bandwidth leeching. The only bad thing about using an HTTP handler for images is that you end up with some pretty ugly URLS. By using a FileResult object with MVC you not only get a pretty URL but you also get a lot of flexibility on where you want your images to reside. You can store images anywhere you want and the URL will never need to change! read more...

add a comment |category: |Views: 109

tags: another

Building a Star Rating System with ASP.NET MVC and jQuery(www.codecapers.com)

submitted by mceranskimceranski(345) 2 years, 2 months ago

A star rating system allows your readings to rate content based on a 0 to 5 point scale in half point increments. This tutorial steps through the process of implementing a star rating system using JQuery, AJAX and ASP.NET MVC. read more...

add a comment |category: |Views: 225

tags: another

Client Side Validation with jQuery(www.codecapers.com)

submitted by mceranskimceranski(345) 2 years, 3 months ago

Validating user input on the client definitely has its advantages. It not only avoids unnecessary round trips to the server but it can also drastically improve the user experience. When I think about client side validation I think about jQuery. If you are familiar with jQuery, then you know that there are a ton a plug-ins available. Just like an Apple IPod has “an app for that”, jQuery has a “plugin for that”. Anyway, after a quick search I discovered the jQuery Validation plugin. There are two different ways to use the Validation plugin. The first way, is to the pure JavaScript route. This means you establish all the validation rules, messages and callback events from code. The second way is to decorate your input fields with special CSS classes. The plugin will then inspect the class attribute of each field at runtime and apply validation rules accordingly. In the upcoming example, I used the CSS approach. However, if you decided that you want to go in the other direction there are some excellent demos on the validation plugin homepage. read more...

add a comment |category: |Views: 24

tags: another

Dynamic Master Pages in MVC(www.codecapers.com)

submitted by mceranskimceranski(345) 2 years, 3 months ago

I have been working on a new project where I wanted to introduce the concepts of themes. This means that end users will be allowed to change the look and feel of the web application from a configuration menu. In order to deliver this functionality I needed to be able to dynamically change the master page on the fly. Since I am using MVC, I figured that there must be an event somewhere in the controller class that I can override to dynamically assign the master page. After a little bit of reading I discovered the OnActionExecuted event... read more...

add a comment |category: |Views: 141

tags: another