ReviewMicrosoft N Layer App Sample, Part IV-IoC FTW

time to read 3 min | 489 words

Continuing my review of, an Official Guidance (shows up at: which people are expected to read and follow.

While investigating how they are implementing IoC, I found:

/// <summary>
/// Configure root container.Register types and life time managers for unity builder process
/// </summary>
/// <param name="container">Container to configure</param>
void ConfigureRootContainer(IUnityContainer container)
    // Take into account that Types and Mappings registration could be also done using the UNITY XML configuration
    //But we prefer doing it here (C# code) because we'll catch errors at compiling time instead execution time, 
    //if any type has been written wrong.

    //Register Repositories mappings
    container.RegisterType<IProductRepository, ProductRepository>(new TransientLifetimeManager());
    container.RegisterType<IOrderRepository, OrderRepository>(new TransientLifetimeManager());
    container.RegisterType<IBankAccountRepository, BankAccountRepository>(new TransientLifetimeManager());
    container.RegisterType<ICustomerRepository, CustomerRepository>(new TransientLifetimeManager());
    container.RegisterType<ICountryRepository, CountryRepository>(new TransientLifetimeManager());

    //Register application services mappings

    container.RegisterType<ISalesManagementService, SalesManagementService>(new TransientLifetimeManager());
    container.RegisterType<ICustomerManagementService, CustomerManagementService>(new TransientLifetimeManager());
    container.RegisterType<IBankingManagementService, BankingManagementService>(new TransientLifetimeManager());
    //Register domain services mappings
    container.RegisterType<IBankTransferDomainService, BankTransferDomainService>(new TransientLifetimeManager());

    //Register crosscuting mappings
    container.RegisterType<ITraceManager, TraceManager>(new TransientLifetimeManager());


Just to figure out how ridiculous this is, let me show you the entire infrastructure required to support IoC in this application:


Yes, they abstracted the Inversion of Control Container. I think that if you need to do that, it is pretty clear that you don’t really get what IoC is all about.

Maybe, if you are a framework, you need to abstract the container, but you don't need to do that if you are an application (which this is supposed to be) and you certainly don't write your own, that is why CommonServiceLocator is here.

Then again, the code is absolutely littered with things like:


So I guess that it is not really a surprise.

What is a surprise is that they are catching NullReferenceException. You are not supposed to catch this exception. This is an indication that you have some problem with your code, not that you have some invalid argument issue.

More posts in "Review" series:

  1. (22 Jul 2019) Part II
  2. (19 Jul 2019) Part I