What's new in AVR 12.0 and VS 2013

41
bring IBM i RPG assets forward 1 © 2014 by ASNA. All rights reserved. New language features, a new VS, and some interesting DataGate features lurk! What’s new with ASNA Visual RPG and DataGate Presented by Roger Pence

description

ASNApalooza 2014 session: What's new in AVR 12.0 and VS 2013

Transcript of What's new in AVR 12.0 and VS 2013

Page 1: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 1© 2014 by ASNA. All rights reserved.

New language features, a new VS, and some interesting DataGate features lurk!

What’s new with ASNA Visual RPG

and DataGate

Presented by Roger Pence

Page 2: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 2© 2014 by ASNA. All rights reserved.

AVR for .NET 12.0 Overview

• AVR for .NET 12.0 works with Visual Studio 2013• Visual Studio 11.2 works with Visual 2012• Formally, this means that anything older than AVR 11.2

and Visual Studio 2012 is not actively supported!• An alternative to spending $$ on upgrading to

Visual Studio 2013• Use the Visual Studio Isolated Shell

http://msdn.microsoft.com/en-us/library/bb685691.aspx

Page 3: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 3© 2014 by ASNA. All rights reserved.

AVR 11.x/AVR 12.0 coexistence• Visual Studio versions 2012 and 2013 can be

installed on the same PC• So can AVR 11.2 and AVR 12.0, but…• After installing AVR 11.0, recompiled AVR 11.2 apps will

need to be deployed against the AVR 12.0 runtimes• If you have substantial 11.2 apps and don’t want to

upgrade your runtimes, consider using a VM• Windows 8 has Hypervisor Virtual Machine built in

Page 4: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 4© 2014 by ASNA. All rights reserved.

Upgrade tips

• AVR 12.0 installs over AVR 11.2, but earlier AVR versions need to be physically removed before installing AVR 12.0• Install Visual Studio 2013 (or the Isolated Shell)

before installing AVR 12.0• Do all Windows updates before installation• AVR for .NET examples are a separate install—don’t

miss them on the

Page 5: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 5© 2014 by ASNA. All rights reserved.

Just one more thing about Visual Studio. If it makes you feel any better…

Page 6: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 6© 2014 by ASNA. All rights reserved.

Visual Studio “14” is right around the corner!

Page 7: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 7© 2014 by ASNA. All rights reserved.

New or overlooked AVR language features

• Conditional expressions• Automatic properties• BegUsing/EndUsing block• Program described memory files

Page 8: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 8© 2014 by ASNA. All rights reserved.

Conditional expressions

• Conditional expressions return one of two values depending on the value of a *Boolean expression• This is sometimes called a ternary operator• Where “ternary” means “composed of three items”• In this case, the three things are a test, a true result and

a false result• Used appropriately, conditional expressions can

make your code easier to understand• Conversely, you can also brew yourself a mess with

conditional expressions if you aren’t careful!

Page 9: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 9© 2014 by ASNA. All rights reserved.

If Status = “X”Tax = 0

ElseTax = ApplyTaxRate(Amount)

EndIf

Tax = (Status = “X”) ? 0 : ApplyTaxRate(Amount)

Conditional expressions distill these five lines

down to this one line

Page 10: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 10© 2014 by ASNA. All rights reserved.

Tax = (Status = “X”) ? 0 : ApplyTaxRate(Amount)

var = (Boolean test) ? True value : False value

The Conditional Expression general syntax is:

Variable to receive results of the conditional expression.

Page 11: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 11© 2014 by ASNA. All rights reserved.

Tax = (Status = “X”) ? 0 : ApplyTaxRate(Amount)

var = (Boolean test) ? True value : False value

The Conditional Expression general syntax is:

A Boolean test that results in a true or false value.

Page 12: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 12© 2014 by ASNA. All rights reserved.

Tax = (Status = “X”) ? 0 : ApplyTaxRate(Amount)

var = (Boolean test) ? True value : False value

The Conditional Expression general syntax is:

The value to assign to the variable if the test is true.

Page 13: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 13© 2014 by ASNA. All rights reserved.

Tax = (Status = “X”) ? 0 : ApplyTaxRate(Amount)

