[ProgClub list] What is IoC?

Stuart Laughlin stuart at bistrotech.net
Thu Oct 13 02:09:48 AEDT 2011

On Tue, Oct 11, 2011 at 7:15 AM, John Elliot <jj5 at progclub.org> wrote:
> I try to avoid the keyword 'new' in all of my (non-trivial) software. I
> create static factory methods on my concrete types to obviate the need for
> new-ing types (you call the factory method instead, and it fails if it has
> to *before* the new object is created, constructors on the other hand run
> after the instance has been created). I then wrap these factory methods in
> an abstract factory, concrete implementations of which defer to classes'
> factory methods. So I have AbstractFactory calls FactoryMethod calls new.
> AbstractFactory picks an implementation (e.g. debug or production),
> FactoryMethod validates parameters, new is private and receives
> instantiation state from its factory method which has validated that state.

What does that code look like? Reading your description I can't work
out whether Notifier's static CreateInstance() method would receive an
IMessageClient parameter, or if that method would obtain an instance
from the factory (in which case I can't work out how you get the
factory into Notifier).

	public abstract class BaseFactory {
		public abstract Notifier CreateNotifier();
		public abstract IMessageClient CreateMessageClient();
	public class ProductionFactory : BaseFactory {
		public override Notifier CreateNotifier ()
			// maybe I pass new EmailSender() here?
			return Notifier.CreateInstance();
		public override IMessageClient CreateMessageClient ()
			return EmailSender.CreateInstance();

	public class Notifier
		private readonly IMessageClient sender;

		private Notifier (IMessageClient sender)
			this.sender = sender;
		public static Notifier CreateInstance() {
			// I think I need an IMessageClient to pass to my constructor
			// (or would you not pass it in on the ctor?)
			// Do I get that from my factory or does it get passed in here?

		public void NotifyUsers (IEnumerable<User> users, string message)
			foreach (var user in users) {
				this.sender.Send (message, user);

You mentioned earlier that this factory/config scenario essentially
fills the role of an IoC container, and I would tend to agree.
However, so far it appears to me that an IoC container accomplishes
this in a more simple and elegant way, according to my tastes anyway.
I'll keep going with my explanation and see if you agree.



More information about the list mailing list