Created
August 10, 2012 08:55
-
-
Save distantcam/3312705 to your computer and use it in GitHub Desktop.
Attachments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Attachment<T> : IAttachment | |
{ | |
protected T viewModel; | |
protected abstract void OnAttach(); | |
void IAttachment.AttachTo(object obj) | |
{ | |
viewModel = (T)obj; | |
OnAttach(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IAttachment | |
{ | |
void AttachTo(object obj); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment