Presentation is loading. Please wait.

Presentation is loading. Please wait.

Generating Data Access Assemblies with IronRuby Rob Rowe Blog:

Similar presentations

Presentation on theme: "Generating Data Access Assemblies with IronRuby Rob Rowe Blog:"— Presentation transcript:

1 Generating Data Access Assemblies with IronRuby Rob Rowe Twitter: @rippinrobr Blog:

2 Why? Make tedious changes easier and quicker Create code based off of data specs from outside sources Wanted to learn Ruby Used IronRuby to access to.NET libraries such as System.Reflection.Emit Generate a DLL without compilation

3 What is IronRuby? An implementation of Ruby 1.8.x Built on top of.NET’s Dynamic Language Runtime Allows IronRuby to access.NET objects such as System.Reflection or any other.NET library Easy to ‘load’.NET libraries

4 Overview Generated Code – Models DLL and C# Domain/Service layer Consuming Application – ASP.NET MVC 2

5 Field Definitions Tables

6 Creating a.NET Assembly 1.Establish Assembly characteristics 2.Create the new Assembly 3.Define the Module 4.Create the Class 5.Create the Properties a.Create the backing field b.Create the Property c.Create Getter d.Create Setter 6.Save the Assembly

7 1. Establish Assembly Characteristics Create an instance of the System.Reflection.AssemblyName class Set Version Set Name Can set CultureInfo, Crypto KeyPair among as well as other settings

8 2. Create the new Assembly Creates the assembly in memory The first parameter is the AssemblyName object from step 1. Second parameter sets the permissions for the assembly. Here we can run it and save it to the file system. Returns an AssemblyBuilder Object. Used to create the module for the assembly.

9 3. Define the Module Each.NET assembly must have at least one module ‘Container’ for classes, interfaces, etc. Parameters: 1.Name of the module 2.Name of the DLL 3.false – so no symbols are created

10 4. Create the Class @mod_builder is the object returned when we created the module. Creates the class ‘under’ the module First parameter is fully qualified name of the class Second parameter is used to set public, static, etc. Returned TypeBuilder object is used to create the class’s fields and properties.

11 5a. Create Fields Must create a field for each property Creates a private field used by the getter/setters Uses the class’s TypeBuilder object Parameters ▫Defines the name ▫CLR Type ▫Sets Access level

12 5b. Create Properties Creates the Property’s TypeObject used for getter/setters Parameters: ▫Property Name ▫Says there’s a default value for this property ▫The CLR Type ▫Since there are no parameters for our properties we pass in null

13 5c. Create Getter – Prep Work Creates the Get method TypeObject DefineMethod’s parameters ▫Name of the method ▫Attributes for the method ▫Return type (CLR type) ▫Parameters to the method – none for getter GetILGenerator - creates the object that is used to create the IL code

14 5c. Create Getter – Method Body OpCodes.Ldarg_0 – loads the first argument on the stack which is ‘this’ in our case. OpCodes.Ldfld – pushes the value of the private field onto the stack. OpCodes.Ret – the returns the last item on the evaluation stack, the value of the private field.

15 5d. Create Setter – Parameters Constructs the Type array needed for the setter’s method parameters The type added to the list is the CLR type of the property Need to use a generic list to create the necessary CLR array Use List ’s ToArray method to get around CLR array type issues

16 5d. Create Setter – Method Body OpCodes.Ldarg_0 – loads the first argument on the stack which is ‘this’ in our case. OpCodes.Ldarg_1 - loads the setter parameter onto the stack OpCodes.Stfld – sets the value of private_field to the value of the item popped from the stack. OpCodes.Ret – adds the return statement.

17 5e. Finalizing Property Creation Need to associate the getter and setter with our new property Use the property TypeBuilder to associate the two method’s TypeBuilder objects with the IL we just created

18 6. Saving the Assembly The assembly is saved to the file system by using the AssemblyBuilder object’s Save method Once it has been saved we can use our new Models DLL like any other assembly.

19 Resources/Contact Info Twitter: @rippinrobr Blog: Code: ybuilder ybuilder Books: IronRuby Unleashed, Programming Ruby (AKA The Pickaxe Book)IronRuby UnleashedProgramming Ruby (AKA The Pickaxe Book)

Download ppt "Generating Data Access Assemblies with IronRuby Rob Rowe Blog:"

Similar presentations

Ads by Google