Skip to content

Instantly share code, notes, and snippets.

@distantcam
Created August 10, 2012 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save distantcam/3312705 to your computer and use it in GitHub Desktop.
Save distantcam/3312705 to your computer and use it in GitHub Desktop.
Attachments
abstract class Attachment<T> : IAttachment
{
protected T viewModel;
protected abstract void OnAttach();
void IAttachment.AttachTo(object obj)
{
viewModel = (T)obj;
OnAttach();
}
}
class AutoAttachmentModule : Module
{
protected override void AttachToComponentRegistration(
IComponentRegistry componentRegistry, IComponentRegistration registration)
{
registration.Activating += Activating;
}
void Activating(object sender, ActivatingEventArgs<object> e)
{
var vmType = e.Instance.GetType();
var attachments = Conventions.FindAll<AttachmentConventions>(vmType)
.Where(t => e.Context.IsRegistered(t))
.Select(t => (IAttachment)e.Context.Resolve(t));
foreach (var a in attachments)
a.AttachTo(e.Instance);
}
}
interface IAttachment
{
void AttachTo(object obj);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment