Saturday, 29 August 2009

Polymorphic Application Integration End Points

From now on, one of absolute “must-haves” for any software engineering effort is, to create polymorphic AI EPs.  Sounds fancy, but it’s not.

All it really is, is dependency injection or IoC, not at the unit level, but at the application-integration level.  At the unit level, for example, if you create a domain class which talks to a repository object, then you may swap out the repository for a mock repository during an automated unit test.

At the AI level, this is where you conceptually and logically separate out the application from other services or applications.  e.g., whenever you use the SmtpClient class, you’re binding your code to the concrete implementation of an SMTP end point.  SMTP is an end point, so are web services and WCF services.  In fact, anything outside your domain of control. The database is also integration, hence, we often have database abstraction layers within architectures that are meant to abstract the data-storage technology from the code (ideally we could change the RDBMS from SQL Server to Oracle without changing any code)

Here are some example application integration end points AIEP:

  1. Currency convert web service
  2. Email/SMTP service
  3. Database engine
  4. Exception Reporting service
  5. A web-browser (it’s the GUI but it helps to think of it as integration)
  6. SMS messaging service
  7. Logging service
  8. MSMQ messaging

All these endpoints are about communicating beyond your application boundary.  By loosely coupling your code to AIEPs, it means you have better ability to test your code in your development environment.

If you have ever accidentally sent out emails to real users from your test environment, then Polymorphic AIEPs will enable you to swap out your SmtpClient for MockSmtpClient.

For example:

public interface ISmtpClientEP{

void Send(string text);

}

 

public class SmtpClientEP : ISmtpClientEP{

public void Send(string text){

// instantiate the real smtpclient and send the message

}

}

 

public class TraceClientEP : ISmtpClientEP{

public void Send(string text){

// send your text to anywhere!  e.g. a text file in a local directory?

}

}

 

Then if you use an IoC Container, such as Structure Map, then you can use this from the client code like this:

ObjectFactory.GetInstance<ISmtpClientEP>().Send(“hello world”);

 

But remember on application start up to link structure map to the concrete implementation of the class you want to use:

ObjectFactory.Initialize(x =>
{

    x.ForRequestedType<ISmtpClientEP>()
      .CacheBy(StructureMap.Attributes.InstanceScope.PerRequest)
      .TheDefaultIsConcreteType<SmtpClientEP >();

}

So what have we done here?  We have utilised Structure Map to specify what the concrete implementation of ISmtpClientEP we wish to use.

Now if you have use conditional compilation arguments you can do this:

#IF(DEBUG)

ObjectFactory.Initialize(x =>
{

    x.ForRequestedType<ISmtpClientEP>()
      .CacheBy(StructureMap.Attributes.InstanceScope.PerRequest)
      .TheDefaultIsConcreteType<TraceClientEP >();  // use the trace end point for debug

}

#ELSE

ObjectFactory.Initialize(x =>
{

    x.ForRequestedType<ISmtpClientEP>()
      .CacheBy(StructureMap.Attributes.InstanceScope.PerRequest)
      .TheDefaultIsConcreteType<SmtpClientEP >();  // real one for release compilation!

}

#ENDIF

 

So polymorphic AIEPs utilise DI (Dependency Injection) to enable you to change (polymorph) your concrete implementations.  This is just like automated-unit testing (TDD), except, in my view TDD can be a large overhead, so my hybrid solution is to use DI for Application Integration End Points and if you want, you can hard-wire you concrete implementations of other classes (classes that are internal to your app), without worrying about affecting other systems or services when you hit Debug.

Tuesday, 25 August 2009

StopBeat.com and PointAngle spam and Intela

Please note that bingo spam from the domain StopBeat.com and PointAngle.com come from Intela +44 (0) 207 849 3419 / +1.303.473.0000

http://intela.com/contact.php 

Moan at them.