Monday, June 22, 2015

How to add a password to Web.config and encrypt it

I recently had the need to store a password in the web.config file in an ASP.NET web app and I wanted to encrypt it.  I did not want to encrypt the entire appSettings section of the Web.config, so here's a solution I came up with, answering my own question on Stack Overflow, community wiki style!

http://stackoverflow.com/questions/30983920/how-to-add-a-password-to-web-config-and-encrypt-it

Need to Mock a DbSet for testing Entity Framework?

I wanted to create fast unit tests that did not rely on the database, nor data in the DB.  I used the Moq framework to create mock objects, but the Entity Framework DbSet was not easy to mock, so I created my own class.  The gist is on github.  View the readme in the gist for more details.  (One of the hardest things is to workaround the DbFunctions which only work for LINQ to Entities).

Thursday, May 21, 2015

Implementing a Base Class with Entity Framework

I want to add a bunch of audit fields to all of my tables.  Here's how I did it with a base class.  Note that the base class must have a key column. The subclass does not have an ID explicitly; it inherits the ID column from the base class.

The call to MapInheritedProperties will put the inherited properties directly on the subclass table. 



Nhibernate performance improvements

I recently took a process that was running for hours and would never finish and made it run in less than an hour.  It was also using one big transaction which was locking other people out of the database.  So here are the big 3 performance improvements I used.

Batching

I basically used some code from the NH documentation to implement SQL execution batching.  I also batched the transactions which was very easy to do.  You just do:

session.Transaction.Commit();
session.BeginTransaction();

This was a huge performance boost.

Native SQL

There was some code that read a list of entities from the DB into a session and then updated the same field to the same value in a loop.  Instead I used "session.CreateQuery" to run a single update.  Then the code did not have to do the select, nor load all the entities into memory.

Pre-Fetching

Running selects in a loop can be disastrous to performance.  So where possible I pre-fetched data that I needed before the loop and put it into a Dictionary.  (Or Map, Hash Table, whatever you want to call it!)

Monday, March 2, 2015

A Little Research on Ionic and AngularJs 2.0

First, AngularJS 2.0 is not coming out for a while. There is currently no release date. You can look at their status/progress on Google drive:


Right now it seems about half way done?  It's a crude estimate.  There is some talk that it will be done "end of 2015". 

Also, Ionic Framework. Wow it looks great, but it does not solve ALL your problems. You can use it to create beautiful HYBRID apps, but not really for mobile web: 


So if you use it, you still may need to support mobile web depending on your project requirements. 

Now what about Ionic with AngularJS 2.0?  It may take even longer for that. I read that Ionic uses some Angular APIs pretty heavily, and they are all changing. 

Monday, January 5, 2015

jQuery POST to ASP.NET MVC Controller

I recently created some code to us jQuery to post to a MVC Controller.

Here's the code.



I was a little surprised at the different return values from the C#.

In the case of a success, I am returning a JSON object to the client, which is being processed in the done callback of the jquery promise.

When there is an error, there is a response code with a message sent back to the client which is handled by the fail callback.

From my reading and searches on Stack Overflow, these appear to be the best practices as of this writing for return values from a HttpPost Action.

Note that I am not posting a form directly.  If you do that, your HttpPost service should also include a redirect like this:

    // http://stackoverflow.com/a/9435432/1804678
    return RedirectToAction("Index", "Work");