DCSIMG
December 2009 - Posts - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2012 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

December 2009 - Posts

Sessions in Sela SDP

Sessions in Sela SDP

SDPLogo
Yesterday I finished my part in the Sela SDP with two sessions.
The first session I delivered was EF4 evolution in .NET 4. In
that session I showed the new features of EF4 which make it a mature
technology. I want to thank the audience that attended the session.
Since I finished the lecture early, I also had an improvised session
about how to create inheritance with EF.
In the afternoon I took a part of the Sela’s 6 MVP’s session which was an
experts panel about technologies.
As in the ORM day from Monday the recording of the sessions
(In Hebrew) will be published in the SDP site.
You can download the slide deck and demos of the EF4 session from here.
Enjoy!

DotNetKicks Image

Father for the Second Time

Father for the Second Time

This is not my usual technical blog posts.
Somebody asked me today where I’ve been in the last two weeks.
The answer is in the title :-)
My wife gave birth to my second child, Eyal, almost two weeks ago.
I took a small vacation in order to help my wife in the first days of
the maternity leave.
I will return blogging in the near future.
Stay tuned!

DotNetKicks Image

ORM Day at Sela SDP

ORM Day at Sela SDP

SDPLogo

Today I took a part of an ORM tutorial day at Sela SDP conference.
The day included the following sessions:

In my session I talked about the use of Stored Procedures in EF model,
Inheritance mapping and how to extend the EF model.
The session was fun and the audience was great. 
In the experts panel there were a lot of good questions and we also talked
about very interesting real world scenarios. 
My session’s slide deck and demos can be downloaded from here.
As always the database backup can be found with the demos. 
Also the session recordings will be published soon in the SDP site
(in Hebrew). 
Stay tuned for more SDP details in the near future!

DotNetKicks Image

Reading a Xml File in T4 Templates

Reading a Xml File in T4 Templates

After I wrote the Generating Code From a File Using T4 Templates
post about the
use of T4 templates
in EF4
, I played with
them for a while (not
in EF but generally with
T4 templates).
In an old project that
I’ve created almost 3 years ago
which automated the use of lookup tables I had an enum.
That enum was meant to be the connection between an Xml
node names and was heavily used in the application.
For each Xml node I needed to add an entry in the enum.
So I thought what the hell lets see if I could have make it using
a T4 template. The result is in the post.

Reading a Xml File in T4 Templates

The first thing to do is to add a new file with the extension of
.tt into the project. After I had that T4 template file I started to
think what is the way to read an Xml file from the T4. Since the
Xml file was a part of the project the use of Path class to get the
absolute location was one way to get the file.
Pay attention that if you will open the solution from the IDE and
not from its location (double click on the solution from its current
location) the Path.GetFullPath won’t work.
After I had the file it was as simple as to read a Xml using
XmlDocument and XPath query.
The following example is the T4 I have created:

<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
using System;
 
namespace Examples
{
    public class ExampleClass
    {
        #region Enums
 
        public enum eExampleEnum
        {
<#
    foreach (string name in this.GetNames())   
    {
#>
    <#= name #>,
<# 
    }
        #>
        }
        
        #endregion
    }
}
<#+
    public List<string> GetNames()
    {
        List<string> result = new List<string>(); 
        XmlDocument doc = new XmlDocument();
        string absolutePath = Path.GetFullPath("File.xml");                
        doc.Load(absolutePath);
        foreach (XmlNode node in doc.SelectNodes("/Root/Element"))
        {
            result.Add(node.Attributes["Name"].InnerText);
        }
        return result;
    }
#>

Summary

The use of T4 template can help us to generate code using
a code generation tool. In the example I showed how you can use
a T4 template in order to generate an enum from a given Xml file.

DotNetKicks Image

T4 Templates in Entity Framework 4

T4 Templates in Entity Framework 4

T4 templates orT4 Templates in Entity Framework 4
Text Template
Transformation Toolkit
is
a mean of creating a code
generation
artifacts which
can save a lot of time.
This technology is a part of
Visual Studio since 2005 but
was one of those technologies
that were kept in secret by Microsoft.
Almost all the code generation stuff that is being used
in Visual Studio in technologies such as MVC, Entity Framework,
LINQ to SQL and many other use those templates as backbone
to generate code. In this post I’m going to show one of the
new features of EF4 which is the use of T4 templates to extend
or manipulate the code generation. If you like to read more about
T4 templates I recommend Scott Hanselman’s post – 
T4 (Text Template Transformation Toolkit) Code Generation -
Best Kept Visual Studio Secret
which has many references to good
reading stuff.

T4 Templates in Entity Framework 4 Example

In EF4 we can use T4 templates to extend and manipulate the
code generation. In the example I start with a very simple
console application. That application has a Entity Data Model
which is the same School model that I used for all my previous
EF posts:
Solution Explorer 

Adding a T4 Template

Now lets explore the process of using the T4 templates.
In order to use the T4 templates, from the Entity Framework
Designer
surface use the right mouse button to open the menu
and choose the Add Code Generation Item menu item:
Add Code Generation Item 
This will open the Add New Item window that holds only EF4 T4
templates
:
Add New Item
As you can see I have two T4 templates that I can generate. I choose the
ADO.NET EntityObject Generator. A security warning will be popping up to
check if you trust the source that generated the T4.
Security Warning
I choose to continue and a new file with the extension .tt is added to
the solution.
After Adding the T4 Template
When you observe the generated .tt file you can see that all the generated
stuff of EF is located in a new .cs file that is attached to it. When opening  
the SchoolModel.Designer.cs file that is attached to the edmx file you will
see a comment that say it is no longer being used:

// Default code generation is disabled for model 'path\SchoolModel.edmx'. 
// To enable default code generation, change the value of the 'Code Generation Strategy' designer
// property to an alternate value. This property is available in the Properties Window when the model is
// open in the designer.

Now that we have our own code generation file we can start manipulating
the generated code.

Manipulating Generated Code Using a T4 Template

Say for example that I want to remove the generated OnContextCreated
method which is called from the ObjectContext’s constructors:

/// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public partial class SchoolEntities : ObjectContext
    {
        #region Constructors
    
        /// <summary>
        /// Initializes a new SchoolEntities object using the connection string found in the 'SchoolEntities' section of the application configuration file.
        /// </summary>
        public SchoolEntities() : base("name=SchoolEntities", "SchoolEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        /// <summary>
        /// Initialize a new SchoolEntities object.
        /// </summary>
        public SchoolEntities(string connectionString) : base(connectionString, "SchoolEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        /// <summary>
        /// Initialize a new SchoolEntities object.
        /// </summary>
        public SchoolEntities(EntityConnection connection) : base(connection, "SchoolEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        #endregion
    
        #region Partial Methods
    
        partial void OnContextCreated();
    
        #endregion
    
        #region ObjectSet Properties
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Course> Courses
        {
            get
            {
                if ((_Courses == null))
                {
                    _Courses = base.CreateObjectSet<Course>("Courses");
                }
                return _Courses;
            }
        }
        private ObjectSet<Course> _Courses;
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Department> Departments
        {
            get
            {
                if ((_Departments == null))
                {
                    _Departments = base.CreateObjectSet<Department>("Departments");
                }
                return _Departments;
            }
        }
        private ObjectSet<Department> _Departments;
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Enrollment> Enrollments
        {
            get
            {
                if ((_Enrollments == null))
                {
                    _Enrollments = base.CreateObjectSet<Enrollment>("Enrollments");
                }
                return _Enrollments;
            }
        }
        private ObjectSet<Enrollment> _Enrollments;
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Person> People
        {
            get
            {
                if ((_People == null))
                {
                    _People = base.CreateObjectSet<Person>("People");
                }
                return _People;
            }
        }
        private ObjectSet<Person> _People;
 
        #endregion
        #region AddTo Methods
    
        /// <summary>
        /// Deprecated Method for adding a new object to the Courses EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
        /// </summary>
        public void AddToCourses(Course course)
        {
            base.AddObject("Courses", course);
        }
    
        /// <summary>
        /// Deprecated Method for adding a new object to the Departments EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
        /// </summary>
        public void AddToDepartments(Department department)
        {
            base.AddObject("Departments", department);
        }
    
        /// <summary>
        /// Deprecated Method for adding a new object to the Enrollments EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
        /// </summary>
        public void AddToEnrollments(Enrollment enrollment)
        {
            base.AddObject("Enrollments", enrollment);
        }
    
        /// <summary>
        /// Deprecated Method for adding a new object to the People EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
        /// </summary>
        public void AddToPeople(Person person)
        {
            base.AddObject("People", person);
        }
 
        #endregion
    }

Opening the .tt file and then removing the OnContextCreated
method call from the constructors and also the generation of the
OnContextCreated partial method will result in the following
ObjectContext:

/// <summary>
/// No Metadata Documentation available.
/// </summary>
public partial class SchoolEntities : ObjectContext
{
    #region Constructors
 
    /// <summary>
    /// Initializes a new SchoolEntities object using the connection string found in the 'SchoolEntities' section of the application configuration file.
    /// </summary>
    public SchoolEntities() : base("name=SchoolEntities", "SchoolEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
    }
 
    /// <summary>
    /// Initialize a new SchoolEntities object.
    /// </summary>
    public SchoolEntities(string connectionString) : base(connectionString, "SchoolEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
    }
 
    /// <summary>
    /// Initialize a new SchoolEntities object.
    /// </summary>
    public SchoolEntities(EntityConnection connection) : base(connection, "SchoolEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
    }
 
    #endregion
 
    #region ObjectSet Properties
 
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public ObjectSet<Course> Courses
    {
        get
        {
            if ((_Courses == null))
            {
                _Courses = base.CreateObjectSet<Course>("Courses");
            }
            return _Courses;
        }
    }
    private ObjectSet<Course> _Courses;
 
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public ObjectSet<Department> Departments
    {
        get
        {
            if ((_Departments == null))
            {
                _Departments = base.CreateObjectSet<Department>("Departments");
            }
            return _Departments;
        }
    }
    private ObjectSet<Department> _Departments;
 
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public ObjectSet<Enrollment> Enrollments
    {
        get
        {
            if ((_Enrollments == null))
            {
                _Enrollments = base.CreateObjectSet<Enrollment>("Enrollments");
            }
            return _Enrollments;
        }
    }
    private ObjectSet<Enrollment> _Enrollments;
 
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public ObjectSet<Person> People
    {
        get
        {
            if ((_People == null))
            {
                _People = base.CreateObjectSet<Person>("People");
            }
            return _People;
        }
    }
    private ObjectSet<Person> _People;
 
    #endregion
    #region AddTo Methods
 
    /// <summary>
    /// Deprecated Method for adding a new object to the Courses EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
    /// </summary>
    public void AddToCourses(Course course)
    {
        base.AddObject("Courses", course);
    }
 
    /// <summary>
    /// Deprecated Method for adding a new object to the Departments EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
    /// </summary>
    public void AddToDepartments(Department department)
    {
        base.AddObject("Departments", department);
    }
 
    /// <summary>
    /// Deprecated Method for adding a new object to the Enrollments EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
    /// </summary>
    public void AddToEnrollments(Enrollment enrollment)
    {
        base.AddObject("Enrollments", enrollment);
    }
 
    /// <summary>
    /// Deprecated Method for adding a new object to the People EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
    /// </summary>
    public void AddToPeople(Person person)
    {
        base.AddObject("People", person);
    }
 
    #endregion
}

Of course this is silly to do because this is an extension point
when the context is being created but for the example it is enough
to show the power of the T4 templates.

Summary

The T4 templates enables us to implement code generation.
When you have something that you do manually more then one time
then applying a T4 template can help you to make it automatic.
As I show in the post, EF4 is shipping with a way to manipulate the
code generation using T4 templates. Next time when something bother
you in the EF generated code you can do something about it.

DotNetKicks Image

Get Ready for Sela SDP Conference

Get Ready for Sela SDP Conference

SDP Conference

The PDC conference is over. SDP Occurence
A new conference is on the
way. The SDP conference is
going to happen between the 
27 to 30 of December. In that
conference we (Sela Group
are going to to share the
future technologies
including the ones that where
exposed in the Microsoft PDC.

In the conference I’m going to
have a session with
Bnaya Eshet on
ADO.NET Entity Framework
Evolution in .NET 4.0
. If you
want to hear where EF is
heading, this is the place to be.
Also, I’m taking a part in an very interesting ORM day
along with Ido Flatow, Bernie Almosni and Erez Harari.
In that day I’m going to have a deep dive session about
EF and also participate in the experts panel we are going
to have. This is going to be fun. 
There are other tutorials and sessions that are very recommended
so if you want to be a part of the conference you can go to
the conference web site – SDP.
See you there.

DotNetKicks Image

Windows Server AppFabric

Windows Server AppFabric

One of the announcements Windows Server AppFabric
in the PDC was the
release of beta1 for
a new windows server
which is called AppFabric.

What is AppFabric?

From the AppFabric site – “Windows Server AppFabric is a set
of integrated technologies that make it easier to build, scale and
manage Web and composite applications that run on IIS.

AppFabric is a collection of technologies in one consolidated place.
These technologies are Microsoft Distributed Cache (aka “Velocity”),
Windows Workflow Foundation and Windows Communication Foundation
(aka “Dublin”).

For more details go to Windows Server AppFabric site.

DotNetKicks Image