Skip to content

Instantly share code, notes, and snippets.

@nemesv
Created April 24, 2013 15:09
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 nemesv/0ad4f1b3363102b6ad9f to your computer and use it in GitHub Desktop.
Save nemesv/0ad4f1b3363102b6ad9f to your computer and use it in GitHub Desktop.
[TestFixture]
public class Class2
{
[Test]
public void Test()
{
var contianerBuilder = new ContainerBuilder();
contianerBuilder.Register(c => CreateConfigureSpecialEventAggregator())
.Named<IEventAggreator>("SpecialUseAggregator");
contianerBuilder.Register(c => CreateConfigureAnotherUseAggregator())
.Named<IEventAggreator>("AnotherUseAggregator");
contianerBuilder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.Where(t => t.Name.EndsWith("ViewModel"))
.OnPreparing(Method)
.WithMetadataFrom<EventAggrAttribute>();
var container = contianerBuilder.Build();
var aViewModel = container.Resolve<AViewModel>();
var cViewModel = container.Resolve<CViewModel>();
Assert.AreEqual(((EventAggreator)aViewModel.eventAggreator).Name, "SpecialUseAggregator");
Assert.AreEqual(((EventAggreator)cViewModel.eventAggreator).Name, "AnotherUseAggregator");
}
private void Method(PreparingEventArgs obj)
{
ResolvedParameter resolvedParameter = ResolvedParameter.ForNamed<IEventAggreator>(obj.Component.Metadata["Key"].ToString());
obj.Parameters = new List<Parameter>() { resolvedParameter};
}
private EventAggreator CreateConfigureSpecialEventAggregator()
{
return new EventAggreator("SpecialUseAggregator");
}
private EventAggreator CreateConfigureAnotherUseAggregator()
{
return new EventAggreator("AnotherUseAggregator");
}
}
public class EventAggrAttribute : Attribute
{
public string Key { get; set; }
public EventAggrAttribute(string key)
{
Key = key;
}
}
[EventAggrAttribute("SpecialUseAggregator")]
public class AViewModel
{
public readonly IEventAggreator eventAggreator;
public AViewModel(IEventAggreator eventAggreator)
{
this.eventAggreator = eventAggreator;
}
}
[EventAggrAttribute("SpecialUseAggregator")]
public class BViewModel
{
private readonly IEventAggreator eventAggreator;
public BViewModel(IEventAggreator eventAggreator)
{
this.eventAggreator = eventAggreator;
}
}
[EventAggrAttribute("AnotherUseAggregator")]
public class CViewModel
{
public readonly IEventAggreator eventAggreator;
public CViewModel(IEventAggreator eventAggreator)
{
this.eventAggreator = eventAggreator;
}
}
[EventAggrAttribute("AnotherUseAggregator")]
public class DViewModel
{
private readonly IEventAggreator eventAggreator;
public DViewModel(IEventAggreator eventAggreator)
{
this.eventAggreator = eventAggreator;
}
}
public class EventAggreator : IEventAggreator
{
public readonly string Name;
public EventAggreator(string name)
{
this.Name = name;
}
}
public interface IEventAggreator
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment