Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 101 Struts University Series. About Ted Husted Lead author, Struts in Action Co-Author, JUnit in Action Member, Apache Software Foundation Member,

Similar presentations


Presentation on theme: "Spring 101 Struts University Series. About Ted Husted Lead author, Struts in Action Co-Author, JUnit in Action Member, Apache Software Foundation Member,"— Presentation transcript:

1 Spring 101 Struts University Series

2 About Ted Husted Lead author, Struts in Action Co-Author, JUnit in Action Member, Apache Software Foundation Member, Struts PMC and iBATIS PMC Working developer

3 Refactoring with Spring Inside Spring

4 How can we use Spring to simplify development? Working example without Spring Refactor to use Spring

5 Client story Manage simple types We want to edit the descriptions of items that appear in our drop-down lists. We want to add new items to the drop-down lists.

6 Client conversation Lists are drawn from simple data tables Seven lists Two to five columns Three to 70+ items Doesn't want to delete (right now) Could use a datagrid with in-place editing Must be idiot-proof (e.g. validate input)

7 Development issues Different queries for different tables Different validations for different columns Some columns can be blank, some cannot Most columns are strings, but some are integers or dates

8 Things to Avoid Seven controls for seven tables

9 Likely solution Dynamic datagrid Helper classes

10 Class management One interface One base class Seven instant classes

11 Datagrid markup

12 Code-behind public interface ITypesHelper : IViewHelper { string EditText { get; set; } string QuitText { get; set; } string SaveText { get; set; } string ListCommand { get; set; } string SaveCommand { get; set; } string DataKeyField { get; set; } IList DataFields { get; set; } IList DataLabels { get; set; } int BindColumns (DataGrid grid, int i); void DataSource (DataGrid grid); void DataBind (DataGrid grid); void Save (string key, ControlCollection controls) ; }

13 BindColumns method public virtual int BindColumns (DataGrid grid, int i) { grid.DataKeyField = DataKeyField; i = BindEditColumn (grid, i); int colCount = DataFields.Count; int lblCount = DataLabels.Count; for (int c = 0; c < colCount; c++) { string column = DataFields [c] as string; string label = (lblCount < c) ? column : DataLabels [c] as string; i = View.BindColumn (grid, i, label, column) ; } return i; }

14 TypesHelper instance private class CountyHelper : TypesHelper { public CountyHelper () { ListCommand = "County.List"; SaveCommand = "County.Save"; DataKeyField = "CountyKey"; IList dataFields = new ArrayList (2); dataFields.Add ("CountyName"); dataFields.Add ("EclsOffice"); DataFields = dataFields; IList dataLabels = new ArrayList (2); dataLabels.Add ("County Name"); dataLabels.Add ("ECLS Office"); DataLabels = dataLabels;} }

15 Code-behind private const string cmd_County = "County"; private const string cmd_Event = "Event"; private const string cmd_Program = "Program"; private const string cmd_Status = "Status"; private const string cmd_Staff = "Staff"; private const string cmd_Ticket = "Ticket"; private void Select_Init_Types (ListItemCollection items) { items.Add (cmd_County); items.Add (cmd_Event); items.Add (cmd_Program); items.Add (cmd_Staff); items.Add (cmd_Status); items.Add (cmd_Ticket); }