var = (Boolean test) ? True value : False value

The Conditional Expression general syntax is:

The value to assign to the variable if the test is true.

Page 14: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 14© 2014 by ASNA. All rights reserved.

What do you think?

•Which would you rather debug? • Five lines or one line?

• Jeff Atwood, at StackOverflow says,

• Conditional expressions provide a way for you to provide a tested assignment quickly and simply• Conditional Expressions, used wisely, add to the

readability of your code.

If you can’t get away with no code, the next best thing is to start with brevity.

Page 15: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 15© 2014 by ASNA. All rights reserved.

DclFld i Type(*Integer4)DdlFld b Type(*Boolean)DclFld b2 Type(*Boolean)

b = *Falseb2 = *False

i = b2 ? 3.0 : b ? 4 : 5.0 * 9

What is the value of i?

Page 16: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 16© 2014 by ASNA. All rights reserved.

First, let’s add parenthesesi = (b2) ? 3.0 : (b) ? 4 : 5.0 * 9

What is the value of i?

Page 17: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 17© 2014 by ASNA. All rights reserved.

First, let’s add parenthesesi = (b2) ? 3.0 : (b) ? 4 : 5.0 * 9

Then solve the right-most part first…i = (b2) ? 3.0 : (b) ? 4 : 5.0 * 9

Note: conditional expressions are rightassociative—that is the right-most test is evaluated first.

What is the value of i?

Page 18: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 18© 2014 by ASNA. All rights reserved.

(b) ? 4 : 5.0 * 9

Is b true? Nope. So this part resolvesto 5.0 * 9. Or 45.

What is the value of i?

Page 19: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 19© 2014 by ASNA. All rights reserved.

Now it’s a little easier to read/i = (b2) ? 3.0 : 45

Is b2 true. Nope. i’s value is 45.

What is the value of i?

Page 20: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 20© 2014 by ASNA. All rights reserved.

If (b2) i = 3.0

ElseIf (b) i = 4Else i = 45 EndIf

Or (more simply stated?)…

Page 21: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 21© 2014 by ASNA. All rights reserved.

Conditional expressions

• From • Write clearly, don’t be too clever• Write clearly, don’t sacrifice clarity for “efficiency”

• Use conditional expressions where they add clarity, but don’t get carried away!

Page 22: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 22© 2014 by ASNA. All rights reserved.

If you need to assign a true/false value, don’t forget this chestnut:

MyControl.Visible = (TaxStatus = “T”)

No need for an if statement here! The Boolean test resolves to true or false.

A conditional footnote…

Page 23: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 23© 2014 by ASNA. All rights reserved.

Automatic properties

•Most of us have created classes with AVR that need to surface properties. • To do so, you could simply use a public field, but

public fields won’t data bind when using a strongly-typed collection with your class.

Page 24: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 24© 2014 by ASNA. All rights reserved.

Publishing a public field—won’t data bind!DclFld CMCUSTNO Type(*Integer4) Access(*Public)

To do it properly takes a nine lines of code! DclFld _CMCustNo Type( *Packed ) Len( 9,0 ) BegProp CMCustno Type( *Packed ) Len( 9,0 ) Access( *Public ) BegGet LeaveSr _CMCustNo EndGet BegSet _CMCustNo = *PropVal EndSet EndProp Show class

Private backing field

Public property name and type

Getter

Setter

Page 25: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 25© 2014 by ASNA. All rights reserved.

There has to be a better way!

Page 26: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 26© 2014 by ASNA. All rights reserved.

Borrowing from C#

• C# solves this problem with “automatic properties”

public int Customer { get; set; }

• Under the covers, the compiler generates the backing field. • Succinct. Direct. And readable!

Page 27: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 27© 2014 by ASNA. All rights reserved.

Automatic properties in AVRInstead of doing this:

DclFld _CMCustNo Type( *Packed ) Len( 9,0 )

BegProp CMCustno Type( *Packed ) Len( 9,0 ) Access( *Public )

BegGet

LeaveSr _CMCustNo

EndGet

BegSet

_CMCustNo = *PropVal

EndSet

EndProp

The new DclProp operation code lets you do this:

