eric moreau: fluent interfaces

13
Ajouter des interfaces « fluent » à vos classes .Net Eric Moreau, MVP Moer inc. [email protected] www.moer.ca

Upload: msdevmtl

Post on 18-Jul-2015

151 views

Category:

Technology


0 download

TRANSCRIPT

Ajouter des interfaces « fluent » à vos classes .Net

Eric Moreau, MVP

Moer inc.

[email protected]

www.moer.ca

Interfaces « Fluent »

• Terme viendrait de Eric Evans et Martin Fowler

• Définition Wikipédia• « implementation of an object oriented API that aims to provide more

readable code »

• « normally implemented by using method cascading (concretely method chaining) to relay the instruction context of a subsequent call »

Interfaces « Fluent »

• Vous les utilisez déjà depuis longtemps!

• Un premier exemple

Dim s As String = "Eric Moreau"s = s.Trim()s = s.ToUpper()s = s.Replace("R", "z")

string s = "Eric Moreau";s = s.Trim();s = s.ToUpper();s = s.Replace("R", "z");

Dim s2 As String = "Eric Moreau".Trim().ToUpper().Replace("R", "z")

string s2 = "Eric Moreau".Trim().ToUpper().Replace("R", "z");

Interfaces « Fluent »

• LINQ est « fluent » depuis le début

• Voici un 2e exemple

Dim fileList = dir.GetFiles("*.*").Where(Function(f) f.Name.StartsWith("odbc")).OrderBy(Function(f) f.FullName)

var fileList = dir.GetFiles("*.*").Where(f=> f.Name.StartsWith("odbc")).OrderBy(f => f.FullName);

Interfaces « Fluent » - Alternatives

• Object Initializer• Propriétés seulement

• À l’initialisation seulement

• With … End With (VB only)• Semble similaire

• Vraiment pas « fluent »

Dim emp2 As Employee = New Employee With {.Name = "Eric Moreau",.Dob = New DateTime(1970, 5, 28),.HireDate = New DateTime(2007, 2, 1),.Salary = 123456,.BossName = "Pointy-Haired Boss"

}

Dim emp3 As Employee = New EmployeeWith emp3

.Name = "Eric Moreau"

.Dob = New DateTime(1970, 5, 28)

.HireDate = New DateTime(2007, 2, 1)

.Salary = 123456

.BossName = "Pointy-Haired Boss"End With

Interfaces « Fluent » - Ma recette

• Ma recette s’intègre à une classe qui existe déjà sans changer son comportement actuel

• Étape 1 – Partez d’une classe régulière

Public Name As StringPublic Dob As DatePublic HireDate As DatePublic Salary As Int32Public BossName As String

Interfaces « Fluent » - Ma recette

• Étape 2 – Créez une interface (fonction retourne interface)

Public Interface IEmployeeFluent

Function OfName(ByVal pName As String) As IEmployeeFluentFunction AsOf(ByVal pDate As Date) As IEmployeeFluentFunction BornOn(ByVal pDate As Date) As IEmployeeFluentFunction SetSalary(ByVal pSalary As Int32) As IEmployeeFluentFunction ReportTo(ByVal pName As String) As IEmployeeFluent

End Interface

Interfaces « Fluent » - Ma recette

• Étape 3 – Implémentez l’interfacePublic Function OfName(ByVal pName As String) As IEmployeeFluent Implements IEmployeeFluent.OfName

Name = pNameReturn Me

End Function

Public Function AsOf(ByVal pDate As Date) As IEmployeeFluent Implements IEmployeeFluent.AsOfHireDate = pDateReturn Me

End Function

Public Function BornOn(ByVal pDate As Date) As IEmployeeFluent Implements IEmployeeFluent.BornOnDOB = pDateReturn Me

End Function

Interfaces « Fluent » - Ma recette

• Étape 4 – Utilisez

Dim emp2 As IEmployeeFluent = New Employee()MessageBox.Show(emp2.

OfName("Eric Moreau").AsOf(New DateTime(2007, 2, 1)).BornOn(New DateTime(1970, 5, 28)).SetSalary(123456).ReportTo("Pointy-Haired Boss").ToString())

Interfaces « Fluent » - Ma recette

• Mais d’habitude on n’aime même pas créer une instance

• On ajoute donc une méthode d’initialisation à notre objet

Public Shared Function Hire() As IEmployeeFluentReturn New Employee

End Function

Interfaces « Fluent » - Ma recette

• Utilisez la méthode d’initialisation

Dim emp As IEmployeeFluent = Employee.Hire().OfName("Eric Moreau").AsOf(New DateTime(2007, 2, 1)).BornOn(New DateTime(1970, 5, 28)).SetSalary(123456).ReportTo("Pointy-Haired Boss")

MessageBox.Show(emp.ToString())

En conclusion

• Simple

• Meilleure expérience d’utilisation

• « fit » bien avec les nouveaux APIs comme LINQ

• Article de mars 2014• http://emoreau.com/Entries/Articles/2014/03/Net-fluent-interface.aspx

Ajouter des interfaces « fluent » à vos classes .Net

Eric Moreau, MVP

Moer inc.

[email protected]

www.moer.ca