The Repository Pattern

Preliminary documentation for the projects:

CodeCater.EntityBase
CodeCaster.DataAccess
CodeCaster.DataAccess.EntityFramework
CodeCater.Security
CodeCaster.Security.MembershipAndRoleProviders
CodeCaster.Security.EntityBase
CodeCaster.Security.Entities (todo: create SecuredEntityAccess as repository filter)
CodeCaster.Security.DataAccess.EntityFramework

The goal: create a repository library that:

  • Is testable
  • Is less awkward to use than all repository code out there
  • Reduces boilerplate, ugly or just plain dangerous code in any class that wishes to expose any kind of data, for example a database table displayed through an MVC controller
  • Does not limit in any way the use of the underlying context, if any.
  • Does not rely on Entity Framework or any persistence framework (though allows for it through extensions, like CodeCaster.DataAccess.EntityFramework!)
  • Can also be used to wrap files, Xml, JSON or really any data source: repository is a principle, nothing EF-specific!
  • Has been used (by me) to realize projects, so common fallacies are fixed before RTM.
  • Is open source and free.
  • Helps structure projects.
  • Allows for common tasks to be performed in a streamlined and standardized manner, like constraining EF’s Include() code to the appropriate repository. Someone once called custom repository methods like invoiceRepository.GetUnpaidInvoices() “awkward”, but I truly can’t see what’s wrong with it.

Presumptions:
1. All entities have an integer ID field.
2. All secured entities have an SecurityDescriptor_ID field pointing to a Security Descriptor that describes the permissions for that given entity.

Posted in Tech | Leave a comment

Stack Overflow reference questions

In this post I keep a list of questions and answers I frequently link to on Stack Overflow.

.NET

NullReferenceException

What is a NullReferenceException and how do I fix it?

HTTP 500 – Internal Server Error on IIS

How to find the underlying cause for an HTTP 500 – Internal server error in IIS?

Localization of ASP.NET MVC Unobtrusive Validation

MVC 3 jQuery Validation/globalizing of number/decimal field

Change Visual Studio New Class Template

How do I edit the Visual Studio templates for new C# class/interface?

IPC mechanisms in .NET

IPC Mechanisms in .NET – Usage and Best Practices

Enable WCF Tracing

How to turn on WCF tracing?

 

PHP

Enable Error Reporting in PHP

How to get useful error messages in PHP?

Posted in Tech | Leave a comment

Posting sortable form items to an MVC controller

I wanted to allow the user to edit a list of items while also enabling the user to sort them, and to post that back in a meaningful way without parsing on the server side. This does the trick:

public class SortableModel
{
  public List<CustomItem> CustomItems { get; set; }
}

public class CustomItem
{
  public int Order { get; set; }
  public string Description { get; set; }
}  

var model = new SortableModel
{
  CustomItems = new List<CustomItem>
  {
    new CustomItem { Order = 0, Description = "Test 1"},
    new CustomItem { Order = 1, Description = "Foo 2"},
    new CustomItem { Order = 2, Description = "Bar 3"}
  }
};

View:

<script src="@Url.Content("Scripts/jquery-1.9.1.js")"></script>
<script src="@Url.Content("Scripts/jquery-ui-1.10.1.js")"></script>
<script type="text/javascript">
  $(function () {
    $("#sortMe").sortable({
      stop: function (event, ui) {

        $("#sortMe li").each(function (index, element) {
          // find the hidden input in each <li> and set it to its current index, according to the DOM
          var hiddenInput = $(element).children(".order").first();
          hiddenInput.val(index);
        });
      }
    });
  });
</script>
@using (Html.BeginForm())
{
  <ul id="sortMe">
  @for (int i = 0; i < Model.CustomItems.Count; i++)
  {
    <li>@Html.HiddenFor(m => m.CustomItems[i].Order, new { @class = "order" }) @Html.TextBoxFor(m => m.CustomItems[i].Description) <span>Drag me!</span></li>
  }
  </ul>
  <input type="submit" value="Save" />
}

Now my action can accept a SortableModel parameter, where we can loop through the list of CustomItems in order to determine their order!

[HttpPost]
public ActionResult CustomFieldsForPort(SortableModel model)
{
  // do your thing with model!
	
  return View(model);
}

Up next: adding and deleting items.

Posted in Tech | Leave a comment

Controller injection with Unity under ASP.NET MVC 4

A lot of different manuals exist on the web explaining various portions of MVC unit testing, Entity Framework mocking and Inversion of Control (IoC) using Unity as dependency injection container. In this and hopefully future posts I’ll try to sew together the bits I found on various places.
Continue reading

Posted in Tech | Tagged , , , , , | 1 Comment

An idea for multilingual webpages: the Content-Languages response header

Much content on the web is provided in multiple languages. Think about corporate websites, Wikipedia and various sites running on popular content management systems. The issue I would like to address is the display and selection of the languages the content is available in.
Continue reading

Posted in Tech | Leave a comment

Using the WebClient class – with cookies!

Contrary to the somewhat crude HttpWebRequest class, the WebClient allows the developer to easily download and upload data and strings from and to webservers.

The rough edges of the HttpWebRequest class make developers jump to the WebClient (or HttpClient, since .NET 4.5) to significantly increase the ease-of-use. The WebClient has one drawback: it doesn’t support cookies!

Continue reading

Posted in Tech | Tagged , , , , | Leave a comment

Configuring a shared packages directory for NuGet and automatically downloading packages

This entry is also available in Dutch.

The ever so popular Visual Studio extension named NuGet enables the developer to easily install packages from within the development environment. NuGet packages contain populair and less known libraries, like log4net, jQuery and Entity Framework.

Continue reading

Posted in Tech | Tagged , , , | 2 Comments

Sockets and protocols: exchanging messages. Using bytes.

There’s also a Dutch version of this article available.

When two applications wish to communicate over an (inter)network, sockets quickly come into play. Nowadays sockets are delivered by almost every operating system, in the form of an API that enables the developer to easily transfer data through a local network or over the internet to another machine.

Continue reading

Posted in Tech | Tagged , , | 5 Comments

Remove Origin advertisements

Do you love the Sims and FIFA advertisements popping up after logging in to Origin when you’re about to play some Battlefield 3? Or even the Premium ads while you already have bought Premium? I don’t, so inspired by this post I contacted EA support and asked if there’s any way to disable displaying the advertisements. Their solution: reboot your PC. Luckily, I found a real solution:

Run Notepad (or the slightly better alternative) and open the file %appdata%\Origin\Settings.xml, and locate the following lines:

<Setting key="PromoBrowserGameFinishedLastShown" type="2" value="xxx"/>
<Setting key="PromoBrowserOriginStartedLastShown" type="2" value="xxx"/>

Change the values of xxx to 9999999 and the ads will disappear. :)

Posted in Tech | Tagged , , | 4 Comments

Remotely viewing detailed error pages in IIS

When deploying applications or services to IIS one can sometimes run into errors that haven’t been foreseen during development. Of course, with appropriate logging you can quickly identify the source of the problem, but when your site lacks this functionality you’d love to see the same detailed exception information as you see while you’re developing the application.

Continue reading

Posted in Tech | Tagged , , | Leave a comment