Tdd,Ioc

Post on 10-May-2015

1.007 views 1 download

Tags:

Transcript of Tdd,Ioc

Introduction to TDD and CI

Antonio Radesca

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

Practices and Tool

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

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...

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; }

}}

Behaviour Driven Development

➲TDD + DDD

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.

Mocks➲ Do you need to test something that is related

to other components?

Rhino Mocks

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

An Example

Continuous Integration

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

ET...

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

Continuous Integration

Introduction to IOC and AOP

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

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.

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

IOC

➲ Hollywood Principle

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

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...

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

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>();➲ }➲ }➲ }

Aspect Oriented Programming

What is an aspect?➲ Paradigm programming based on crosscuts

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

Windsor,Unity Application Block...

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); } }}