Moving!

Posted by Kenny Eliasson | Posted in | Posted on 10:24

Yes, as the title above says, i'm moving my blog posts to www.codejunkies.se.

CodeJunkies is a collaboration of friends with coding as a passion. We decided to group all our various blogs to one bigger (and hopefully better and more interesting) blog. Sooo, what are you waiting for, head over there now! :)

Meet my new HtmlHelper extensions friends, ForEach and If

Posted by Kenny Eliasson | Posted in , , , | Posted on 13:15

How many times have you written code like this in your MVC views?

<%
 int currentIndex = 0;
 foreach(var item in Model.Items) {
%>
    <% if(currentIndex == 0) {%>
       <div class="item first"><%= item.Name %></div>
    <% } else if(currentIndex == Items.Count()-1) { %>
       <div class="item last"><%= currentIndex + 1 %> <%= item.Name %></div>
    <% } else { %>
       <div class="item"><%= item.Name %></div>
    <% } %>
<%
 currentIndex++;
 }
%>

This is pure ugliness :(

What I missed was something like django for variables. They include both a Last and First variable on the loop.

After reading Phil Haacked's blog about "A code based repeater for .NET MVC" I decided to shamelessy take some parts of his code and write my own extension methods.

The first extension method I wrote was the Html.ForEach which gets me a index counter.

public static void ForEach<T>(this HtmlHelper html, IEnumerable<T> items, Action<T, int> render)
{
    if (items == null)
        return;

    int i = 0;
    items.ForEach(item => render(item, i++));
}

And to use it

<% Html.ForEach(Model.Items, (item, index) => { %>
    <div>#<%= index + 1 %> <%= item.Name %>
<% }); %>

Pretty slick and I dont need to have the counter variable in the view code. It uses lambda in way I didn't thought was possible before I read Haacked blog post.

I then decided to see if I could manage to check if I was on the first or last item in the loop.

I came up with this

public static void ForEachExtra<T>(this HtmlHelper html, IEnumerable<T> items, Action<T, ForLoop<T>> render)
{
    if (items == null)
       return;

    var loop = new ForLoop<T>(items);

    int i = 0;
    items.ForEach(item => { render(item, loop.Update(i)); i++; });
}

public class ForLoop<T>
{
    private readonly int _itemCount;

    public ForLoop(IEnumerable<T> items)
    {
        _itemCount = items.Count();
    }

    public int Counter { get; set; }
    public int Counter0 { get; set; }
    public bool First { get; set; }
    public bool Last { get; set; }

    public ForLoop<T> Update(int i)
    {
        Counter = i + 1;
        Counter0 = i;
        First = i == 0;
        Last = i == _itemCount-1;
        return this;
    }
 }


And to use this you will write

<% Html.ForEachExtra(Model.Templates, (template, loop) => {%>
   <% if(loop.First) { %> First! <% }%>">
   <div>#<%= loop.Counter %> - <%= template.Name %></div>
   <% if(loop.Last) { %> Last! <% }%>
<% }); %>


Starting to look impressive here! :) But I still think I can get it better, so onto my other new extension method, If()!

What I want is to have code that looks something like this

<% Html.ForEachExtra(Model.Items, (item, loop) => { %>
   <div class="item <%= Html.If(loop.First).Write("first").ElseIf(loop.Last).Write("last") %>
       #<%=loop.counter%> <%= item.Name %>
   </div>
<% }); %>

That would be awesome so off I went to write it.

public interface IConditionWrite
{
    IElseIfConditionBuilder Write(string output);
}

public interface IElseIfConditionBuilder
{
    IConditionWrite ElseIf(bool condition);
}

public class ConditionBuilder : IConditionWrite, IElseIfConditionBuilder
{
    private readonly IList<FluentHtmlCondition> _conditions;
    private FluentHtmlCondition _lastAddedCondition;

    public ConditionBuilder()
    {
        _conditions = new List<FluentHtmlCondition>();
    }

    public IConditionWrite AddCondition(FluentHtmlCondition condition)
    {
       _lastAddedCondition = condition;
       _conditions.Add(condition);
       return this;
    }
        
    public override string ToString()
    {
        foreach (var condition in _conditions) {
            if (condition.Fulfilled)
                return condition.Output;
        }

        return "";
    }

    public IConditionWrite ElseIf(bool condition)
    {
        AddCondition(new FluentHtmlCondition(condition));
        return this;
    }

    public IElseIfConditionBuilder Write(string output)
    {
        _lastAddedCondition.Output = output;
        return this;
    }
}

public class FluentHtmlCondition
{
    public readonly bool Fulfilled;
    public string Output;

    public FluentHtmlCondition(bool fulfilled)
    {
        Fulfilled = fulfilled;
    }

    public void Write(string output)
    {
        Output = output;
    }
 }

And in my Html-helper extension class i add

public static IConditionWrite If(this HtmlHelper html, bool condition)
{
    return new ConditionBuilder().AddCondition(new FluentHtmlCondition(condition));
}

And there you have it all, hope someone can have some use with it.

How I do data-access

Posted by Kenny Eliasson | Posted in , , , , | Posted on 10:04

Over the years I've tested alot of different techniques for getting data from a database in a .NET application.

From the simple ADO.NET wrapper that executes raw sql or SP's and then returning a DataTable to the more specified "Repository" where each entity in my domain had a corresponding Repository for querying the data.

I then discovered ORM's, especially NHibernate and started using it with the "One repository for each entity" approach. This worked great but when my projects grew bigger and bigger the need to create a new Repository for each new entity was to cumbersome (and often the Repository would have an interface, so for each new entity I created 3 new classes).

I later implemented Linq2NHibernate and made it so that the most basic queries (i.e find by name, ordering etc) was made using it. More advanced queries I still shuffled into a entity-specific repository.

My interface at this time looked at this

public interface INHibernateRepository
{
    ICriteria ExecuteCritera(DetachedCriteria criteria);
    IQuery ExecuteCritera(string hql);
    IList ExecuteMultiCriteria(params DetachedCriteria[] criterias);
    void Delete(int id) where ENTITY : DomainObject;
    int Save(ENTITY entity) where ENTITY : DomainObject;
    IQueryable Query() where ENTITY : DomainObject; //Linq2Nhibernate
    ENTITIY Get(int id) where ENTITIY : DomainObject;
}

Pretty basic stuff for querying and fetching by Primary Key.

If i got a entity specific repository i would extend INHibernateRepository like this

public interface QuestionRepository : INHibernateRepository
{
    Question GetLatestInserted();
    //more question specific implementations
}

I then, of course, had implementing classes of these interfaces.

To use it in a MVC application I used Dependency Injection to wire up my controllers.

public class QuestionController(INHibernateRepository repository, IQuestionRepository questionRepository, IUserRepository userRepository /* etc */)
{
}

Some may say that I shouldn't inject repositories directly to my controller, but even if I broke it out to a domain-service, it would still need 3 dependencies.

So instead of using the approach above I have begun using Domain Queries. I've read abit about them before but I was when i read Implementing Domain Queries i realized how it would benefit me.

I started my refactoring spree by adding two new methods on my repository interface

public interface INHibernateRepository : IRepository
{
    IEnumerable Query(IDomainQuery query);
    T FindOne(IDomainQuery domainQuery);
    /* All other methods */
}

I then one by one extracted the more complicated queries to their own query-class.

During this process I didn't encounter a single problem and I could remove all of my entity specific repositories and use _one_ repository for all database calls.

So when I'm writing a query nowadays, I first and foremost try to use Linq, if that doesn't work I create a new query class instead of creating a whole new repository for a single entity.

The template method pattern

Posted by Kenny Eliasson | Posted in , , | Posted on 11:44

One of my favorite patterns is the "Template method pattern".

Basically its about having a abstract class that defines common behaviour with points that can be customizable.

Yesterday I was refactoring alot of classes tha't didn't make use of the template pattern, but they all inherited from the same base-class. The base-class acted more like a common place for methods that all objects needed.

Let me show you some code that I had before the refactoring.

public class QuestionQuery : QueryBuilder {

public IFluentMdxQuery BuildQuery() {
  return new FluentMdxQuery(_context)
  .Get(x =>
  {
    x.Rowset.ForQuestion(_queryId).WithChildren();
    x.AddMeasure(DefaultMeasure.RespondentCount);
    x.AddMeasure(DefaultMeasure.RespondentShare);

    base.AddComparisonIndex(x);
    base.AddBreakdown(x);
    base.AddTimeBreak(x);
    base.SwapAxes(x);
  })
  .WithFilter(base.FilterManager)
  .Order.By(_properties.OrderBy)
  .TheMeasureIs(currentMeasure)
  .AscendingIf(_properties.OrderBy != OrderBy.Measure)
  .SetupWith(setup => {
    setup.HideTheseIds(q => q.AddRange(_properties.HiddenIds));
    setup.AddHighlight.OnMeasure(DefaultMeasure.RespondentShare);
  })
  .WithRules(rules => {
    rules.AddDefaultRules(_properties.ShowAllAnswers());
    foreach (var rule in base.AddedRules) {
      rules.Add(rule);
    }
    rules.Add(new MakeQuestionLevelTotalAndPlaceLastRule());
  });

Thats alot of base-method calls in there.
Now take into consideration that I've got somewhere between 5 to 10 more classes like this.

So yesterday I was assigned to add new functionality to the system which involved these classes. And because the classes are built as they are, I was forced to add the functionality to all classes, and that tingled my "bad code sense". I was duplicating code all over the place!

So I sat down and realized that the Template method would be a perfect pattern to implement here. Make all common calls in a base-class and add ways of overriding the certain parts of the creation.

So heres my new base class. (Some methods are missing since they not add any value besides of taking up space)
public IFluentMdxQuery BuildQuery(ISetupFluentFilterManager filterQueryManager)
{

  return new FluentMdxQuery(_context)
  .Get(x => {
    SetupDimensionCollector(x);
    AddComparisonIndex(x);
    AddBreakdown(x);
    AddTimeBreak(x);
    SwapAxes(x);
  })
  .WithFilter(AddFilter(filterQueryManager))
  .SetupOrder(SetupOrder)
  .SetupWith(AddSetupRules)
  .WithRules(ruleCollector => {
    ruleCollector.AddDefaultRules();
    foreach (var rule in _addedRules) {
      ruleCollector.Add(rule);
    }
    AddExtraRules(ruleCollector);
  });
}

  protected abstract void SetupDimensionCollector(IDimensionCollector dimensionCollector)

  protected virtual void SetupOrder(IFluentMdxSortCommand sortCommand)
  {
    sortCommand.UseDefault();
  }

  protected virtual void AddExtraRules(IRuleCollector ruleCollector) { }


Its almost looks the same as the old one, but this time I've added the metods to the base class, implemented the default behaviour and made them virtual so they can be overriden in the child classes. I also made some methods abstract to force the implementing classes to add them.

So, how did these changes affect my QuestionQuery class?

protected override void SetupDimensionCollector(IDimensionCollector dimensionCollector)
{
  dimensionCollector.Rowset.ForQuestion(_queryId).WithChildren();
  dimensionCollector.AddMeasure(DefaultMeasure.RespondentCount);
  dimensionCollector.AddMeasure(DefaultMeasure.RespondentShare);
}

protected override void SetupOrder(IFluentMdxSortCommand sortCommand)
{
  sortCommand.By(_querySettings.OrderBy);
  sortCommand.TheMeasureIs(GetCurrentMeasure());
  sortCommand.AscendingIf(_querySettings.OrderBy != OrderBy.Measure);
}


Damn! Only 2 methods needed to be overriden. Everything else was default behaviour. Of course not all my classes we're as simple as this, but they all got more to the point and I avoided alot of duplication.

These changes also make it easy for me to add functionality to all query classes easy, just add it to the base class and we're done :)

Shout it

Model binding i .NET MVC

Posted by Kenny Eliasson | Posted in , | Posted on 15:55

(From now on I will try to write my posts in english)
I see alot of variations in how people do model-binding in .NET MVC.
Some guys use the old-school way using Request.Form directly in their Controller actions.

public ActionResult Create()
{
Recipe recipe = new Recipe();
recipe.Name = Request.Form["Name"];
return View();
}

A better way is to pass a FormCollection to the action.
public ActionResult Create(FormCollection values)
{
Recipe recipe = new Recipe();
recipe.Name = values["Name"];

// ...

return View();
}

The best way is of course to accept your entity directly.
public ActionResult Create(Recipe recipe)
{
_repository.Save(recipe);
return View();
}

This works great when dealing with simple objects.
But what happens when you have a rich domain model and your entities references other entities and so on..

Take this simple class for examle
public class Comment
{
public int Id { get; set; }
public string Message { get; set; }
public Post Post { get; set; }
}

If Post is a reference to entity that's already saved in the database. How do you populate it nicely?
Either you can let the ModelBinder bind what it can, and in your action method do
public ActionResult Create(Comment comment)
{
comment.Post = _repository.Load(Request.Post["PostId"]);
_repository.Save(comment);
return View();
}

This works, but I dont really like it :P

I return to a solution further down.

Another thing I often find irritating is the need to inject a repository into a controller only to read up a entity.
Instead of having to do like this
public class PostController : Controller
{
private readonly IRepository _repository;
public PostController(IRepository repository)
{
_repository = repository;
}

public ViewResult Details(int id)
{
return View(_repository.Get(id));
}
}

I would instead like it to be like this.
public class PostController : Controller
{
public ViewResult Details(Post post)
{
return View(post);
}
}

Now this was a very simple case, but even then i think it has several advantages, one being that it's easier to unit-test this method and another being that we can hide alot of repetetive code.

How do I accomplish both binding to Details/Show-actions as well as binding more complex objects?

Answer: I wrote my own modelbinder ;)
public class GenericBinderResolver : DefaultModelBinder
{
private readonly ICanResolveDependencies _resolver;
private static readonly Type BinderType = typeof(IModelBinder<>);

private class ModelBindResult
{
public bool Success;
public object BoundObject;
}

///
/// Creates a new GenericBinderResolver. This should be used as the DefaultModelBinder
///

/// For easy testability of this class we created a new interface thats extracts out the "GetInstance" method from StructureMap.
public GenericBinderResolver(ICanResolveDependencies resolver)
{
_resolver = resolver;
}

public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
//First look at the routes to see if we have any parameters named "Id", if we do load the entity from the database
var result = TryBindById(bindingContext, controllerContext);
if(result.Success) {
return result.BoundObject;
}

//If there wasn't a Id in the route check if we have custom modelbinder for the type.
result = TryBindWithCustomModelBinder(bindingContext, controllerContext);
if (result.Success) {
return result.BoundObject;
}

//If nothing works, do it the usual way.
return base.BindModel(controllerContext, bindingContext);
}

private ModelBindResult TryBindById(ModelBindingContext bindingContext, ControllerContext controllerContext)
{
var result = new ModelBindResult();

int entityId = 0;
//Is the type we're binding to assignable to our base domain object.
if (IsAssignableToDomainObject(bindingContext.ModelType))
{
if (EntityExistsInRoute(controllerContext.RouteData.Values, bindingContext.ModelName, out entityId))
{
result.Success = true;
result.BoundObject = _resolver.TryGetInstance().Get(bindingContext.ModelType, entityId);
}
}

return result;
}

private ModelBindResult TryBindWithCustomModelBinder(ModelBindingContext bindingContext, ControllerContext controllerContext)
{
var result = new ModelBindResult();
Type genericBinderType = BinderType.MakeGenericType(bindingContext.ModelType);
IModelBinder binder = _resolver.TryGetInstance(genericBinderType) as IModelBinder;
if (binder != null) {
result.Success = true;
result.BoundObject = binder.BindModel(controllerContext, bindingContext);
}
return result;
}

private bool EntityExistsInRoute(RouteValueDictionary routeValueDictionary, string modelName, out int entityId)
{
entityId = TryParseRouteData(routeValueDictionary, "id");
if (entityId > 0)
return true;

entityId = TryParseRouteData(routeValueDictionary, string.Concat(modelName, "id"));
if (entityId > 0)
return true;

return false;
}

private int TryParseRouteData(RouteValueDictionary routeValueDictionary, string key)
{
if (routeValueDictionary.ContainsKey(key))
{
string id = routeValueDictionary[key].ToString();
if (!string.IsNullOrEmpty(id))
{
return int.Parse(id);
}
}

return 0;
}

private bool IsAssignableToDomainObject(Type modelType)
{
return typeof(DomainObject).IsAssignableFrom(modelType);
}
}

