C Omega

844 views

Published on

http://www.intertech.com/Courses/Course.aspx?CourseID=99303

This is from an Intertech (http://www.Intertech.com) presentation on C Omega.

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
844
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C Omega

  1. 1. I nv e s t i g a t i ng Cω ( COm g a ) e • An overviewof pos s ible C# 3.0 language features. An Intertech Course
  2. 2. Course Name • Intertech Training is a privately owned training / developm com ent pany. • Specializing in .NET, J2EE™ and XM w L orkshops (3 –5 day). • All courses can be custom ized to suit your needs (w ow all our m e n aterial, so the charge is m inimal). • Classes are authored and delivered by industry experts (noted authors, M icrosoft M VPs, etc). Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 2
  3. 3. Course Name • Any Intertech Training class can be delivered: • At our facilities in Minneapolis, M innesota. • At your facility (w train all over the US, Canada, and e abroad). • Via “ eb Course Live” An instructor lead W W . ebcast (all you need is an internet connection and telephone). • w w w .intertechtraining.com (visit our booth!) Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 3
  4. 4. Course Name • Slides for this talk w be posted this Friday at the Intertech Training ill hom page: e • w w w .intertechtraining.com Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 4
  5. 5. Course Name Ove r vi e w o f Pr e s e nt a t i o n • Understand the role of COm ega (Cω ). • W W ho, hat, Where, When, Why? • OverviewCω ’ extensions to the CLR. s • Stream choice fields, anonym s, ous types, etc. • OverviewCω ’ XM Data Integration. s L • Allocating objects via XM setting properties via L, XM etc. L, • OverviewCω ’ SQL Data Integration. s • Database assemblies, inherit SQL keywords etc. Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 5
  6. 6. Course Name Pur pos e o f Pr e s e nt a t i o n • To explore w your life (and m life) as a C# program er m hat y m ight evolve into. • Gotta lov e s pe culation! • To peak your interest enough to check out COmega on your own terms. • Gotta lov e ne w toy s (it’ not all about program ing data grids, is it?) s m Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 6
  7. 7. Course Name Fr am ng t he COm g a La ng ua g e i e • COm ega (Cω ) is an experimental program ing m language for the .NET platform: • Cω is a superset of C#, created by Microsoft Research. • The free SDK (http://research.microsoft.com /Com ega) provides com and line tools, docum m entation, sam ple code and several Visual Studio 2003 project tem plates. • The rum or-m suggests that several features of ill Cω are the basis of possible C# 3.0 (y e s , 3.0) language features: • http://channel9.msdn.com/ShowPost.aspx?PostID=10276 Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 7
  8. 8. Course Name Fr am ng t he COm g a La ng ua g e i e • Like any language, Cω w designed to as address specific industry challenges: • Cω attem to blend XM OOP and SQL data into pts L, a unified whole. • Today, these three concepts, w hile obviously conceptually related, are disjointed. • For exam ple, C# ‘integrates’ L / SQL data using XM s pe cializ ation of data types (e.g. System.Xml.dll, System .Data.dll, etc.) Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 8
  9. 9. Course Name Fr am ng t he COm g a La ng ua g e i e • Consider the steps required for a sim .NET ple data selection: • Create a *Connection type. • Create a *Com and type (and any required m *Param eter types) and associate it to the *Connection type. • M ake use of a *DataReader or *DataAdapter to obtain records (create DataSet if required). • Shut dow connections (if using the connected n layer). Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 9
  10. 10. Course Name Fr am ng t he COm g a La ng ua g e i e public static void Main(string[] args) OOPS! Two typos… { SqlDataReader oReader; SqlConnection oCnn = new SqlConnection("Integrated Security=SSPI;Persist Security" + "Info=False;Initial Catalog=Northwind;Data Source=localhost"); oCnn.Open(); SqlCommand oCmd = new SqlCommand("slect * fom Customers", oCnn); These SQL Select typos oReader = oCmd.ExecuteReader(); will be found at while (oReader.Read()) runtime! Console.WriteLine("Customer Name: {0}", oReader["CompanyName"].ToString()); Ideally, we could catch oReader.Close(); oCnn.Close(); Errors at compile time… } Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 10
  11. 11. Course Name Fr am ng t he COm g a La ng ua g e i e • W hile the previous code is not rocket science, there is a clear disjoin between the syntax of the language and the data access m odel: • Ideally database interactions w ould be supported directly in the language. • For exam ple, w if w had keyw hat e ords w hich directly interacted w the underlying data? ith • Likew ise, w if w could ‘ a reference’ the database w hat e set to e are interested in? The com piler could then enforce ty pe s afe ty of our S QL (rather than blow up at runtim ing e)! Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 11
  12. 12. Course Name Fr am ng t he COm g a La ng ua g e i e • A sim disjoin exists w XM integration: ilar ith L • If w w to create a type w e ish hich is based on a stream of XM w m m L, e ust ake use of object serialization, the System.Xm l.dll assembly, DataSets, etc. • This im plies that w have forethought regarding w e hich types w need to be persisted… ill Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 12
  13. 13. Course Name Fr am ng t he COm g a La ng ua g e i e • Ponder the steps needed to (de)serialize an object’ state as XM s L: • Apply the [Serializable] attribute and/or implem the ent ISerializable interface. • Create the Xm lSerializer, making sure to pass in an array of types (type[]) for any sub-objects. • Open the stream containing the data. • Serialize or Deserialize aw ay. • Close up the stream s. Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 13
  14. 14. Course Name Fr am ng t he COm g a La ng ua g e i e public static void Main() // The Car class is serializable! { [Serializable] // Make a car and listen to the tunes. public class JamesBondCar Console.WriteLine("***** Creating a new James Bond Car *****"); { JamesBondCar myAuto = new JamesBondCar("Fred", 50, false, true); … myAuto.TurnOnRadio(true); myAuto.GoUnderWater(); } // Now save car using a pure XML format. Console.WriteLine("n***** Saving car to CarXmlData.xml file *****"); myStream = File.Create("CarXmlData.xml"); XmlSerializer myXmlFormat = new XmlSerializer(typeof(JamesBondCar), "Cars"); myXmlFormat.Serialize(myStream, myAuto); <?xml version="1.0"?> myStream.Close(); <JamesBondCar … > // Read in the Car from the XML file. <PetName>Fred</PetName> Console.WriteLine("n***** Reading car from Xml file *****"); <MaxSpeed>50</MaxSpeed> myStream = File.OpenRead("CarXmlData.xml"); <isFlightWorthy>false</isFlightWorthy> JamesBondCar carFromXml = (JamesBondCar)myXmlFormat.Deserialize(myStream); <isSeaWorthy>true</isSeaWorthy> Console.WriteLine("{0} is alive!", carFromXml.PetName); </JamesBondCar> carFromSoap.TurnOnRadio(true); myStream.Close(); } Of course, some .NET types (e.g. the DataSet) has built in support for XML data representation. Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 14
  15. 15. Course Name Fr am ng t he COm g a La ng ua g e i e • Again, not rocket science. However w if w hat e could apply XM data directly in code? For L exam ple: • myObj = <Data><MoreData>Foo</MoreData></Data>; • Furtherm ore, w if w could ‘ hat e reference’an XSD file as a binary unit? • W w e ould then have ty pe s afe ty of our XML. • Errors of XM docum definitions w L ent ould be found at com pile tim not runtim e, e. Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 15
  16. 16. Course Name Fr am ng t he COm g a La ng ua g e i e • From a design perspective, Cω sits squarely w ithin the intersection of the CLR, SQL relational data and XM data representation: L Cω accomplishes integration SQL XML through generalization Cω (e.g., blending these elements directly into the language) CLR – Several new keywords. – Several new concepts. – New types of assemblies. Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 16
  17. 17. Course Name Fr am ng t he COm g a La ng ua g e i e • In addition to the ‘ data integration angle’Cω also provides , features to sim plify the developm of loosely-coupled, netw ent orked applications: • Direct (and sim plified) support for asynchronous m ethod invocations (via the ‘ async’ keyw ord). • Direct (and sim plified) support for creating threads / code synchronization. • Bottom line, rem oting / thread support is integrated into the language rather than hidden w ithin the .NET BCL. • This talk w focus on the ‘ ill data integration angle’ . • The COm ega help system (provided w the SDK) w ith alks through several rem oting / threading examples. Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 17
  18. 18. Course Name Fr am ng t he COm g a La ng ua g e i e • Once you install the COm ega SDK, you w find a num of ill ber interesting subdirectories off the root: • By default, COmega is installed under C:Program FilesMicrosoft ResearchCom ega COmega Subdirectory Meaning in Life /bin COmega compiler and SQL / XML conversion tools. /Documents Supplied help. /Public Assemblies The COmega runtime assembly. /Samples Lots of code samples. /Templates Visual Studio .NET project templates. Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 18
  19. 19. Course Name Fr am ng t he COm g a La ng ua g e i e • Like any .NET language, Cω can be used to build executable assemblies or code libraries: – As well, Visual Studio 2003 supports Cω Windows Forms and Console based templates. – Of course, Cω projects have access to all aspects of the .NET base class libraries. Today, Cω’s VS.NET integration does not Provide designer support, however Debugging and IntelliSense support is enabled. Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 19
  20. 20. Course Name Fr am ng t he COm g a La ng ua g e i e • The Cω SDK provides a set of command line tools, including the Cω compiler, Cωc.exe. – Like csc.exe (the C# compiler), cwc.exe supports the /target, /reference and /out flags. – Cwc.exe supports response files, checked code, etc. The command line flags of cwc.exe are identical to those of csc.exe: cwc @myBuild.rsp Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 20
  21. 21. Course Name De m : o Che c ki ng out t he COm ga e Type Sys t e m Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 21
  22. 22. Course Name Fr am ng t he COm g a La ng ua g e i e • To achieve SQL / XM data integration, Cω provides L several newprogram ing constructs, for exam m ple: • S tre ams : Stream a sim to custom enum s ilar erators, how ever they are constructed on the fly. • Co nte nt Clas s e s : These classes are used to program atically m represent XM data / SQL data in a strongly typed m L anner. • Cho ic e Type s : Provides a w to define types w ay hich can be constructed w various possible fields (very helpful for ith XM L/DDL schem a). • Po s s ibly Null Value s : A sim plified w to determ if a value ay ine is null (sim to, but m pow ilar ore erful than, C# 2.0 nullable values). Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 22
  23. 23. Course Name COm ga St r e a m e s • Cω introduces the concept of a ‘stream’ type. – Cω streams are similar to strongly typed arrays / enumerators, however they are created on the fly. – A stream is declared by appending the operator '*' to the type name in the declaration of the variable. – Stream types are typically used within a ‘stream function’. // A stream f(x) of video games. This method streams back public static string* VideoGames() { string data (note string*). yield return "Resident Evil 4"; yield return "Metroid Prime: Echoes"; The ‘yield return’ construct represents yield return "Project: Snowblind"; yield return "Doom 3"; a ‘soft return’ for that single item. yield return "Half Life 2"; } Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 23
  24. 24. Course Name COm ga St r e a m e s • Under the hood, Cω stream functions leverage the System.Collections.Generic.IEnumerable<> generic interface: – Within the CIL, a compiler generated ‘closure class’ is used to throw back the stream values. .method public hidebysig static class System.Collections.Generic.'IEnumerable<System.String>' VideoGames() cil managed { .maxstack 1 .locals init ([0] class Test/'closure:2601' V_0, [1] class System.Collections.Generic.'IEnumerable<System.String>' 'return value', [2] class System.Collections.Generic.'IEnumerable<System.String>' 'Display Return Local') IL_0000: newobj instance void Test/'closure:2601'::'.ctor$PST06000063'() IL_0005: stloc.0 IL_0006: ldloc.0 IL_0007: stloc.1 IL_0008: br IL_000d IL_000d: ldloc.1 The ‘closure’ type is IL_000e: stloc.2 Responsible for spitting back IL_000f: ldloc.1 IL_0010: ret The stream’s values. } // end of method Test::VideoGames Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 24
  25. 25. Course Name COm ga St r e a m e s • The ‘closure’ type implements the standard enumerator interfaces as expected: .class auto ansi sealed nested private specialname 'closure:2601' extends [mscorlib]System.Object implements [mscorlib]System.Collections.IEnumerable, System.Collections.Generic.'IEnumerator<System.String>', [mscorlib]System.Collections.IEnumerator, [mscorlib]System.IDisposable, System.Collections.Generic.'IEnumerable<System.String>' { } // end of class 'closure:2601' Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 25
  26. 26. Course Name COm ga St r e a m e s • Cω streams can be invoked within a foreach construct as well as ‘apply-to-all’ syntax: – This shorthand notation extracts each value for processing. Notice the ‘it’ keyword. This line Can be read: static void Main() { // Read stream via traditional foreach. “Iterate over the strings from the foreach(string game in VideoGames()) VideoGames() stream function, Console.WriteLine(game); And place each into the WriteLine() Method of the Console type.” // Now read stream using 'apply-to-all' VideoGames().{Console.WriteLine(it)}; } Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 26
  27. 27. Course Name COm ga St r e a m e s • The ‘apply-to-all’ syntax scope can contain any number of statements, only some of which apply to the current item (‘it’): // Now read stream using 'apply-to-all' VideoGames().{ Given the construction of the Console.Write("Here is a string: "); VideoGame() stream function, Console.WriteLine(it); Console.Write("Now in uppercase: "); ‘it’ is a System.String and Console.WriteLine(it.ToUpper()) }; therefore yields access to each member. Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 27
  28. 28. COm ga Choi c e / Cont e nt e Course Name Type s • A ‘choice type’ is a Cω construct which allows you to define fields that may be from a set of possible legal data types. – For example, assume you wish to build a class that maps to a specific piece of XML (or relational) data. – Also assume that once such field could either be a string or a DateTime but not both. – If you have a background in C(++), think of choice types are a union. // Content type with a ‘choice’. struct GamingSystem GamingSystem is a COmega { ‘content type’ in that is defines struct { anonymous inner types which map to string? color; // More details in a moment… int numberOfGames; data content. choice {string date; DateTime dateTime;}; }; } Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 28
  29. 29. COm ga Choi c e / Cont e nt e Course Name Type s • Declaring variables of Choice types is interesting (to say the least): – Given that a choice type is well suited to model XML data, Cω allows you to declare and assign choice types by simply supplying the XML elements. – Note that the XML elements map directly to the fields of the choice type. static void Main() { // Choice type. // ‘myXbox’ type’s is inferred to be GamingSystem. struct GamingSystem myXbox = <GamingSystem> { <color>Black</color> struct <numberOfGames>10</numberOfGames> { <date>Feb 2000</date> string? color; </GamingSystem>; int numberOfGames; choice {string date; DateTime dateTime;}; // Now print color of GamingSystem type }; // (note ‘nullable type’). } string? color = myXbox.color; foreach(it in color) { Console.WriteLine("Color = {0}", it); }; } Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 29
  30. 30. COm ga Choi c e / Cont e nt e Course Name Type s • As shown on the previous slide, Cω infers a variables type based on the data assignment. – This however is not required. You may declare the variable with the type in mind. – The resulting CIL is identical however (and each approach is type safe / compiler checked). static void Main() { // Strongly name the ‘myXBox’ data type. GamingSystem myXbox = <GamingSystem> <color>Black</color> <numberOfGames>10</numberOfGames> <date>Feb 2000</date> </GamingSystem>; … } Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 30
  31. 31. COm ga Choi c e / Cont e nt e Course Name Type s • Furthermore, given that the <data> element has been defined as a choice, we could allocate a new DateTime during the assignment: static void Main() { // Recall! <date> is a choice (string or DateTime). GamingSystem myXbox = <GamingSystem> <color>Black</color> <numberOfGames>10</numberOfGames> <date>DateTime.Now</date> </GamingSystem>; … } Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 31
  32. 32. Course Name COm ga Nul l a bl e Ty pe s e • The previous example made use of ‘nullable’ types, which are similar to the same syntax seen in C# 2.0: – Suffixing a ? token to the end of a variable type denotes that a value may not actually be present. static void Main() // Choice type. { struct GamingSystem // ‘color’ specified here… { GamingSystem myXbox = <GamingSystem> struct <color>Black</color> { <numberOfGames>10</numberOfGames> string? color; <date>DateTime.Now</date> int numberOfGames; </GamingSystem>; choice {string date; DateTime dateTime;}; }; // ‘color’ NOT specified here… } GamingSystem myPS2 = <GamingSystem> <numberOfGames>10</numberOfGames> <date>DateTime.Now</date> </GamingSystem>; … } Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 32
  33. 33. COm ga Choi c e / Cont e nt e Course Name Type s • If a nullable type is specified is, it is assigned the value. If not, it is set to null. – ? Is simply a shorthand for performing the test manually. Shorthand for this. static void Main() { … // Now print color of GamingSystem type. string? date = myXbox.color; static void Main() … { } … // Now print color of GamingSystem type. string date = myXbox.color; if(date != null) // Use it. else // Do something else. … } Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 33
  34. 34. Course Name De m : o The COm ga XM I nt e g r a t i o n e L ( W ndows GUI e x a m e ) i pl Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 34
  35. 35. Course Name COm ga XM I nt e g r a t i o n e L • As you have seen, choice types can be created by assigning rawXM to an instance. L • How ever, Cω deeply integrates XM into the language. L • Using a sim syntax, you can create an instance of ilar any .NET type. • The root elem is the type itself, w ent hile the sub- elem ents are nam properties. ed Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 35
  36. 36. Course Name COm ga XM I nt e g r a t i o n e using System; L using System.Windows.Forms; public class ComegaForm : Form { private Button myButton; public ComegaForm () { // Create the Button type. The Button is ‘new-ed’ myButton = <Button> <Name>Test</Name> via XML. Note that the <Text>Hello</Text> <Size>80,24</Size> <Location>8,8</Location> Sub-elements are <BackColor>Salmon</BackColor> </Button>; properties of myButton.Click += new EventHandler(myButton_Click); this.Controls.AddRange(new Control[]{ myButton }); the Button class. } void myButton_Click(object sender, EventArgs e) { MessageBox.Show("Stop clicking me!"); } static void Main() { // Create the Form. Application.Run(<ComegaForm > <Text>This Form built with COmega</Text> Form is new-ed here…see next slide… <Width>200</Width> <Height>200</Height> <BackColor>White</BackColor> <FormBorderStyle>Fixed3D</FormBorderStyle> </ComegaForm > ); } } Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 36
  37. 37. Course Name COm ga XM I nt e g r a t i o n e L .method private hidebysig static void Main() cil managed { static void Main() … { // Define local Color and FormBorderStyle structures. // Create the Form. Application.Run(<ComegaForm > .locals init ([0] class ComegaForm V_0, <Text>This Form built with COmega</Text> [1] valuetype [System.Drawing]System.Drawing.Color V_1, <Width>200</Width> [2] valuetype [System.Windows.Forms]System.Windows.Forms.FormBorderStyle V_2) <Height>200</Height> IL_0000: newobj instance void ComegaForm::.ctor() // Make new Form derived type. <BackColor>White</BackColor> … <FormBorderStyle>Fixed3D</FormBorderStyle> IL_0007: ldstr "This Form built with COmega“ </ComegaForm > ); } // Set properties on the internal object. IL_000c: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Text(string) … IL_0017: call instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Width(int32) … IL_0022: call instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Height(int32) IL_0027: call valuetype [System.Drawing]System.Drawing.Color [System.Drawing]System.Drawing.Color::get_White() … IL_002f: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Control::set_BackColor (valuetype [System.Drawing]System.Drawing.Color) … IL_0038: call instance void [System.Windows.Forms]System.Windows.Forms.Form::set_FormBorderStyle( valuetype [System.Windows.Forms]System.Windows.Forms.FormBorderStyle) // Run the form! IL_003e: call void [System.Windows.Forms]System.Windows.Forms.Application::Run( class [System.Windows.Forms]System.Windows.Forms.Form) IL_0043: ret } Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 37
  38. 38. Course Name COm ga XM I nt e g r a t i o n e L • The sam holds true for custom types (as show in the choice type e n exam ple). • Assum you have a class type nam Car. e ed • This type could be allocated and assigned to an in-m ory stream of em XM L. Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 38
  39. 39. Course Name COm ga XM I nt e g r a t i o n e L public class Car { // Made public for ease of use. public string petName; public int currSpeed; public override string ToString() { return string.Format("{0} is going {1}", petName, currSpeed); .method private hidebysig static void Main() cil managed } { } .entrypoint // Code size 40 (0x28) public class Test .maxstack 3 { .locals init ([0] class Car V_0, [1] class Car viper) static void Main() IL_0000: newobj instance void Car::.ctor() { IL_0005: stloc.0 viper = <Car> IL_0006: ldloc.0 <currSpeed>100</currSpeed> IL_0007: ldc.i4.s 100 <petName>Zippy</petName> IL_0009: stfld int32 Car::currSpeed IL_000e: ldloc.0 </Car>; IL_000f: ldstr "Zippy" Console.WriteLine(viper); IL_0014: stfld string Car::petName Console.ReadLine(); … } } } Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 39
  40. 40. Course Name COm ga XM I nt e g r a t i o n e L • Understand that the previous exam ples only scratch the surface of COm ega’ XM s L integration: • XQuery support is integrated into the language. • As w rawXM can be used in place of return ell, L values, m ethod implementations and field m bers: em • Assum a class nam Em e ed ail: Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 40
  41. 41. Course Name COm g a XM I nt e g r a t i o n e L // An e-mail class. public class Email // Spam class which ‘has-a’ Email variable. { public class Spam struct{ { struct{ public static Email Sample = string From; <Email> string To; <Header> string? Subject; <From>Foo@Bar.com</From> } Header; <To>undisclosed recipients</To> struct{ <Subject>Bigger! HHJHdHJJFourty_Fourth</Subject> string P; </Header> }* Body; <Body> } <P> BMWS ON SALE TODAY // Return an Email object. </P> public static Email Vacation(DateTime d, TimeSpan s, string to){ <P> return <Email> YOU QUALIFY FOR A FREE BMW IF YOU FILL OUT OUR <Header> 10 HOUR FREE SURVEY FOR ONLY 40.00 USD. <From>John Doe</From> </P> <To>{to}</To> <P>THIS TRANSACTION IS 100% RISK FREE.</P> <Subject>OOF</Subject> </Body> </Header> </Email>; <Body><P>I am OOF from {d} until {d+s}.</P></Body> } </Email>; } } Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 41
  42. 42. Course Name COm ga XM I nt e g r a t i o n e L • Finally, XSD files can be ‘ referenced’ use in a Cω for project. • Visual Studio 2003 provides the ‘ Xsd Schem option to the Add a’ Add Reference dialog. • The com and line Xsd2Com m ega.exe com and line utility can m also be used. Either approach will import an XSD schema and generate a .NET assembly containing Cω types that map to the types defined in the XSD schema. Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 42
  43. 43. Course Name COm g a XM I nt e g r a t i o n e L Assume we have persisted an ADO.NET DataSet as an *.xsd file. • We could now reference the *.xsd file and make use of the ‘XML assembly’. • In code, we can simply manipulate the Titles type directly. Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 43
  44. 44. Course Name De m : The COm g a SQL o e I nt e gr at i o n Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 44
  45. 45. Course Name COm ga SQL I nt e g r a t i o n e • SQL integration is also present w Cω : ith • Databases can be ‘ referenced’ a project. into • Visual Studio 2003 provides the ‘ Database Add Schem option to the Add References dialog. a’ • The SDK com and line too, sql2com m ega.exe, perform the sam operation. s e Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 45
  46. 46. Course Name COm ga SQL I nt e g r a t i o n e • In any case, the result is term a ‘ ed database assem : bly’ • The Database type allow us to connect to the database via s connection string constructor parameters. • The Database type also exposes a public property for each table in the database. • In addition to the database type, the database assem bly defines class types w hich represent each table in the database. • As you would expect, each table class exposes the underlying columns. • Using a database assem bly, you can interact w the ith physical database using a sim plified object model. • The database assem perform all the ‘ bly s heavy lifting’ . Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 46
  47. 47. Course Name COm ga SQL I nt e g r a t i o n e • Assum you have created a database assem for the e bly Northw database provided by M SQL server: ind S The generated Database Type is used to establish a Connection to the datasource As well as interact with the Various database objects. Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 47
  48. 48. Course Name COm ga SQL I nt e g r a t i o n e public static void Main( string[] args ) { String connectstring = "Data Source=(local); Initial Catalog=Northwind; Connect Timeout=5; Integrated Security=SSPI"; Database dbo = new Database(connectstring); string mycity = args.Length>0 ? args[0] : "London"; Connect to Northwind Console.WriteLine("Contacts in "{0}"", mycity); Console.WriteLine(); Console.WriteLine("CustomerID Contact"); Console.WriteLine("--------------------------------------------"); Notice that the SQL res = select CustomerID, ContactName Statement is constructed from dbo.Customers where City == mycity order by ContactName; Using COmega keywords. // Print out rows foreach( row in res) { Console.WriteLine("{0,-12} {1}", row.CustomerID, row.ContactName); } Console.ReadLine(); } Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 48
  49. 49. Course Name COm ga SQL I nt e g r a t i o n e • Ponder the CIL behind the Database ctor: .method public hidebysig specialname rtspecialname instance void .ctor(string connectionString) cil managed { The Database type is // Code size 8 (0x8) Making use of .maxstack 2 Of an internal SqlDatabase IL_0000: ldarg.0 Type to maintain the IL_0001: ldarg.1 IL_0002: call instance void [System.Compiler.Sql.Runtime] Data session. System.Compiler.Sql.SqlDatabase::.ctor(string) IL_0007: ret } // end of method Database::.ctor Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 49
  50. 50. Course Name COm ga SQL I nt e g r a t i o n e • The exposed properties (such as Custom ers) encapsulate an internal field (_Custom in this case). ers • Table fields are of type System.Compiler.Sql.SqlTable. .method public hidebysig specialname instance class [System.Compiler.Sql.Runtime]System.Compiler.Sql.SqlTable get_Customers() cil managed { … IL_0001: ldfld class [System.Compiler.Sql.Runtime]System.Compiler.Sql.SqlTable Northwind.Database::_Customers … IL_000a: ldstr "Customers" IL_000f: ldtoken Northwind.Customers … IL_0019: newobj instance void [System.Compiler.Sql.Runtime]System.Compiler.Sql.SqlTable::.ctor (class [System.Compiler.Sql.Runtime]System.Compiler.Sql.SqlDatabase, string, class [mscorlib]System.Type) IL_001e: stfld class [System.Compiler.Sql.Runtime]System.Compiler.Sql.SqlTable Northwind.Database::_Customers … IL_0024: ldfld class [System.Compiler.Sql.Runtime]System.Compiler.Sql.SqlTable Northwind.Database::_Customers IL_0029: ret } Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 50
  51. 51. Course Name COm ga SQL I nt e g r a t i o n e • The actual rowdata is represented by a strongly typed class. • Colum are sim public fields. ns ply // Interacting with the Customers // object. res = select CustomerID, ContactName from dbo.Customers where Country == location order by ContactName; foreach( row in res) { Console.WriteLine("{0,-12} {1}", row.CustomerID, row.ContactName);} Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 51
  52. 52. Course Name COm ga SQL I nt e g r a t i o n e • Inserting newdata is also straightforward. • The SQL statem can be generated using the ‘ ent insert’and ‘ into’ keywords. // Insert a new record (yes, it is this simple). int n = insert CustomerID = "ABCDE", ContactName="Frank", CompanyName="Acme“ into DB.Customers; • As an alternative, the colum values can be assem n bled as a tuple constructor, which is then inserted to the correct table object: // Insert a new record (alternative form). row = new{CustomerID = "ABCDE", ContactName="Frank", CompanyName="Acme"}; int n = insert row into DB.Customers; Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 52
  53. 53. Course Name COm ga SQL I nt e g r a t i o n e • Updating newdata involves the ‘ update’and ‘ and set’ ‘ here’ w keywords: // Update an existing record with new content… int n = update DB.Customers set City = "London" where Country == "UK" && City == "Lundon"; • Deleting records (as you might be suspecting) involves the ‘ delete’ keyword: // Delete a contact… delete from DB.Customers where CustomerID == "ALFKI"; Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 53
  54. 54. Course Name COm ga SQL I nt e g r a t i o n e • COm ega’ SQL integration does go far beyond s select, insert, update, delete. As you check things out further, you w find support for: ill • Joins. • Aggregates. • Transactions. • Group-By clauses. • Etc… Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 54
  55. 55. Course Name Pr e s e nt a t i o n Sum a r y ! m • COm ega (Cω ) is an experim ental .NET language w hich hints at C# futures. • Cω integrates XM L/SQL data representation directly into the language. • Cω also expands the CTS w newconstructs w ith hich support program ing generalization. m • http://research.m icrosoft.com/Com ega Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 55
  56. 56. Course Name • Rem ber! Slides for this talk w be posted this Friday at the Intertech em ill Training hom page: e • w w w .intertechtraining.com Copyright © Intertech, Inc. 2006 •w w w .Intertech.com•800-866-9884 •Slide 56

×