6

I am brand new to IoC and thus have been following the examples provided by Jeffery Palermo in his posts at http://jeffreypalermo.com/blog/the-onion-architecture-part-1/ and in his book hosted here https://github.com/jeffreypalermo/mvc2inaction/tree/master/manuscript/Chapter23

Most important to note is that I am not using a pre-rolled IoC container, mostly because I want to understand all the moving parts.

However, I am creating a windows service rather than an ASP.NET MVC webapp so I am little bogged down on the startup portion. Specifically, in the web.config he registers an IHttpModule implementation INSIDE the infrastructure project as the startup module and then uses a post-build event to copy the necessary dlls into the website directory to get around having a direct dependency in the web project itself.

I don't think I have this type of luxury in a true windows service, so how do I achieve something similar, should I have a small startup project which has dependencies to both the Infrastructure and Core, or is there another method to get around the compile-time restrictions of the windows service?

Thanks in advance.

kirps
  • 1,345
  • 2
  • 17
  • 28
  • Especially if you are brand new to IoC I would recommend using a well known (and tested!) IoC container. It's a noble approach to write one by yourself but in the long run you'll burn time each of the containers programmers have already invested. Regarding your question: what's wrong about having a reference to the IoC framework? – Zebi Jul 26 '11 at 04:57
  • @Zebi, the reference issue is not about referencing an IoC framework, but about referencing the infrastructure dll directly. The point of IoC seemed to be to remove these transitive dependencies. The startup of the web project seemed to take advantage of a loophole in the fact that the web.config can be used to configure a call to the infrastructure layer felt a bit of a cheat to me as to do it in a desktop app or service you wouldn't be able to use the same technique and would have to reference the infrastructure dll directly from your app or UI project, thus defeating the purpose of IoC. – kirps Jul 27 '11 at 14:24
  • The purpose of an IoC container is not to manage your assembly references. The purpose is to manage dependencies of classes. The IoC pattern itself has nothing to do with assemblies at all. – Zebi Jul 27 '11 at 15:45
  • @Zebi, but doesn't the reference imply dependency? – kirps Jul 27 '11 at 16:21
  • I added an answer to clarify. Please comment if this explains it to you. – Zebi Jul 27 '11 at 18:47
  • @Zebi - Thank you for the additional info, I'm still missing something, my main goal was to use the principles of IoC to create a project which follows Onion Architecture and limits my Nhibernate dependency etc. limited to my data access library. To me it feels like these methods tie my execution logic to my DAL by making my nhibernate bootstrapper part of the OnStart method. – kirps Jul 28 '11 at 01:27
  • @kirps let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1907/discussion-between-zebi-and-kirps) – Zebi Jul 28 '11 at 06:06

2 Answers2

3

Based on the tags of this question (c#) I'm assuming that you'll implement the Windows Service by deriving from ServiceBase. If so, the OnStart method will be your Composition Root - this is where you compose the application's object graph. After you've composed the object graph, composition is over and the composed object graph takes over.

In OnStop you can decommission the object graph again.

There's nothing stopping you from implementing the various components of the resolved object graph in separate assemblies. That's what I would do.

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • I'll have to dig in to your answer further today when I get free (this is for my side project) But to see if I understand, are you suggesting to pull the bootstrapper out into it's own assembly, or did I completely miss the point there? – kirps Jul 27 '11 at 14:17
  • Personally, I'd keep the bootstrapper in the assembly containing the ServiceBase-derived class and put everything else in one or more separate assemblies. – Mark Seemann Jul 27 '11 at 14:44
1

I think you missunderstood the role of an IoC framework.

To answer your question

but doesn't the reference imply dependency?

Yes it does, but on an other level. IoC is about dependencies between classes. Instead of using new Something() in your class you provide a constructor which requires all dependent interfaces. This way the class has no control which implementation is passed to it. This is inversion of control. The IoC Container is just an aid to help managing the dependencies in a nice manner.

Say you have a ICustomerNotificationService interface with an implementation like

public class MailNotificationService : INotificationService
{
    IMailerService _mailer;
    ICustomerRepository _customerRepo;
    IOrderRepository _orderRepo;

    public MailNotificationService(IMailerService mailer, 
                                   ICustomerRepository customerRepo, 
                                   IOrderRepository oderRepo)
    {
        // set fields...
    }

    public void Notify(int customerId, int productId)
    {
        // load customer and order, format mail and send.
    }
}

So if your application requests an instance of ICustomerNotificationServcie the container figures out which concrete implementations to take and tries to satisfy all dependencies the requested class has.

The advantage is that you can easily configure all dependencies in your bootstrapping logic and be able to change the behaviour of your application very easily.

For example when testing you start the application with an IMailerService implementation which writes the mails to a file and in production mode a real mail service is wired. This would not be possible if you newed up say a MailerService in your constructor instead of taking it as a parameter.

A good IoC container can handle much more, for you like lifetime management, singletons, scanning assemblies for Types you want to register and many more. We based our entire plugin system on Structure Map for example.

You may want to take a look at this blog article and its second part.

Zebi
  • 8,682
  • 1
  • 36
  • 42