DclProp CMCustNo Type(*Integer4) Access(*Public)

Show class

Page 28: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 28© 2014 by ASNA. All rights reserved.

87% less code!

Page 29: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 29© 2014 by ASNA. All rights reserved.

Publishing a read-only property

DclProp CMCustNo Type(*Integer4) Access(*Public) + SetAccess(*Private)

Automatic properties add the SetAccess and GetAccess keywords, otherwise they use the same keywords as DclFld/BegProp.

Page 30: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 30© 2014 by ASNA. All rights reserved.

Automatic properties

• Let you specify entity-type classes with substantially less code• Use the new SetAccess and GetAccess keywords to

restrict get or set access•Work in all databinding scenarios•Make your classes much easier to read and create

Page 31: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 31© 2014 by ASNA. All rights reserved.

Using iDisposable objects safely• .NET is a garbage-collected language—there is

usually no need for explicit object disposal• For classes that use pure managed this is great• You’ll rarely need to write a class that needs explicit

object disposal—the GC does that work for you• But, any class that uses Win32 APIs under the

covers needs explicit disposal• These are many .NET Framework classes that work with

COM, the file system, or any number of other native services that need explicit displosal

Page 32: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 32© 2014 by ASNA. All rights reserved.

Driven by convention

• The .NET rule is that if a classes offers a Close or a Dispose method, you must call that method before the class goes out of scope.

Page 33: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 33© 2014 by ASNA. All rights reserved.

This fragment redirects an Excel spreadsheet to an HTTP output steam, using the intrinsic MemoryStream object. This code has the potential to be called many times per day in an ongoing Web app. What’s wrong with it?

DclFld WorkBook Type(XLWorkBook) DclFLd MemStream Type(System.IO.MemoryStream) New()

WorkBook = Query.Exec() *As XLWorkBookWorkBook.SaveAs(MemStream)MemStream.WriteTo(context.Response.OutputStream)

Page 34: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 34© 2014 by ASNA. All rights reserved.

I forgot to call the MemoryStream class’s Dispose() method! This causes a resource/memory leak that will ultimately bring a Web site to its knees!

DclFld WorkBook Type(XLWorkBook) DclFLd MemStream Type(System.IO.MemoryStream) New()

WorkBook = Query.Exec() *As XLWorkBookWorkBook.SaveAs(MemStream)MemStream.WriteTo(context.Response.OutputStream)MemStream.Dispose() Whoops!

Page 35: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 35© 2014 by ASNA. All rights reserved.

Again, borrowing from C#

• AVR 12.0 adds the BegUsing/EndUsing block• This construct provides a convenient way to use

iDisposable objects correctly•With BegUsing/EndUsing, you’ll never forget to call

Dispose() again!

Page 36: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 36© 2014 by ASNA. All rights reserved.

BegUsing/EndUsing provides a protective block with which you surround the instance of an iDisposable object.

DclFld WorkBook Type(XLWorkBook)

BegUsing MemStream Type(MemoryStream) + Value(*New MemoryStream()) WorkBook = Query.Exec() *As XLWorkBook WorkBook.SaveAs(MemStream) MemStream.WriteTo(context.Response.OutputStream)EndUsing

Page 37: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 37© 2014 by ASNA. All rights reserved.

BegUsing/EndUsing

• Typically, using BegUsing/EndUsing, you instance the object in place • You can use BegUsing/EndUsing with an existing

instance of an object, but remember this rule:

After the EndUsing state, the object in use is no longer available! • The safest thing to do is use it as presented on the

previous slide.

Page 38: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 38© 2014 by ASNA. All rights reserved.

You don’t need to ever

call this method!

Why? Didn’t I say that’s the

rule?

Why does the DataSet have a dispose method?

Page 39: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 39© 2014 by ASNA. All rights reserved.

The DataSet’s Dispose() method is an artifact of it inheriting from the MarshalByValueComponent.It’s not there because the DataSet needs a dispose method.

Page 40: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 40© 2014 by ASNA. All rights reserved.

Page 41: What's new in AVR 12.0 and VS 2013

We bring IBM i RPG assets forward 41© 2014 by ASNA. All rights reserved.

Why do men have nipples?