The ASP.NET MVC framework is not very friendly (or rather requires too many settings to fake correctly and causes too much friction when testing, IMHO) due to the use of abstract base classes instead of interfaces. We managed to write abstractions for each request and session storage. We keep these abstractions very light, and then our controllers depend on these abstractions for each request or for storage in the session.
For example, here we manage auth files. We have ISecurityContext:
public interface ISecurityContext { bool IsAuthenticated { get; } IIdentity CurrentIdentity { get; } IPrincipal CurrentUser { get; set; } }
With a specific implementation, for example:
public class SecurityContext : ISecurityContext { private readonly HttpContext _context; public SecurityContext() { _context = HttpContext.Current; } public bool IsAuthenticated { get { return _context.Request.IsAuthenticated; } } public IIdentity CurrentIdentity { get { return _context.User.Identity; } } public IPrincipal CurrentUser { get { return _context.User; } set { _context.User = value; } } }
chadmyers Oct 06 '08 at 22:02 2008-10-06 22:02
source share