Alot of code but the important things is that I inject a instance of ICanResolveDependencies which is a implementation of your favorite IoC-container. In my case StructureMap.

I then look at the route data for a key named "id" if the ModelBindingContext.ModelType is a DomainObject which happens to be my base-class for all entites.
So routes like "Post/Detail/5" would have a key named "id". I take the key and use it with to get the entity from the database. And whops i solved a problem.

Also important to note is that if the ModelType isn't is a DomainObject nothing happens and I will let the default model-binder do its work.

The next thing I do is to check if I have custom model-binder class for the ModelType. All my custom model-binders inherit from the same base-class which checks the Request.Form and Querystring dictionaries for a key named "id". If it finds a key named "id" and its greater than 0 it will call the abstract method "BindExisting(int originalId)" else it calls the abstract method "BindNew()".

A implementation of a custom model-binder can look like this
public class CommentBinder : ModelBinder
{
private readonly INHibernateRepository _repository;
private readonly IUserContext _context;

public CommentBinder(INHibernateRepository repository, IUserContext context)
{
_repository = repository;
_context = context;
}

protected override Comment BindNew()
{
//Don't do more work then necessary, I have method on the base-class which does the default model-binding.
var comment = base.DoDefaultModelBinding();

//Since I use NHibernate, just get a proxy to the Post and don't hit the database.
//The Get() method is within the base-class and just returns Request-data.
comment.Post = _repository.Load(Get("PostId"));
comment.User = _context.User;

return comment;
}

protected override Comment BindExisting(int entityId)
{
//load the entity and change the values
_repository.Get(entityId);

var comment = base.DoDefaultModelBinding();

comment.Post = _repository.Load(Get("PostId"));
comment.User = _context.User;

return comment;
}
}

