ReviewMicrosoft N Layer App Sample, Part IV-IoC FTW

time to read 3 min | 489 words

Continuing my review of http://microsoftnlayerapp.codeplex.com/, an Official Guidance (shows up at: http://msdn.microsoft.com/es-es/architecture/en) 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:

image

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:

image

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. (03 Dec 2013) Getting started with LevelDB
  2. (20 Jul 2011) Microsoft N Layer App Sample, part X–Architecture for the Space Age
  3. (15 Jul 2011) Microsoft N Layer App Sample, part IX–Not Invented Here is FUN
  4. (13 Jul 2011) Microsoft N Layer App Sample, part VIII–CRUD is so 90s
  5. (11 Jul 2011) Microsoft N Layer App Sample, part VII–Data Access Layer is GOOD for you
  6. (08 Jul 2011) Microsoft N Layer App Sample, part VI–Single responsibility principle is for idiots and morons
  7. (06 Jul 2011) Microsoft N Layer App Sample, Part V–Cross Cutting is a fine line
  8. (04 Jul 2011) Microsoft N Layer App Sample, Part IV-IoC FTW
  9. (01 Jul 2011) Microsoft N Layer App Sample, Part III–Abstraction is as abstraction does
  10. (30 Jun 2011) Microsoft N Layer App Sample, Part II–getting lost in the architecture
  11. (29 Jun 2011) Microsoft N Layer App Sample, Part I
  12. (12 Oct 2009) GoGrid vs.Amazon EC2
  13. (12 May 2009) C# in Depth
  14. (02 Sep 2008) Hibernate Search in Action
  15. (04 Jun 2008) Umbrella project
  16. (04 Jun 2008) Mass Transit Samples
  17. (23 Aug 2005) iRiver H340