.NET Development Examined

Encrypt C# Object

Wednesday, December 30, 2015

One of the things I've been doing a lot of lately is taking a C# class and encrypting it into a string suitable for storage as a cookie. Typically the process has been to write a helper method to encrypt and another to decrypt for each class object. The process is fairly simple, build a string representation of the class then encrypt it. It looks something like this:

public static byte[] PrivateKey = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
public static string EncryptCustomer(Customer customer)
	if (customer == null){ return string.Empty;	}
	var customerString = new StringBuilder();
	customerString.AppendFormat("|FirstName:{0}", customer.FirstName);
	customerString.AppendFormat("|LastName:{0}", customer.LastName);
	return SecurityHelper.EncryptString(customerString.ToString(), PrivateKey);

To get the encrypted string back to a class we first decrypted the string, then parsed the array. That typically looked like this:

Read More

Comments (0)

Paged Sorted Filtered Knockout Grid

Tuesday, December 1, 2015

It's been crazy busy at work, and one of the things that has made life harder was the choice to use DataTables and Knockout. They really don't play well with each other. It seems that DataTables makes it's own array of items based on the HTML elements when it is instantiated. If the underlying HTML is updated by Knockout the already instantiated DataTables array won't reflect the changes. This makes it very difficult to updated the underlying data and have the DataTables UI reflect the changes.

This drove me to work up a custom Knockout extension for a Paged, Sorted, Filtered Grid. This is a fully Knockout aware solution that allows you to update the underlying Knockout Observable Array and have the changes reflected in the UI presentation. Just what data binding is suposed to be :)

I've thrown up a working example at http://psfgrid.azurewebsites.net/ that for now will serve as it's home. Eventually I'll move it to GitHub to open up full sharing.

Read More

Comments (0)

A Toast to JavaScript

Tuesday, September 1, 2015

Wow, it's been a while since I've had time to follow up on the last post. This year has truly flown by. We've got lot's to cover building our JavaScript Framework, so let's get going on the next step.

In Building-JavaScript-Framework we began our JavaScript Framework by defining our module, added a couple of variables and a method to generate a GUID. That's a great start, but we want our framework to do much more.

A common requirement of all applications is to convey a message to the user. This could be a warning that the something went wrong, or confirmation that an action was successful. It could also be just passing information along to the user. Additionally we may need to display more than one message to the user simultaneously. In this article we will look at creating a 'toast' type notification system that meets these requirements.

Read More

Comments (0)

Building a JavaScript Framework

Friday, February 6, 2015

In today's web development ecosystem it is more and more common to perform tasks on the client (browser). For better or worse the language of choice is JavaScript. It is essential that web developers have at least a basic understanding of JavaScript and there are many beginning tutorials available to help you get started.

There are also many frameworks available designed to either "help" you with JavaScript (like jQuery) or to provide various features to the client (think Angular, Knockout, even BootStrap has a lot of JavaScript). These are all great frameworks and are well worth getting to know. But even the best thought out framework will still fall short of filling all the developers needs. For this reason you will find that you will need a framework of your own to perform functions that are unique to your environment.

Tasks like making AJAX service calls, displaying system message, prompting users for information, even common values like empty GUID and base URL paths can be worked into your own JavaScript framework. So let's get started making a custom application framework that you can re-use on all your projects.

Read More

Comments (0)

Deploying a WCF Service

Friday, December 12, 2014

In our last three part series (Part One, Part Two, Part Three) we learned how to create a WCF service, using the COODE Framework to simplify the setup and configuration that is normally associated with a WCF service. We also saw how the CODE Framework makes it easy to stand up our service endpoints during development.

This great for development, but at some point we will need to deploy our services to productions. Part of the flexibility of WCF services is the various methods it can be hosted. You can deploy your WCF service as a Windows Service, or it can be deployed to IIS, or it can be Self-Hosted (inside your application). If you are using the CODE Framework there is a final option, you can expose your WCF service as a Web API layer.

When deploying a SOA based application (as created in these exercises) you have three distinct parts to deploy: SQL Database, Services, and UI. The process is for the first two is the same, but the UI deployment varies based on the type of UI (i.e. WPF Application, Website, IOS, Android). This is one of the benefits to the SOA architecture, and specifically the CODE Framework based service endpoints, in that multiple types of UIs can utilize a common set of service endpoints

When considering your SOA architecture each part of the deployment can be made to a separate machine (i.e. SQL, Services, and Website) or they can be deployed to the same machine.

Read More

Comments (0)

Creating a Service Layer Part III

Saturday, October 18, 2014

In Part One of our WCF services series we looked at how to create the contracts and implementation of our WCF/CODE Framework service. In Part Two we will look at how to instantiate our service end points during development using a CODE Framework Development Host.

In this post we will look at how to access an SQL database using Entity Framework so that our service layer can actually do something of interest.

Read More

Comments (0)

Creating a Service Layer Part II

Friday, October 10, 2014

In Part One of our WCF services series we looked at how to create the contracts and implementation of our WCF/CODE Framework service. In this part we will look at how to instantiate our service end points during development using a CODE Framework Development Host.

Read More

Comments (0)

Creating a Service Layer

Friday, October 3, 2014

WCF services are an excellent way to expose information to applications. They provide service endpoints that can be called synchronously or asynchronously from a variety of client applications, ranging from web to iOS based. WCF Services can be hosted by IIS, as a Windows Service, or from a standalone application. This loosely coupled relationship allows for reuse of data and business logic from multiple applications.

The CODE Framework Service offering assists in the creation WCF Services, in many cases removing the burden of complex configuration. Additionally the CODE Framework has features that assist programmers during the development process.

The home for the CODE Framework is http://codeframework.codeplex.com/ and multiple documents, articles, and quick tips can be found there.

Services by their nature are responsible for returning information required by the UI. Because they are a supporting service it is common that the UI is developed first. This allows the developer to better define the methods required. However there is no technical reason that requires the UI to exist before services are created. This lab will build on the solution created in the Lab “Creating a WPF Application”, but it can be performed as a standalone exercise.

In this exercise you will create two CODE Framework projects, one to define the Service Contracts, and one to implement the service.

Read More

Comments (0)

CODE Framework AsyncWorker

Tuesday, November 20, 2012

The CODE Framework is an extensive set of utilities that can make your life easier. The tool I want to talk about today is the AsyncWorker. This little gem can be found in the CODE.Framework.Wpf.Mvvm name space and provides a convenient way to execute code on a background thread without having to recall the syntax required to go through the application dispatcher to invoke and control a background thread.Read More

Comments (0)

WPF CollectionViewSource

Saturday, August 11, 2012

While working on a recent WPF project I came across a situation where I need to filter a collection based on user input. Additionally this project used the MVVM methodology, so the desire was to have as much as possible in the view model, not in the XAML or in the code behind. When faced with this situation normally you would just take the filtering criteria and apply a LINQ query on the collection to return a new list to base your display on. However in this case the collection was an Observable one and I needed to edit the filtered result and maintain the notify change event. These requirements ruled out the new list technique, and lead me to discover the CollectionViewSource. I asked around the office, and not many people had seen its use in a view model setting, so I thought I would do a quick post to document it.

So fire up Visual Studio and start a new WPF project and let’s get started.

Read More

Comments (1)

Improved MVC Date Template

Friday, March 16, 2012

This will be a short and simple post dealing with an improvement on the traditional display and editor templates normally found for MVC date display. If you are not familiar with the issue, normally a DateTime field will display the full information, including the time. Commonly this is not the desired display, we only want the date part. While you can format the display on each view, that becomes cumbersome, and it is easy to miss one. MVC has a great solution for that, called DisplayTemplates. Located in the \Views\Shared\DisplayTemplates folder they will apply to that data type site wide. So creating a DateTime.cshtml file here will be used for the display anytime the @Html.DisplayFor helper is used (the same is applied for the @Html.EditorFor and the EditorTemplates folder).

Commonly the contents of the DateTime.cshtml file is simple:

@model System.DateTime @string.Format("{0:d}", Model)

This partial view simply formats the DateTime values using the ShortDate pattern. Simply create the file, and you are ready to do… But what if the field is initialized, but has no value? I recently had a case where the field was not null, but contained the default date of 01/01/0001. Obviously this was not the desired display. Continue on to find out the simply solution.

Read More

Comments (0)

MVC and DataTables Part III

Monday, December 19, 2011

Last time we looked at getting DataTables to work with AJAX to retrieve just the records we are interested in viewing. This allowed us to minimize the traffic sent across the wire to our client. It also increased the responsiveness of our UI. But data is seldom static, so we need a way to create/update/ delete our information. All together now, say ‘aw CRUD!'

If you look at the DataTable documentation is has a sample (not MVC) showing how to use jEditable for in place updating. This technique essentially allows you to click on any “cell” and edit it. I wanted to go a different route and have a “pop-up” editing form displayed. To accomplish this I utilized the jQuery Form plug-in. This plug-in allows us to define an edit and delete form which is initially hidden, and only displayed when we are performing a create/update/delete action. There will be a fair amount of code when we are done, so this may take a couple of posts. The first thing to do is fire up Visual Studio and get coding!

