Skip to content

Instantly share code, notes, and snippets.

@RexMorgan
Created November 4, 2010 01:17
Show Gist options
  • Save RexMorgan/661992 to your computer and use it in GitHub Desktop.
Save RexMorgan/661992 to your computer and use it in GitHub Desktop.
public interface IPartialActionExtension<T>
where T : class
{
T Modify(T model);
}
public interface IPartialActionExtensionGraph<T> : IPartialActionExtension<T>
where T : class
{
}
<%@ Page Language="C#" CodeBehind="MasterLoginLogout.aspx.cs" Inherits="Project.Web.Shared.Partials.Views.MasterLoginLogout" %>
<%@ Import Namespace="Project.Web.Models.Users" %>
<% if (Model.IsAuthenticated) {%>
<a href="<%=Urls.UrlFor(new UserLogoutGetModel())%>">Logout</a>
<% } else { %>
<a href="<%= Urls.UrlFor(new UserLoginGetModel()) %>">Login</a>
<%} %>
public class MasterLoginLogout : FubuPage<MasterLoginLogoutModel>
{
}
[PartialModel]
public class MasterLoginLogoutModel
{
public bool IsAuthenticated { get; set; }
}
public class MasterLoginLogoutPartialActionExtension : IPartialActionExtension<MasterLoginLogoutModel>
{
private readonly IAuthenticationManager _authenticationManager;
public MasterLoginLogoutPartialActionExtension(IAuthenticationManager authenticationManager)
{
_authenticationManager = authenticationManager;
}
public MasterLoginLogoutModel Modify(MasterLoginLogoutModel model)
{
model.IsAuthenticated = _authenticationManager.LoggedIn;
return model;
}
}
public class PartialAction<T>
where T : class
{
private readonly IPartialActionExtensionGraph<T> _graph;
public PartialAction(IPartialActionExtensionGraph<T> graph)
{
_graph = graph;
}
public T Execute(T input)
{
return _graph.Modify(input);
}
}
public class PartialActionExtensionGraph<T> : IPartialActionExtensionGraph<T>
where T : class
{
private readonly IContainer _container;
public PartialActionExtensionGraph(IContainer container)
{
_container = container;
}
public T Modify(T model)
{
_container.GetAllInstances<IPartialActionExtension<T>>()
.Each(extension => model = extension.Modify(model));
return model;
}
}
public class PartialActionsSource: IActionSource
{
public IEnumerable<ActionCall> FindActions(TypePool types)
{
return types
.TypesMatching(t => t.HasAttribute<PartialModelAttribute>())
.Select(m =>
{
var actionType = typeof(PartialAction<>).MakeGenericType(m);
return new ActionCall(actionType, actionType.GetExecuteMethod());
});
}
}
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Project.Web.Shared.Master.Site" %>
<%@ Import Namespace="FubuCore" %>
<%@ Import Namespace="Project.Web.Shared.Partials.Models" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Project Master Page</title>
</head>
<body>
<% this.Partial<MasterLoginLogoutModel>(); %>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment