C#

C# code snippets and ideas

Unit Test for EF LINQ queries using Mocked DbSet

Goal: Create a unit test for a EF repository query. As we all know unit tests are infrastructure agnostic and therefore have no kowledge of for example external services, databases, etc. So how can we create a unit test for a repository query without hitting the DB? Mocked DbSet to the rescue:Solution:Create an extension method on collections that can be used to mock the data/collections returned by EF instead of hitting the DB.//A List of allocations for a gas system that we want to return when ......

Castle Windsor Dependency Injection with MVC4: No component for supporting the service

Problem:Installed MVC4 on my local and ran a MVC3 app and got an error where Castle Windsor was unable to resolve any controllers' constructor injections. It failed with "No component for supporting the service....".As soon as I uninstall MVC4 beta, the problem vanishes like magic?!I also tried to upgrade to NHibernate 3 and Castle and Castle Windsor to version 3 (from version 2), but since I use Rhino Commons, that is not possible as the Rhino Commons project looks like is no longer supported and ......

.Net Rounding issue (MidpointRounding)

Issue: .Net rounding does not work as you would expect. -2.5 rounds to -2 and not -3. What?! Yes! By default .Net uses MidpointRounding.ToEven which will round it to -2. BUT if you round off -3.5 it works as you would expect to -4. If you round off -6.5 you think it would also round off to -7, but you would be wrong, -6! What about 2.5, the default rounds off the 2 and not 3 as you would expect. So what is going on? You can read more about it at http://msdn.microsoft.com/e... ......

.Net - find business days

Goal: Find the business days; i.e. exclude weekends How to: Here is some code to get the first business day after or before the weekend. It is surprisingly simple but thought the code may help save folks some time. Just a note that there is no built-in .Net feature to give you public US or other holiday calendar dates, you need to build your own using a table to keep track of public holidays in most situations. Simple enough to do, extend the methods below to query a database or xml or JSON, etc, ......

System.Web.Script.Serialization.JavaScriptSerializer - prevent serializing a property

Issue: Use the .Net JavaScriptSerializer to serialize an object but due to circular references need to prevent from those properties from being serialized. Solution: In order to ignore certain properties from getting serialized, simply add the ScriptIgnoreAttribute attribute to the property: [ScriptIgnore] public virtual MyClassCausingCircularRefError MyProperty { get { return _privateField; } } ......

A potentially dangerous Request value was detected from the client

Issue: Due to an upgrade from .Net 2, 3.0 or 3.5 to .Net 4.0 or you are starting a new .Net 4.0 project, you may run into this message: A potentially dangerous Request.Path value was detected from the client Or A potentially dangerous Request.Form value was detected from the client Environment: .Net 4.0 MVC 3 (could be MVC 2 also because of the controller/action action filters that are available) Fix: Lets tackle the first issue: A potentially dangerous Request.Path value was detected from the client ......

Combine and compress javascript and css files in ASP.Net MVC

Goal: When loading js or css files, combine all the js files into one and all css files into one file respectively when rendering to improve on performance. Also compress if need be on the fly. In this example we use many css files and even more js files to organize the ASP.Net Mvc web app into manageable pieces. The reason for the separation is mainly because it gives the team the ability to work on different part of the web app by working on the affected css or js files. It also helps to decide ......

Restructuring a large ASP.Net Mvc app

So the issue is, you have a very large ASP.Net MVC app with many controllers and actions and don't know (or do) on how to restructure it for better maintenance. This article contains some thoughts around what my experiences were with building a large scale app with 10K+ lines of jQuery and 266 Actions dispersed over 98 Controllers. In the beginning: Yes God created .... no I created a relatively small scale Mvc app based on a very limited set of requirements, more like a XP or Agile experience or ......

jQuery AutoComplete in ASP.NET MVC Framework

Goal: jQuery AutoComplete in ASP.NET MVC Framework with callback to customize result Platform/Environment: Asp.Net Mvc version 1 jQuery 1.3.2 Autocomplete - jQuery plugin 1.0.2 Quick Solution: Here I am simply returning a list of account names: $('#Name').autocomplete( $('#ajaxListMatchingAccount... { delay: 10, minChars: 3, matchSubset: 1, matchContains: 1, cacheLength: 10, autoFill: true, mustMatch: false, selectFirst: true, max: 15 } ); ajaxListMatchingAccountName... - this is a ......

ASP.NET MVC with jQuery Grid

Incorporating the jQuery Grid With ASP.NET MVC Scenario: You have an ASP.NET MVC application and don't want to custom build smart controls like the ASP.NET GridView, ListView, etc, that support sorting and paging, as well as filtering and searching for data, and all of this using Ajax. Solution: The jQuery Grid plug-in. What tools/plug-ins do I need? jQuery version 1.3 and up jqGrid version 3 and up (this post references version 3.4.4) A strong cup of coffee In the past you had to also add the jQuery ......

Favorite Techical Books

A list of my favorite technical books that I found very useful over the years. Most of them are related to CSS, jQuery, DHTML, Ajax, and ASP.NET C#: <A HREF="http... _fcksavedurl=&q... ......

OutputCacheAttribute Override/Customization

The scenario: A web app is using the OutputCacheAttribute to set a cache duration for data that has different life spans. The IDE: ASP.NET MVC with Visual Studio 2008 The environment: Web-App on IIS The data that is being retrieved varies greatly in terms of their lifespan: Some data is very static like States, Countries Other data is more fluid, especially during end-user testing, like the load of scripts that have to be fixed occasionally and pushed to production during the day. Especially early ......