Skip to content

Core Attributes

Xet Erixon edited this page Aug 17, 2017 · 4 revisions

Attributes

There is an attribute called AutoRegister that can be added to a class that makes the registration a bit more easy. It looks something like this:

public interface IModel{}
[AutoRegister]
public class Model : IModel
{
}

For the container to pick up the auto registered items, you need pass it the assembly holding the classes. Do something like this in your bootstrap code:

using NotNet.Core;
public class Program
{
	public static int Main(string[] args)
	{
		Container.Default.AutoRegister(typeof(Program).GetTypeInfo().Assembly);
	}
}

So when you add a new class that you want to be resolvable, simply add the [AutoRegister] attribute to it, and the container will pick it up.

If you want the auto registered type to be a singleton, do [AutoRegister(ObjectLifecycle.Singleton)].

Constructors

When resolving the object, the constructor with the least amount of arguments are used by default. If you, for some reason, want to use another constructor, decorate it with [PreferredConstructor].

Notes

Someone might argue that using the any of the attributes will make your otherwise nice and clean POCOs dependant on NotNet.Core. This is naturally correct. Hence I will not force anyone to use it, but simply inform about it. I use it my self in the apps I develop since I don't mind being dependant on my own stuff. Hmm, that sounded a bit meta....

Clone this wiki locally