IOC container assembly scanning performance

Asked at 2016-12-29 08:55:40Z
  • 5 Subscribers
  • 114 Views
1

Is convention based assembly scanning good idea? Currently I have something like

foreach(var type in assembly)
{
    if(isRepository(type))
        registerAsRepository(type);
}

This takes more than 1 second at startup. I wonder if it wouldn't be better to just register all types by hand, and implement some kind of "registration testing" in tests rather than at doing it at runtime?


1 answers in total

0
Krzysztof Kozmic Posted at 2017-01-02 11:53:01Z

I love this question. It's so simple yet it touches so many aspects of the problem space.

Like many things in software (IoC) containers aren't a panacea. They trade a level of runtime performance for a level of runtime flexibility and development time productivity.

Also like all tools, they work best when used properly. And since containers manage lifecycle of objects in your application, the properly part here refers to the structure of your app.

Now, it's hard to give specific advice having no details, so I'll be fairly generic.

First, the 1s overhead of scanning a single assembly is highly suspicious on anything resembling modern hardware. It's hard to judge from the pseudocode you posted where the issue in your real code might be, but there clearly is an issue and it's most likely not your container. I would also recommend not doing this type of low level scanning yourself and instead rely in the high level API that your container gives you. It's usually going to be more efficient and readable than any custom code like this.

Then there's the question of architecture. Containers are a sharp tool and give you a range of options despite the simplicity of their API. All the examples you give in the comments can be easily handled with a combination of by-convention registration, explicit type registration and some configuration. I would refer you to the documentation of your container of choice about the specifics but it's a standard scenario and all mature containers deal with it very well and with more than acceptable performance.

Answer this questsion