(If you can't wait until the end you can download the code here)

Read More

Comments (0)

MVC and DataTables Part II

Monday, November 28, 2011

In the first posting (MVC and DataTables) in this series we got a simple DataTables page up and running in MVC. But we are not done yet!

It’s worth noting that there are several options you can pass to the DataTables constructor. If you want more than just the Previous/Next pagination you can pass “sPaginationType”: “full_numbers”. If you want the layout to follow your jQuery UI theme pass “bJWueryUI”: true. See http://www.datatables.net/usage/options for more details on the options DataTables supports.

If you have a limited number of records you are dealing with this simple process works ok, however if you have a large number of records performance starts to degrade, and you will get a "Flash of Uninitialized JavaScript UI," or FOUJUI as my friend Todd Anglin would call it. Basically you will momentarily see the HTML table in its raw format before the JavaScript kicks in and applies its layout. Try changing the number of records being returned in the controller to a higher value to see this effect.

Read More

Comments (0)

MVC and DataTables

Sunday, November 20, 2011

One of the first things that Web Forms developers notice when starting to use MVC is the Model View Controller paradigm. The second thing (followed by a high shrill screech) is the absence of Controls. Followed by the incredulous question ‘what do you mean there is no GridView?’ I have to admit when I first started looking at MVC this bothered me a bit as well. Using a @foreach loop to build an HTML table gave me flashbacks to Classic ASP development. Eventually I accepted it as just another way that MVC departed from the Web Form way of development.

As I continue to explore MVC and its capabilities I am constantly impressed with how flexible MVC really is. In my exploration of the MVC ethos I have come across a couple gems, the jQuery DataTables plug-in and T4 Templates. I wondered why someone hadn’t created a T4 template using a jQuery table, and further thought that a well implemented T4 Template using DataTables might be a good rebuttal to the no GridView ‘problem’.

Covering this will take several posts, in the first one we will get DataTables hooked in our view. Then we will explore some T4 templating to easily implement our custom view.

So if you are interested read on…

Read More

Comments (1)

MVC and jQueryMobile

Friday, August 26, 2011

Having a mobile version of your site is quickly become a requirement, not an optional request. Being able write a project once and display your information on multiple platforms is the quest of all developers these days. There are a lot of frameworks and development platforms that promise the developer they can do just that. Some work, some don’t, but in my mind the most promising is HTML5 applications.

After finishing up a nice new MVC site I want to look at a mobile version. I came across a new (in beta) framework called jQueryMobile. One of the attractions of this framework is that they utilize HTML5 markup, and have a pretty impressive support matrix.

So armed with my MVC project I downloaded jQueryMobile and began my quest for a cross mobile site.

Read More

Comments (1)

jQuery Autocomplete with MVC Part 1

Monday, April 4, 2011

 jQuery is a fantastic way to interject dynamic content and actions into your web pages. MVC is Microsoft’s cutting edge data driven web technology. More and more we are seeing the two technologies 'married' together to give the user a very interactive, high scalable experience. Recently I had an inventory system that I wanted to allow the customer to input the product id, or partial product name and quickly navigate to the details of that product. In another area I wanted to allow the population of a form based on previous data entry, so again a lookup of data was desired. The autocomplete feature of jQuery proved to be the tool for both of these tasks.

I found a couple of post by Mr. James that gave me a great head start.

For this exercise I created a simple 3 page MVC project that used the Northwind Customers table. I also elected to use the EntityFramework to manage the connection to the database.

Read More

Comments (0)

Everything is Sharper

Thursday, March 10, 2011

Everything is sharper now that Texas Jetter is using MVC3 Razor script! I started development on Texas Jetter late last year, and before I could publish it, Microsoft introduced MVC3 and it's Razor scripting. I decided to go ahead and post what I had, and began the process of converting (and a little refactoring, since you can't do something the same way twice). Things have been a little busy, but finally I was at the point where I could post the Razor version of Texas Jetter.  The conversion was not bad, but I did find a couple of items that gave me,,, um topics to research?Read More

Comments (0)

Exporting to a Formatted Spreadsheet

Monday, February 7, 2011

Recently I had the need to export a grid from my ASP.NET page to an Excel spreadsheet. While searching for solutions I came across a post by Erika Ehrli titled How to Export Data to Excel from an ASP.NET Application + Avoid the File Format Differ Prompt. This article is well written and as the title suggests talks about the pesky prompt you receive when exporting an ASP.NET view to a spreadsheet. It also outlines 4 different approaches to exporting to Excel, I highly recommend you check it out.

For my purpose I settled on generating the spreadsheet from a GridView. This is a common technique and very simple to implement. However I wanted a little more control over the formatting of the resultant spreadsheet, and this was a limitation Erika listed. So with a little more research here is how I managed to exercise formatting control of the spreadsheet while still using simple ASP.NET techniques.

Read More

Comments (0)

The Beginning

Thursday, January 27, 2011

Congratulations to me! It’s my birthday and this is the first post to my blog. While it may not seem like much, this is my first public facing MVC based website. Hopefully in the months to come I will have time to add some real topics of interest. Most likely the first one will be an example on how to export an ASP.NET Datagrid to Excel. This may not sound like a topic worthy of a post, but I will show you a secrete on how to control the formatting of the Excel spreadsheet so that it more closely matches that of your website. Read More

Comments (1)