Nuget

Windsor Castle IoC

Look closely at your design. When you look at it functionaly, your repository doesn't really depend on the the session at all, but at some data you store in the session. Create an abstraction over the things you want to extract from the session and let the repository depend on such abstraction. For instance:

public interface IUserProvider { int GetCurrentUserId; } public class SomeRepository : ISomeRepository { private readonly IUserProvider userProvider; public SomeRepository(IUserProvider userProvider) { this.userProvider = userProvider; } }

Now you can create the following implementation of that abstraction:

private class HttpSessionUserProvider : IUserProvider { public int GetCurrentUserId { return (int)HttpContext.Current.Session["UserId"]; } }

You can register this concrete type in your IoC configuration.

This is much better, because you don't want to let your repository depend directly on the HTTP session. This makes testing harder and creates a dependency between your repository and a specific presentation technology.

Source: stackoverflow.com
Video on topic: Windsor Castle IoC
The Castle Windsor Inversion of Control Container
The Castle Windsor Inversion of Control Container
Windsor Castle
Windsor Castle
Intro to IoC and Castle Windsor
Intro to IoC and Castle Windsor

Interesting facts
Share this Post

Related posts

About Windsor Castle

About Windsor Castle

FEBRUARY 28, 2024

Windsor Castle - 5 Things you never knew - TheTrainline Blog

Read More
Windsor Castle Tea

Windsor Castle Tea

FEBRUARY 28, 2024

Get a real taste of British history as you wander through the historic castle of Windsor. It has been a royal home for over…

Read More