(I left out the code for the base class, if you want too see it email me kennyeliasson at gmail dot com)

All I need to care about is how to handle existing and new objects.
This solution have worked out really nice for me WHEN i have more complex objects. Of course I use the default model-binding as often as i can, but sometimes it just isn't enough.

Fluent NHibernate och automappa komponenter

Posted by Kenny Eliasson | Posted in , , , | Posted on 13:28

Nu så vart man "äntligen" tillbaka på jobbet efter en skön jul och nyårsledighet.

Det första jag tog tag i var att generera den nya tabeller till våran rapporteringsapplikation. Jag har tidigare berättat om hur jag använder Fluent NHibernate för att göra detta, men denna gången störde jag mig mycket på att jag var tvungen att göra många mappningar "by-hand" via IAutoMappingOverride för att jag skulle mappa Components.

En Component är en egen klass men datan för klassen ligger i föräldrens tabell.
T.ex. en MetaData-klass som innehåller Created, Version m.m.
Denna MetaData-klass vill man inte mappa till en helt egen tabell utan man mappar min MetaData-klass till kolumner i dess föräldrer. I detta fall mappar jag min Report-entitet till en tabell med namnet Reports som även innehåller kolumner för MetaData-klassen.

Problemet är ju att MetaData-klassen ligger som en property på Reports-klassen och därmed kommer mappas som en "one-to-many" referens. För att komma runt detta innan har jag ofta mappat komponenter för hand, men detta tröttnade jag på idag och bestämde mig för att se hur man kommer runt detta.

Efter lite eftersökningar hittade jag hur säger vilka typer som skall hanteras som komponenter.

På Fluent Nhibernates AutoPersistanceModel finns det en metod som heter Setup(). Via denna kan man bestämma vilka klasser som skall hanteras som komponenter.

.Setup(s => { s.IsComponentType = type => typeof (MetaData) })

Nu fick jag MetaData-klassen att vara mappad som en komponent. Men hur jag gör om så att alla mina befintliga komponenter mappas in på samma sätt?

.Setup(s => { s.IsComponentType = type => typeof (MetaData) || type => typeof(Component2) || etc.. etc.. })

Att göra som ovan med flera "eller" alternativ kan ju bli lite grötigt.

Det jag gjorde istället var att jag gjorde ett tomt interface, ett så kallat marker-interface

public interface IComponent { }

och gjorde så att alla mina komponent-klasser ärvde av det. Sen ändrade jag Setup-koden till

.Setup(s => { s.IsComponentType = type => typeof (IComponent).IsAssignableFrom(type) })

Detta jag tycker jag är sjukt smidigt :)

Om man inte vill använda marker interfaces kan man ju t.ex. använda sig av attribut men då måste man använda reflection för att läsa av klasserna.

Vad tycker ni, vettig lösning? Vad tycker ni om Marker-interfaces vs. Attribut?

Django, perfect fit

Posted by Kenny Eliasson | Posted in , , , | Posted on 12:20

Har under min vintersemester haft möjligheten att fördjupa mig i Django.

Det enda jag gjort i Django tidigare är ett par väldigt lätta exempel som jag visat upp för kompisar men nu skulle jag göra ett riktigt projekt, om än väldigt litet.

Sidan jag skapar är en personlig hemsida till en släkting och det enda som behövs är ett nyhetsarkiv och ett bildgalleri, allt annat kan vara statiskt till en början då hemsidan inte är tänkt att uppdateras allt för ofta.

Så jag satte igång med att generera mina modeller, admin-gränssnittet ändrade inställningar för språk och databaser tills jag stötte på mitt första problem, inlänkning av stylesheets, javascript och bilder.

Först trodde jag att det hade med mina urls, att jag skulle mappa "media"-filer rätt, men det funkade inte! Efter att ha gjort en snabb google-sökning så visade det sig att man skulle använda sig av en inställning som heter MEDIA-ROOT.
Som tur är har vi ett Django projekt på jobbet, så jag drog helt sonika hem det för att se hur dom hade löst det.

if LIVE:
SITE_URL = 'http://www.skarp_hemsida.se'
else:
SITE_URL = 'http://localhost:8000'

DJANGO_ROOT = os.path.dirname(os.path.realpath(django.__file__)).replace("\\", "/")
SITE_ROOT = os.path.dirname(os.path.realpath(__file__)).replace("\\", "/")
MEDIA_ROOT = SITE_ROOT + '/m/'
MEDIA_URL = SITE_URL + '/m/'
ADMIN_MEDIA_PREFIX = '/media/'

En alldeles utmärkt lösning som funkar live och lokalt. Nu var det bara att länka in stylesheetsen med "/m/css/xxx.css"

Andra problem som jag stötte på är att alltid glömma ":" efter class och for-loop deklarationer (class MyClass: resp. for i in ints:).
Att kopiera kod från andra dokument eller ifrån webben förstör verkligen tab-indentering och får Python att fräsa ur sig felmeddelanden.

Saker som jag verkligen avundar folk som sitter och jobbar med Django är template-systemet. Jag som kommer ifrån .NET med sina fula <% %> taggar och for/if-satser avundas verkligen {% %} syntaxen och alla hjälp-metoder som finns.

ActiveRecord, dvs databashantering, spara, querya etc är nåt som funkar out-of-the-box och inget man tänker över. Skönt att slippa behöva skriva kod för att få detta att funka.

PIL, Python Image Library, har jag jobbat en del med för att förminska bilder. Av det lilla jag jobbat med det har det varit helt underbart, man märker att utvecklarna bakom har tagit ett steg till och tänkt till hur vi som utnyttjar API:et vill använda det, inte hur man proppar det med fullt "low-level" funktioner som t.ex. .NET's System.Drawing.

Det har varit mycket lärorikt att sitta och jobba med ett helt annat språk ett tag, ett dynamiskt språk har verkligen sina fördelar men även nackdelar, men mycket av nackdelarna just nu är antagligen för att jag inte har 100% koll på Python.

Någon av er andra som testat ett par andra språk? Ruby har det ju varit mycket snack om men jag har inte riktigt gillat syntaxen. Kanske nåt funktionellt, F#, Lisp, Haskell? Skriv gärna en kommentar :)