16 Code-behind private ITypesHelper dgList_Helper_Init (string category) { dgList_Helper = null; switch (category) { case cmd_County: dgList_Helper = new CountyHelper (); break; //... case cmd_Ticket: dgList_Helper = new TicketHelper (); break; default: throw new NotImplementedException (); } return dgList_Helper; }

17

18

19 TypesHelper instance private class CountyHelper : TypesHelper { public CountyHelper () { ListCommand = "County.List"; SaveCommand = "County.Save"; DataKeyField = "CountyKey"; IList dataFields = new ArrayList (2); dataFields.Add ("CountyName"); dataFields.Add ("EclsOffice"); DataFields = dataFields; IList dataLabels = new ArrayList (2); dataLabels.Add ("County Name"); dataLabels.Add ("ECLS Office"); DataLabels = dataLabels;} }

20 CountyTypesHelper County.List County.Save CountyKey CountyName EclsOffice County Name ECLS Office

21 Code-behind private const string cmd_County = "County"; private const string cmd_Event = "Event"; private const string cmd_Program = "Program"; private const string cmd_Status = "Status"; private const string cmd_Staff = "Staff"; private const string cmd_Ticket = "Ticket"; private void Select_Init_Types (ListItemCollection items) { items.Add (cmd_County); items.Add (cmd_Event); items.Add (cmd_Program); items.Add (cmd_Staff); items.Add (cmd_Status); items.Add (cmd_Ticket); }

22 TypesSelectItem County CountyTypesHelper

23 Code-behind private ITypesHelper dgList_Helper_Init (string category) { dgList_Helper = null; switch (category) { case cmd_County: dgList_Helper = new CountyHelper (); break; //... case cmd_Ticket: dgList_Helper = new TicketHelper (); break; default: throw new NotImplementedException (); } return dgList_Helper; }

24 Factory.GetObject private ITypesHelper dgList_Helper_Init (string category) { dgList_Helper = Factory.GetObject(category) as ITypesHelper; if (dgList_Helper==null) throw new NotImplementedException (); return dgList_Helper; }

25 CountyTypesHelper County.List County.Save CountyKey CountyName EclsOffice County Name ECLS Office

26 TypesSelectItem County CountyTypesHelper

27 Factory.GetObject private ITypesHelper dgList_Helper_Init (string category) { dgList_Helper = Factory.GetObject(category) as ITypesHelper; if (dgList_Helper==null) throw new NotImplementedException (); return dgList_Helper; }

28 Spring Spring is a generic object factory Replaces programmatic singletons Replaces service locator stubs Spring provides a smart object repository Most often configured via XML documents Each object has a logical ID (not a classname) Resolves dependencies between objects

29 TypesSelectItem County CountyTypesHelper

30 Spring Spring decouples specific datatype dependency from program logic Encourages interface-driven development Encourages reuse of shared objects Centralizes configuration of shared and custom logic

31 Dependency Injection Spring objects define dependencies by a constructor, a factory method, or properties. The Spring container injects dependencies when it creates the object

32 Inversion of Control Constructor Object appear without dependencies Coupled to type Dependency controlled by caller Container Objects appear with dependencies Coupled to interface Dependency controlled by container

33 Inversion of Control Container the inverse of using new operator or Service Locator pattern Hence: Inversion of Control

34 IOC 123 Types of IoC: Type 1 – Interface Injection (Avalon) Type 2 – Constructor Dependency Injection Type 3 – Setter Dependency Injection "The question is: What aspect of control are we inverting?". http://martinfowler.com/articles/injection.html

35 Spring Lifecycle Container instantiated Configuration validated Singletons created, properties set Object dependencies set by properties or constructor arguments. Dependencies provided when object is created

36 Spring Lifecycle Each dependency (property or constructor argument) is a value, or a reference to a container-managed object Values must convert to target datatype Spring will convert strings to any built-in type: int, long, string, bool, et al. Values may define IList, IDictionary, KeyValue, and Set collection types.

37 IObjectFactory Instantiates, configures, and manages a set of objects. Managed objects typically collaborate Dependencies between themselves Collaborator dependencies evident in configuration

38 IApplicationContext Extends IObjectFactory Application-centric features Localization using resource files Event-propagation Resource-loading

39 Bootstrapping IApplicationContext factory = ConfigurationSettings.GetConfig("s pring/context") as IApplicationContext;

40 .NET standard configuration <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" <context type="Spring.Context.Support.XmlApplicationContext, Spring.Core">

41 spring/objects.xml ?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE objects PUBLIC "-//SPRING//DTD OBJECT//EN" "http://www.springframework.net/dtd/spring-objects.dtd">http://www.springframework.net/dtd/spring-objects.dtd

42 Object Definitions Object definitions describe Objects managed by an IObjectFactory or IApplicationContext.

43 Object Definitions Object definitions contain: Object type Container modes prototype or singleton autowiring dependency checkin initialization and destruction methods Values to set in the newly created object. Collaborators (aka dependencies)

44 "Dependency is the leading problem in software development at all levels." -- Kent Beck

45 Struts University Series


Download ppt "Spring 101 Struts University Series. About Ted Husted Lead author, Struts in Action Co-Author, JUnit in Action Member, Apache Software Foundation Member,"

Similar presentations


Ads by Google