VSharma
VSharma

Reputation: 358

Pros and cons of injecting dependencies through code or configuratoin

I know structuremap is used for IoC in C#. It can be hooked via two ways. Through configuration:

ObjectFactory.Initialize(x =>
{
      x.UseDefaultStructureMapConfigFile = true;
});

<StructureMap>
    <DefaultInstance PluginType="XXXXXXX, YYYYYY" PluggedType="AAAAA,BBBBB" Scope="PerRequest" />
</StructureMap>

And from code, like this:

ObjectFactory.Initialize(x =>
{
     x.UseDefaultStructureMapConfigFile = false;
     x.AddRegistry<StructureMapRegistry>();
});

HttpContextLifecycle cycle = new HttpContextLifecycle();
For<IDataRepository<MethodName>>().LifecycleIs(cycle).Use<MethodName>();

I want to know what are the pros and cons of both approaches.

Upvotes: 2

Views: 215

Answers (1)

CodeCaster
CodeCaster

Reputation: 151604

  • Configuration:
    • Pro: you can alter it without having to recompile.
    • Con: no static typing. A typo in a type or interface name can go unnoticed until runtime.
  • Code:
    • Pro: static typing. Not possible to compile with unknown or incompatible types.
    • Con: you can't alter it without having to recompile.

Upvotes: 3

Related Questions