7156ccff4584964af3c3014623989859.ppt
- Количество слайдов: 58
ARC 310 Learning to Live with the Static Typing Fascist and the Dynamic Typing Fanboy in your Enterprise. . . James Crisp and Jim Webber, Thought. Works
Roadmap Platform Code affordances Tool support Extensibility Development Process Data access Metaprogramming Application development Integration
Platform Windows!. Net 3. 5 Maybe Linux, via Mono
Platform Anywhere Windows Linux Mac CLR JVM
Readability How concise and readable is the code? How much code will I have to maintain? How productive can I be?
Attributes class Person attr : id attr_accessor : name, : age end
"Flintoff")" src="https://present5.com/presentation/7156ccff4584964af3c3014623989859/image-7.jpg" alt="Named Parameters p = Person. new( : first_name => "Freddy", : last_name => "Flintoff")" />
Named Parameters p = Person. new( : first_name => "Freddy", : last_name => "Flintoff") p. update(: age => 72, : sex=>'m')
Default Property Implementation class Person { public string First. Name { get; set; }. . .
Meta Plumbing class Domain. Object def initialize(attributes = {}) attributes. each do |name, value| eval("@#{name} = value") end end
Duck Typing h = House. new( number => 20 ) p = Phone. new( number => 123456 ) p = h p. number >> 20
Type Inference var p = new Person { Last. Name = "Smith"};
Who needs Duck Typing? interface INumbered { int Number { get; } } class House : INumbered. . . class Phone : INumbered. . . INumbered phone; INumbered house = new House(); phone = house; phone. Number
Data LINQ and Entity Framework Castle project Active record for. Net Builds on NHibernate too!
Design Time
Generate and Use the Database Person. Data. Context pdc = new Person. Data. Context(); pdc. Create. Database(); var adults = from p in pdc. Persons where p. Age > 18 select p; foreach (Person adult in adults) { Console. Write. Line(adult. First. Name + " " + adult. Last. Name); }
Data Active Record ships with Rails Good for green field web apps Objects and properties mapped 1: 1 to tables and columns Simple API for searching and saving Can use underlying platform – eg, Hibernate or NHibernate
Lambda Functions What are Lambda functions? From the functional programming community Who? Think: anonymous inline methods C# 2. 0 anonymous delegates with nicer syntax
Pass a function into a collection List
Lambda Functions codgers = people. find {|p| p. age > 30} codgers. each {|c| print c. name }
Metaprogramming & DSLs Method Missing DSLs Code generation at run time
Method Missing If a method is not defined, falls through to ‘method_missing’. Active. Record (part of Rails) uses this to “generate” properties on domain objects at run time. Allows DSLs like : Person. find_by_name_and_age('Jim', 84)
Code Generation at Run Time Create new properties, methods and classes on the fly Redefine, wrap and rename methods Eval() a string, and it will be code
Code Generating DSL class Geek < Active. Record: : Base has_many : laptops has_one : girlfriend jim = Geek. new(: name => 'Jim' ) laptop = Laptop. new(: brand=> 'dell') jim. laptops << laptop
r. Spec DSL describe Geek do before(: each) do @geek = Geek. new end it "should have no laptops initially" do @geek. laptops. count. should == 0 end. .
Autogen and Metaprogramming Can generate useful code from metadata at compile time E. g. Domain objects from database schemas E. g. Service proxies from WSDL Etc Attributes for metaprogramming And the reflection APIs if I have to. . .
DSLs Some support for fluent interfaces in NUnit etc Assert. That(. . . ); Assert. Is. Not. Null(. . . ); Can create our own DSLs by using cunning class name conventions E. g. n. Behave for BDD Like TDD but focussed on behaviour rather than implementation
n. Behave DSL override public void Specify() { Given(new Ruby. Programmer()). When(new Building. Proper. Software()). Then(new Ruby. Programmer. Should. Be. Fired()); } Tooling available to turn this code into development stories for x. Behave
Extensible Type System All classes are open and can be extended Eg, Rails extends Ruby's number class: >> 5. days. from_now => Sun Aug 05 14: 28: 12 +1000 2007 Existing methods can be removed, wrapped or renamed Frameworks often designed around reusable Mixins rather than inheritance
> =>" src="https://present5.com/presentation/7156ccff4584964af3c3014623989859/image-30.jpg" alt="Adding new methods class String def url? self. starts_with? "http: //" end >> =>" />
Adding new methods class String def url? self. starts_with? "http: //" end >> => "hi". url? false "http: //jamescrisp. org". url? true
Extensible Type System We’ve always had interfaces and abstract classes from the underlying. Net framework Now we have extension methods too. . .
Defining Extension Methods namespace Jim. Webber { public static class Web. Uri. Validator { public static bool Is. Valid. Uri(this string str) { return str. To. Lower(). Starts. With("http"); } } }
Using Extension Methods using Jim. Webber; class Program { static void Main(string[] args) { string s ="http: //jim. webber. name"; bool b = s. Is. Valid. Uri(); } }
Testing Unit Testing TDD – NUnit BDD – n. Behave n. Mock – Interfaces easy, classes slightly trickier
NUnit [Test. Fixture] public class Person. Test { [Set. Up] public void Given. APerson. Exists() {. . } [Tear. Down] public void Person. Should. Be. Removed() {. . } [Test] public void Person. Should. Be. Older. Than. Zero() {. . . Assert. That(p. Age > 0); } }
n. Mock Example Interface. To. Be. Mocked a. Mock = mocks. New. Mock
Testing TDD Unit test framework part of standard libraries BDD r. Spec, r. Behave Mocks Mocha, r. Mock, Flex. Mock, etc
Unit Test Example def test_person_is_called_james do assert_equal 'James', @person. name end OR after a little bit of meta programming test 'person is called James' do assert_equal 'James', @person. name end
Mocha Example p = mock('person') p. stubs(: age). returns(26) p. expects(: name). returns('James')
Tool Support Visual Studio Intellisense, debugger, Power. Shell, etc Continuous Integration Cruise. Control. Net and friends Build Process NAnt, MSBuild, NMaven, etc
Power. Shell PS C: UsersJim Jim Webber PS C: UsersJim jim webber PS C: UsersJim Webber> $name = "Jim Webber" Webber> $name. To. Lower() Webber> PS C: UsersJim Webber> ([xml](new-object System. Net. Web. Client). Download. String("http: //jim. webber. name/feeds/atom. aspx") ). feed. title World Wide Webber
Tool Support IDE Visual Studio with Iron Ruby Intelli. J, e-Edit, Text. Mate provide syntax highlighting and script support. Build Process: Rake Continuous Integration Cruise. Control. rb and friends
Console C: ruby>ruby script/console >> p = Person. new( : name => 'Jim' ) => #
Gems C: ruby> gem install mocha Bulk updating Gem source index for: http: //gems. rubyforge. org Successfully installed mocha-0. 5. 3 Installing ri documentation for mocha 0. 5. 3. . . Installing RDoc documentation for mocha 0. 5. 3. . .
NMaven Warning: NMaven currently immature Apache Incubator project Supports repositories for dependencies Across the Internet Plugs into the standard Maven build cycle Will have VS integration Extensible through Mono-based plugins Also could use Ivy for dependency management. . .
Web Apps RAILS Rails is the most famous and popular Ruby framework Rails provides (among other things): Neat MVC framework and route mapping Template based views Domain focussed business layer Active Record and DSLs for persistence Easy AJAX and Web 2. 0 support Auto-generated code (write time and run time) Fast change cycle (edit file -> refresh page) Plug-ins for code re-use
Web Apps ASP. Net Plus new ASP. Net AJAX functionality Combine ASP. Net with Entity Framework for Rails-like functionality Silverlight For richer client functionality
Rich Client Most Ruby dev is Rails and Web 2. 0 There are GTK / Gnome bindings for Ruby and some rich client Ruby apps. WPF under. NET
Rich Client WPF Clear separation of markup and business logic Whizzy acceleration and goodness from Direct. X 10 Also the older Win. Forms stuff is available
Integration WCF WS-* SOAP, WSDL, WS-Sec. Pol, WS-Trust, WS -Federation, WS-Coordination. WSAtomic. Transaction, WS-Kitchen. Sink. . . REST Support URI templates, [Web. Get], etc Enterprise-y stuff too Queues
Integration REST-centric Web services are produced like web pages, using same framework and routing Message Queues using Reliable-Msg or underlying JMS or MSMQ WS-* support SOAP 4 R Use underlying platform for WS-*, eg, WCF or Java frameworks like XFire
XML and Friends. Net has Xml. Serializer (and friends) Dom, XPath, templates, etc VB has XML literal support. . . Dim x As XElement =
C# Not Tightly Coupled to XML var x = new XElement("Date"); x. Add(new XElement("Year", "2006")); x. Add(new XElement("Month", "01")); x. Add(new XElement("Day", "01"));
XML and Friends XPath and Document Object Model well supported To generate complex XML (eg, LIXI), the best way is templating using Erb All objects have to_xml method with serialises properties to xml XML Builder is nice to work with and leverages Ruby's flexible method_missing
XML Builder >> ? > >> >> >> builder. date { builder. year "2006" builder. month "01" builder. day "01" }
Coexisting in the enterprise When to go Ruby versus when to use C# and. Net? Enterprise “heavy lifting? ” Cross-language enterprise development? And what about mixing C# and Iron. Ruby. . . ?
ARC 310 Questions? Ask James and Jim!
ARC 310 Evaluation Forms Don’t Forget!


