var builder = new ContainerBuilder(); builder.RegisterByAttributes(typeof(MyClass).Assembly); _container = builder.Build();
public interface ISingleInstanceClass {} [SingleInstance] public class SingleInstanceClass : ISingleInstanceClass {} public interface IInstancePerDependencyClass {} [InstancePerDependency] public class InstancePerDependencyClass : IInstancePerDependencyClass {} public interface IConcreteTypeRegistrationClass {} [SingleInstance(AsImplementedInterface = false)] public class ConcreteTypeRegistrationClass : IConcreteTypeRegistrationClass {} [SingleInstance(AsImplementedInterface = false)] [SingleInstance(Name = "Test", AsImplementedInterface = false)] public class MultiRegistrationClass {}
From a use case perspective... it's sort of a personal preference
issue. The team I work with prefers not to mix the DI technology into
the business code, so we specifically look for ways to not mark things
with attributes.
I'm sure if you wanted to add a contrib project with this stuff, you
could do that. You'd probably want a few more extension methods, like
the ability to register individual types by attribute rather than
whole assemblies, and you'd probably want to return the registration
info from the extension method (the way RegisterAssemblyTypes does) so
you could chain things together like
builder.RegisterTypesByAttribute(assembly).InNamespace("Just.This.Namespace");
-T
--
You received this message because you are subscribed to the Google Groups "Autofac" group.
To post to this group, send email to aut...@googlegroups.com.
To unsubscribe from this group, send email to autofac+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/autofac?hl=en.
public abstract AutofacRegistrationAttribute : Attribute
{
public abstract void
Register<TImplementor>(IRegistrationBuilder<TImplementor,
ConcreteReflectionActivatorData, SingleRegistrationStyle>
registration);
}
Custom attributes could derive from AutofacAttribute
public InstancePerDependencyAttribute : AutofacRegistrationAttribute
{
public abstract void
Register<TImplementor>(IRegistrationBuilder<TImplementor,
ConcreteReflectionActivatorData, SingleRegistrationStyle>
registration)
{
registration.InstancePerDependency();
}
}
The assembly scanning registration callback could find any attributes
derived from AutofacAttribute, do the RegisterType() call on them, and
just pass to the Register method on each attribute found.
You could do pretty much anything at that point - do additional custom
scanning for metadata, set lifetime, or whatever. Probably wouldn't be
too much churn in the core, but I admit I've not thought it totally
through. :)
On Dec 5, 9:54 pm, Nicholas Blumhardt <nicholas.blumha...@gmail.com>
wrote:
> Hi Rob!
>
> Steve Hebert had a shot at this some time ago - the implementation is in
> contrib:http://code.google.com/p/autofac/source/browse/#hg%2Fcontrib%2FSource...