Archive for category OO Design

Ninject + WCF Docs

I’ve recently started using Ninject, having previously only used Unity as my IoC container, but it seems the internet is scarce of any documentation. I’ve employed the WCF extension, and found this handy guide on how to get up and going with it. www.aaronstannard.com/post/2011/08/16/dependency-injection-ninject-wcf-service.aspx

ps- This is my 200th blog post! Woohoo!

No Comments

OO & Inheritance with Javascript

I’m getting ready to start a game development project and getting my OO Javascript up to scratch- I’ve been googling around for a while as there seems to be a whole load of different ways to implement OO and in particular inheritance in javascript.  I’ve finally found a method that work’s reliably, and with multiple generation of inheritance- It’s from an article on the site point website – for quick reference I will cut to the meat and give a usage example- for details see the original article;

add this function to your project;

function copyPrototype(descendant, parent) {
  var sConstructor = parent.toString();
  var aMatch = sConstructor.match( /\s*function (.*)\(/ );
  if ( aMatch != null ) { descendant.prototype[aMatch[1]] = parent; }
  for (var m in parent.prototype) {
    descendant.prototype[m] = parent.prototype[m];
  }
};

Then define your classes (prototypes) like so;

function AClass(param) {
   this.Text=param;
}
AClass.prototype.Speak = function() {
  alert(this.Text);
}

function BClass(param, name) {
  this.AClass(param);
  this.Name = name;
}
copyPrototype(BClass, AClass);
BClass.prototype.Speak = function() {
  alert('Hi ' + name);

  // Call overridden parent method...
  AClass.prototype.Speak.apply(this);
}

So the above example will give you 2 classes- B is derived from A.  Calling AClass.Speak will give you a single alert with the contents of the 'text' property, whereas BClass.Speak will give you two text boxes; the first greeting you by name, and the second being the alert box from AClass.  I've tested this myself with multiple generations of inheritance and it worked fine, where other methods have not.

No Comments

Thycotic Design Pattern Articles

Great bunch of articles on different design patterns up on the Thycotic Blog;

No Comments

Creating a re-usable shopping basket with Generics in C#

We recently had troubles with our new reusable basket we’re developing at work.  We sell books, and most books come from one central data source which for the sake of this article i shall call CDS- so we have a CDS entities project which holds elements Group, Book, Author etc.  However not all of our sites run use the CDS for their data- so we have other projects with their own elements for Book, Author, Category etc which hold their own data.

We wanted to create a central project containing the basket functionality which we could drop into any project and use.  So the basket needs to be able to add lots of different types of books- some from the CDS entity library or some any other entity library we use.  To do this we created an Interfaces project which holds an interface called IBook which held the minimum data required by the basket such as Title, Price, ISBN, Cover Image.  This project is then referenced by the sites, the entity libraries (like CDS or any other’s we build) and the basket itself.

Adding the book to the basket was fine, however the problem arose when viewing items- the basket needed to be able to load the book data back- this is fine we thought- we would simply add a load() method to IBook, however this didnt solve the problem as the basket only had IBook signature of load- it didn’t know which concrete implementation of load to run.

The answer was to use generics.  We added a generic parameter to the top of the basket which accepted a type which it assumed to be derived from IBook and always have a default constructor, like so;

public class Basket<T> where T : Hachette.ProcessLayer.IBook, new() {
   private List<BasketItem<T>> _basket_items;
   ...
}

Likewise we made the basket item’s generic, and pass the type T from the basket to the basket item when instanciating them- the basket items are then able to define a real book instance of type T so it knows which concrete book it’s dealing with, so it can call the correct load method. The use of generics here means the basket object can reference a real book class from the CDS project (in a separate assembly) without having to reference that assembly, meaning we can drop in any type of book from any assembly for future sites, and with the parent website stating the type to use when creating the basket, the basket can remain completely independent.

2 Comments