Tdd,Ioc

25
Introduction to TDD and CI Antonio Radesca

Transcript of Tdd,Ioc

Page 1: Tdd,Ioc

Introduction to TDD and CI

Antonio Radesca

Page 2: Tdd,Ioc

Test Driven Development

➲ Defined by Kent Beck in Test Driven Development By Examples

➲ Rules➲ Never write a single line of code unless

you have a failing automated test➲ Eliminate duplication

Page 3: Tdd,Ioc

Practices and Tool

➲ Nunit,MBUnit...➲ Resharper (Refactoring)➲ Gallio suite

Page 4: Tdd,Ioc

Refactoring➲ Process of rewriting code for improve

readability, semplification, performances and design

➲ “Refactoring” by Martin Fowler➲ There you can find more informations and

patterns➲ Automated from some tools: VS refactoring,

Resharper,Visual Assist...

Page 5: Tdd,Ioc

Some Examplesnamespace ERP.UnitTests.Repository.NHibernate{ [TestFixture] public class FixtureNHibernateClientRepository { [SetUp] public void SetupMethod() { TestUtilities obj=new TestUtilities(); obj.ExecuteQuery("DELETE FROM CLIENTS"); obj.ExecuteQuery(@"DBCC CHECKIDENT(Clients,RESEED,0)"); } [Test] public void SaveClient() { Client obj=new Client(); obj.NameSurname = "name"; obj.City = "city"; obj.Address = "Address"; ERP.Repository.NHibernate.ClientRepository objRep=new ClientRepository(); objRep.Save(obj); objRep.Commit(); Client tmp = objRep.Load(1); Assert.AreEqual(true, CompareClients(tmp, obj)); }

private bool CompareClients(Client tmp, Client obj) { if (tmp.ID == obj.ID && obj.City == tmp.City && tmp.NameSurname == obj.NameSurname && tmp.Address == obj.Address) return true; return false; }

}}

Page 6: Tdd,Ioc

Behaviour Driven Development

➲TDD + DDD

Page 7: Tdd,Ioc

BDD:Practices➲ A SubjectMatterExpert (typically a business

user) works with a BusinessAnalyst to identify a business requirement. This is expressed as a story using the following template

➲ As a Role

➲ I request a Feature

➲ To gain a Benefit ➲ The speaker, who holds the Role, is the

person who will gain the Benefit from the requested Feature.

Page 8: Tdd,Ioc

Mocks➲ Do you need to test something that is related

to other components?

Page 9: Tdd,Ioc

Rhino Mocks

➲ Created by Ayende➲ Simple, documented and updated➲ CODE...

Page 10: Tdd,Ioc

An Example

Page 11: Tdd,Ioc

Continuous Integration

➲ Extende and improve build automation➲ Prevent from integration problems➲ Examples:CruiseControl.NET,TFS,Draco.N

ET...

Page 12: Tdd,Ioc

CI:Practices➲ Maintain a Single Source Repository➲ Automate the Build➲ Make Your Build Self-Testing➲ Everyone Commits Every Day➲ Every Commit Should Build the Mainline on

an Integration Machine➲ Keep the Build Fast➲ Test in a Clone of the Production Environment➲ Make it easy to get the latest deliverables➲ Everyone can see the results of the latest

build➲ Automate Deployment

Page 13: Tdd,Ioc

Continuous Integration

Page 14: Tdd,Ioc

Introduction to IOC and AOP

Page 15: Tdd,Ioc

Inversion Of Control➲ Good software: High coesion,low coupled

MyWebPageC la s s

SqlServerDataPr…C la s sSq lServerD ataPro v ider

MyWebPageC la s s

I DataProviderI n te r fa c eI D ataPro v id er

Page 16: Tdd,Ioc

What is IOC➲ From Wikipedia: abstract principle describing an

aspect of some software architecture designs in which the flow of control of a system is inverted in comparison to the traditional architecture of software libraries.

➲ Instead of specifying a sequence of decisions and procedures to occur during the lifetime of a process, the user of an IoC framework writes the desired responses linked to particular events or data requests. External entities then take control over the precise calling order and additional maintenance that are to be carried out to execute the process.

Page 17: Tdd,Ioc

Example

MyWebPageC la s s

I DataProviderI n te r fa c e

DbFactoryC la s s

OracleDataProvi…C la s s

SqlServerDataPr…C la s s

I D ataPro v id er

Page 18: Tdd,Ioc

IOC

Page 19: Tdd,Ioc

➲ Hollywood Principle

➲ Don't call us, we'll call you.➲ High cohesion and low coupling

Page 20: Tdd,Ioc

Dependency Injection

➲ Dependency Injection (DI) in computer programming refers to the process of supplying an external dependency to a software component. It is a specific form of inversion of control where the concern being inverted is the process of obtaining the needed dependency.

➲ The object simply provides a property that can hold a reference to that type of service; and when the object is created a reference to an implementation of that type of service will automatically be injected into that property - by an external mechanism.

➲ See Martin Fowler for types...

Page 21: Tdd,Ioc

IOC Framework for .NET➲ Managed Extensibility Framework➲ Autofac➲ Castle MicroKernel/Windsor➲ ObjectBuilder➲ PicoContainer.NET➲ Puzzle.NFactory➲ Spring.NET➲ StructureMap➲ Ninject➲ Unity➲ NauckIT.MicroKernel

Page 22: Tdd,Ioc

Example➲ using System;➲ using System.Collections.Generic;➲ using System.Linq;➲ using System.Text;➲ using Castle.Core.Resource;➲ using Castle.Windsor;➲ using Castle.Windsor.Configuration.Interpreters;➲ using Microsoft.Practices.Unity;

➲ namespace ERP.Services➲ {➲ public interface IServicesFactory➲ {➲ T GetService<T>();➲ }

➲ public class ServicesFactory : IServicesFactory➲ {➲ private static➲ IWindsorContainer container;

➲ static ServicesFactory()➲ {➲ container = new WindsorContainer(➲ new XmlInterpreter(new ConfigResource("notification")));➲ }

➲ public T GetService<T>()➲ {➲ return container.GetService<T>();➲ }➲ }➲ }

Page 23: Tdd,Ioc

Aspect Oriented Programming

Page 24: Tdd,Ioc

What is an aspect?➲ Paradigm programming based on crosscuts

concepts➲ Examples:Logging,Transactions,Caching...➲ AOP Containers:Spring.NET,Castle

Windsor,Unity Application Block...

Page 25: Tdd,Ioc

Some Examplesusing System;using System.Collections.Generic;using System.Linq;using System.Text;using Castle.DynamicProxy;using Utils;

namespace ERP.Services.AOP{ //Classe che gestisce il logging instanziando oggetti generici public class LoggingManager<T> { public void Log<T>(string method) { ProxyGenerator generator = new ProxyGenerator();

T logger = generator.CreateClassProxy<T>(new LoggingInterceptor());

ReflectionHelper.CallMethod(logger,method); } }}