<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>SeeSharp - SeeSharp</title>
        <description>A C# development blog</description>
        <link>/SeeSharp/RSS</link>
        <language>en</language>
        <image>
            <url>http://www.hightech.ir/favicon.png</url>
            <title>SeeSharp</title>
            <link>/SeeSharp/RSS</link>
            <width>64</width>
            <height>64</height>
        </image>
        <item>
            <dc:creator>HEskandari</dc:creator>
            <title>Deserting SVN for good: Using Git</title>
            <description>&lt;p&gt;
	I&amp;#39;ve been having tremendous amount of problems lately using &lt;a href=&quot;http://tortoisesvn.tigris.org/&quot;&gt;TortoiseSVN&lt;/a&gt;. Although server seemed to work fine, my SVN client had lots of problems, so I decided it was time to switch to &lt;a href=&quot;http://en.wikipedia.org/wiki/Git_(software)&quot;&gt;Git&lt;/a&gt;. I was using the free service available at &lt;a href=&quot;http://xp-dev.com&quot;&gt;xp-dev&lt;/a&gt; but the free service only includes SVN repositories, so I tried the next best thing: GIT-SVN. Let&amp;#39;s see how it worked out for me.&lt;/p&gt;
&lt;p&gt;
	Git is not (yet) natively supported on windows. The only way to run it on Windows was to use &lt;a href=&quot;http://www.cygwin.com&quot;&gt;Cygwin&lt;/a&gt;, a linux like environment for Windows, but the new project called &lt;a href=&quot;http://www.google.com/url?q=http://code.google.com/p/msysgit/w/list&amp;amp;ei=CemTS9SaC5K2lAfshbWcBw&amp;amp;sa=X&amp;amp;oi=smap&amp;amp;resnum=1&amp;amp;ct=result&amp;amp;cd=3&amp;amp;ved=0CAoQqwMoAjAA&amp;amp;usg=AFQjCNHXK6mIm-wFQ-ITmoQVFild9JDYug&quot;&gt;msysgit&lt;/a&gt; is trying to make a native Windows client. To get started &lt;a href=&quot;http://www.google.com/url?q=http://code.google.com/p/msysgit/downloads/list&amp;amp;ei=CemTS9SaC5K2lAfshbWcBw&amp;amp;sa=X&amp;amp;oi=smap&amp;amp;resnum=1&amp;amp;ct=result&amp;amp;cd=1&amp;amp;ved=0CAgQqwMoADAA&amp;amp;usg=AFQjCNFyPtxDGdLMDJmffOhizc_XsDDHNA&quot;&gt;download&lt;/a&gt; and install msysgit from google code. I&amp;#39;m using version 1.6.5.1 preview 20091022 at the time of this post, but it is updated regularly.&lt;/p&gt;
&lt;p&gt;
	To connect to SVN repository, open a Git Bash window on your destination folder and enter the following command:&lt;/p&gt;
&lt;pre&gt;
&lt;strong&gt;git svn -s clone http://your-svn-address
&lt;/strong&gt;&lt;/pre&gt;
&lt;p&gt;
	If your SVN repository has no standard folders such as &amp;quot;trunk&amp;quot;, &amp;quot;tag&amp;quot; and &amp;quot;branch&amp;quot; you should remove &amp;quot;-s&amp;quot; parameter. This command would get all the sources, except empty folders in your existing SVN repository because Git does not track folders but file content.&amp;nbsp; It would take some time for git to get all the revisions but when it finishes, you&amp;#39;re all set.&lt;/p&gt;
&lt;p&gt;
	From this point on, you can use &lt;a href=&quot;http://www.kernel.org/pub/software/scm/git/docs/everyday.html&quot;&gt;git commands&lt;/a&gt; but before committing changes back into your SVN repository, you should update your working folder by the following commands:&lt;/p&gt;
&lt;pre&gt;
&lt;strong&gt;git svn fetch
git svn rebase
&lt;/strong&gt;&lt;/pre&gt;
&lt;p&gt;
	This will update your local git working copy by getting all the changes made by other people and then applies your local changes over that.&lt;/p&gt;
&lt;p&gt;
	The last command to send your changeset to the SVN repository is the following:&lt;/p&gt;
&lt;pre&gt;
&lt;strong&gt;git svn dcommit
&lt;/strong&gt;&lt;/pre&gt;
&lt;p&gt;
	To find out more about git commands, try &lt;a href=&quot;http://git.wiki.kernel.org/index.php/GitCheatSheet&quot;&gt;this&lt;/a&gt; cheat sheet that comes handy.&lt;/p&gt;</description>
            <link>http://www.hightech.ir/SeeSharp/deserting-svn-for-good-using-git</link>
            <guid isPermaLink="true">http://www.hightech.ir/SeeSharp/deserting-svn-for-good-using-git</guid>
            <pubDate>Sun, 07 Mar 2010 18:00:00 GMT</pubDate>
            <category>svn</category>
            <category>git</category>
        </item>
        <item>
            <dc:creator>HEskandari</dc:creator>
            <title>Silverlight Applications with J2EE Backend - Part Two</title>
            <description>&lt;p&gt;
	In &lt;a href=&quot;http://www.hightech.ir/SeeSharp/Silverlight-J2EE-Backend-Part-One&quot;&gt;previous&lt;/a&gt; post, we saw how easy it is to use J2EE as a rich backend. Let&amp;#39;s continue with our example and show how to fetch and send some data from our J2EE backend to a Silverlight rich client application. Let&amp;#39;s continue our work using a simple Blog domain model.&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;
	&lt;img alt=&quot;&quot; src=&quot;http://www.hightech.ir/Images/BlogPics/J2EE_P2_BlogDomainModel.png&quot; style=&quot;width: 556px; height: 293px;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
	&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
	To create our simple domain model, let&amp;#39;s create the domain classes in java and let &lt;a href=&quot;http://en.wikipedia.org/wiki/Java_Persistence_API&quot;&gt;JPA&lt;/a&gt; create the database for us. For .NET developers familiar with NHibernate and &lt;a href=&quot;https://www.hibernate.org/hib_docs/nhibernate/html/mapping-attributes.html&quot;&gt;NHibernate Mapping Attributes&lt;/a&gt; project, when comparing this to .NET coding style, there should be nothing new here. Here&amp;#39;s a piece of code:&lt;br /&gt;
	&amp;nbsp;&lt;/p&gt;
&lt;pre class=&quot;brush:java&quot;&gt;
package ir.hightech.silverbox.domain;

import javax.persistence.*;
import java.util.List;

@Table(name = &amp;quot;sl_Post&amp;quot;)
@Entity
public class Post {

    private int postId;
    private String title;
    private Blog blog;
    private List&amp;lt;Comment&amp;gt; comments;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = &amp;quot;PostId&amp;quot;, nullable = false)
    public int getPostId() {
        return postId;
    }

    public void setPostId(int postId) {
        this.postId = postId;
    }

    @ManyToOne
    public Blog getBlog() {
        return blog;
    }

    public void setBlog(Blog blog) {
        this.blog = blog;
    }

    @Column(name = &amp;quot;Title&amp;quot;, nullable = false)
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @OneToMany(mappedBy = &amp;quot;post&amp;quot;)
    public List&amp;lt;Comment&amp;gt; getComments() {
        return comments;
    }

    public void setComments(List&amp;lt;Comment&amp;gt; comments) {
        this.comments = comments;
    }
}

package ir.hightech.silverbox.domain;

import javax.persistence.*;

@Table(name = &amp;quot;sl_Comment&amp;quot;)
@Entity
public class Comment {

    private int commentId;
    private Post post;
    private String text;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = &amp;quot;CommentId&amp;quot;, nullable = false)
    public int getCommentId() {
        return commentId;
    }

    public void setCommentId(int commentId) {
        this.commentId = commentId;
    }

    @ManyToOne()
    public Post getPost() {
        return post;
    }

    public void setPost(Post post) {
        this.post = post;
    }

    @Column(name = &amp;quot;CommentText&amp;quot;, nullable = false)
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}
&lt;/pre&gt;
&lt;p&gt;
	Let&amp;#39;s create a service to return a list of all available Posts and display them on our Silverlight client. Continuing from previous post, you&amp;#39;ve already learned how to create services (or &lt;a href=&quot;http://en.wikipedia.org/wiki/Session_Beans&quot;&gt;SessionBeans&lt;/a&gt; as called in EJB context) but when you actually have an entity model and want to expose it through service, you&amp;#39;d better think twice. There are a lot of things that can go wrong if you do so:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;
		Your change in domain model will ripple to your clients because of changes in service contract and will probably break all your clients.&lt;br /&gt;
		&amp;nbsp;&lt;/li&gt;
	&lt;li&gt;
		Your domain model might contain sensitive information which is best kept on your service tier.&lt;br /&gt;
		&amp;nbsp;&lt;/li&gt;
	&lt;li&gt;
		Other technical stuff such as your entities beiing lazy and having problems with serialization process.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
	&lt;br /&gt;
	So before continuing, let&amp;#39;s create some &lt;a href=&quot;http://en.wikipedia.org/wiki/Data_transfer_object&quot;&gt;DTO&lt;/a&gt; objects which will be sent across the service boundary. For this sample, a &lt;a href=&quot;http://en.wikipedia.org/wiki/Plain_Old_Java_Object&quot;&gt;POJO&lt;/a&gt; class resembling your existing Post entity would suffice. To map between Entities and DTOs, you can roll your own mapper classes.&lt;/p&gt;
&lt;pre class=&quot;brush:java&quot;&gt;
package ir.hightech.silverbox.common;

import ir.hightech.silverbox.domain.Post;
import javax.ejb.Remote;
import java.util.List;

@Remote
public interface BlogService {
    List&amp;lt;PostDto&amp;gt; getAllPosts();
}


package ir.hightech.silverbox.services;

import ir.hightech.silverbox.common.BlogService;
import ir.hightech.silverbox.domain.Post;
import javax.ejb.*;
import javax.jws.*;
import javax.persistence.*;
import java.util.*;

@Stateless(name = &amp;quot;BlogService&amp;quot;)
@Remote(BlogService.class)
@WebService
public class BlogServiceImpl implements BlogService {

    @PersistenceContext
    protected EntityManager em;

    public List&amp;lt;PostDto&amp;gt; getAllPosts() {
        Query query = em.createQuery(&amp;quot;FROM Post&amp;quot;);
        List&amp;lt;Post&amp;gt; result = query.setMaxResults(10)
                                 .getResultList();

        return new PostMapper().MapMany(result);
    }
}
&lt;/pre&gt;
&lt;p&gt;
	Did you notice the EntityManager? It is injected automatically by EJB container and the good thing is you can inject other services, even your other EJB session beans too. EntityManager provides &lt;a href=&quot;http://technology-related.com/javaee/5/docs/api/javax/persistence/EntityManager.html&quot;&gt;functionalities&lt;/a&gt; similar to ISession in N/Hiberante such as CreateQuery, Find, Attach/Detach, Flush, etc.&lt;br /&gt;
	&lt;br /&gt;
	Now let&amp;#39;s create a new Silverlight application and add a new &amp;quot;Service Reference&amp;quot; to your project which points to our new service at:&lt;/p&gt;
&lt;pre&gt;
http://localhost:9090/Silverbox/BlogService?WSDL
&lt;/pre&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;
	&lt;img alt=&quot;&quot; src=&quot;http://www.hightech.ir/Images/BlogPics/J2EE_P2_AddingServiceReference.png&quot; style=&quot;width: 475px; height: 383px;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
	Note: Use IP / Port of your JBoss server, port is usually 8080. Mind the small / caps characters.&lt;/p&gt;
&lt;p&gt;
	If you open the proxy sources which is automatically generated, you&amp;#39;ll notice our DTO objects are all here in our Silverlight application now! There&amp;#39;s a slight modification though: All the classes and properties are named according to &lt;a href=&quot;http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html&quot;&gt;java naming conventions&lt;/a&gt; so our PostDto class is now named &amp;quot;postDto&amp;quot; on the Silverlight client. This is happened to all the properties of the proxy classes too so if you&amp;#39;re binding generated classes directly to your UI keep in mind that Xaml binding are case sensitive. What is good is that INotifyPropertyChange is already implemented. The rest is just regular Silverlight application calling a remote service:&lt;/p&gt;
&lt;pre class=&quot;brush:xml&quot;&gt;
&amp;lt;Grid x:Name=&amp;quot;LayoutRoot&amp;quot;&amp;gt;
    &amp;lt;ListBox x:Name=&amp;quot;RecentPosts&amp;quot;&amp;gt;
        &amp;lt;ListBox.ItemTemplate&amp;gt;
            &amp;lt;DataTemplate&amp;gt;
                &amp;lt;StackPanel Orientation=&amp;quot;Horizontal&amp;quot;&amp;gt;
                    &amp;lt;TextBlock Text=&amp;quot;{Binding Path=postDate}&amp;quot; /&amp;gt;
                    &amp;lt;TextBlock Text=&amp;quot;{Binding Path=title}&amp;quot; /&amp;gt;
                &amp;lt;/StackPanel&amp;gt;
            &amp;lt;/DataTemplate&amp;gt;
        &amp;lt;/ListBox.ItemTemplate&amp;gt;
    &amp;lt;/ListBox&amp;gt;
&amp;lt;/Grid&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
	and to call the service:&lt;/p&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    var service = new BlogServiceImplClient();
    service.GetAllPostsCompleted += (snd, arg) =&amp;gt; 
    {
        if(arg.Error == null)
        {
            RecentPosts.ItemsSource = arg.Result;
        }
    };
    service.GetAllPostsAsync();
}
&lt;/pre&gt;
&lt;p&gt;
	You should have a rich application that is bound to your database using your service layer on J2EE. Pretty easy so far, don&amp;#39;t you think?&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;
	&lt;img alt=&quot;&quot; src=&quot;http://www.hightech.ir/Images/BlogPics/J2EE_P2_RunningApplication.png&quot; style=&quot;width: 455px; height: 440px;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
	Next post in this serie will show you how to handle business validation using SoapFaults, how to serialize Java exceptions into Silverlight client and how to actually deploy the whole application. Get the sources of this part from &lt;a href=&quot;http://cid-4962b6ceabc2cbd7.skydrive.live.com/self.aspx/BlogFiles/SilverBox%20-%20PartTwo.zip&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
	&amp;nbsp;&lt;/p&gt;</description>
            <link>http://www.hightech.ir/SeeSharp/Silverlight-J2EE-Backend-Part-Two</link>
            <guid isPermaLink="true">http://www.hightech.ir/SeeSharp/Silverlight-J2EE-Backend-Part-Two</guid>
            <pubDate>Sun, 07 Mar 2010 08:00:00 GMT</pubDate>
            <category>RIA</category>
            <category>Silverlight</category>
            <category>J2EE</category>
        </item>
        <item>
            <dc:creator>HEskandari</dc:creator>
            <title>PersianCalendar support for DevExpress</title>
            <description>&lt;p&gt;
	Creating Line of Business applications using &lt;a href=&quot;http://www.devexpress.com/Products/NET/Controls/WPF/Grid/&quot;&gt;DevExpress WPF controls&lt;/a&gt; is just a breeze. The actual application is nice looking too! Since there are a couple of skins available and the good thing is the skin also changes the look and feel of standard WPF controls!&lt;/p&gt;
&lt;p&gt;
	I&amp;#39;ve used &lt;a href=&quot;http://github.com/HEskandari/FarsiLibrary&quot;&gt;my calendar library&lt;/a&gt; to develop a fully localizable solution. These controls support &lt;a href=&quot;http://en.wikipedia.org/wiki/Gregorian_calendar&quot;&gt;Gregorian&lt;/a&gt;, &lt;a href=&quot;http://en.wikipedia.org/wiki/Islamic_calendar&quot;&gt;Hijri&lt;/a&gt; and &lt;a href=&quot;http://en.wikipedia.org/wiki/Iranian_calendar&quot;&gt;Persian&lt;/a&gt; calendars so multicultural application development would be seamless, but the look and feel of my controls were a lot different than the ones provided by DevExpress. On the other hand, DevExpress only supports Gregorian calendar! So I needed to apply look and feel of DevExpress to my controls. It goes without saying that you can open the controls in Expression Blend and give it a style that mimics DevExpress look and feel, but that is not easy to do.&lt;/p&gt;
&lt;p&gt;
	DevExpress provides a PopupEditBase control that can be used to display any control as a Popup. That seemed to be a good choice and it was easy to create a new control all in xaml markup:&lt;/p&gt;
&lt;pre class=&quot;brush:xml&quot;&gt;
&amp;lt;Window x:Class=&amp;quot;DXCustomEditor.MainWindow&amp;quot;
        xmlns=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;
        xmlns:x=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot; 
        xmlns:dxe=&amp;quot;http://schemas.devexpress.com/winfx/2008/xaml/editors&amp;quot; 
        xmlns:fx=&amp;quot;http://schemas.hightech.ir/wpf/2008/FarsiLibrary&amp;quot;
        Title=&amp;quot;DX Editors&amp;quot; Height=&amp;quot;350&amp;quot; Width=&amp;quot;525&amp;quot;
        xmlns:loc=&amp;quot;clr-namespace:DXCustomEditor&amp;quot;&amp;gt;
    &amp;lt;Window.Resources&amp;gt;
        &amp;lt;loc:DateConverter x:Key=&amp;quot;DateConverter&amp;quot;/&amp;gt;
        &amp;lt;ControlTemplate x:Key=&amp;quot;DatePickerTemplate&amp;quot;&amp;gt;
            &amp;lt;fx:FXMonthView SelectedDateTime=&amp;quot;{Binding Path=(dxe:BaseEdit.OwnerEdit).EditValue, Mode=TwoWay, RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource DateConverter}}&amp;quot;/&amp;gt;
        &amp;lt;/ControlTemplate&amp;gt;
    &amp;lt;/Window.Resources&amp;gt;
    &amp;lt;StackPanel Margin=&amp;quot;10&amp;quot;&amp;gt;
        &amp;lt;dxe:PopupBaseEdit IsTextEditable=&amp;quot;False&amp;quot; PopupContentTemplate=&amp;quot;{StaticResource ResourceKey=DatePickerTemplate}&amp;quot; /&amp;gt;
    &amp;lt;/StackPanel&amp;gt;
&amp;lt;/Window&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
	The outcome is pretty good: seamless calendar support and look and feel.&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;
	&lt;img alt=&quot;&quot; src=&quot;http://www.hightech.ir/Images/BlogPics/ePub-Screenshot-s.png&quot; /&gt;&lt;/p&gt;</description>
            <link>http://www.hightech.ir/SeeSharp/PersianCalendar-Support-For-DevExpress</link>
            <guid isPermaLink="true">http://www.hightech.ir/SeeSharp/PersianCalendar-Support-For-DevExpress</guid>
            <pubDate>Fri, 26 Feb 2010 08:57:00 GMT</pubDate>
            <category>FarsiLibrary</category>
            <category>WPF</category>
            <category>DevExpress</category>
        </item>
        <item>
            <dc:creator>HEskandari</dc:creator>
            <title>Silverlight Binding: Lesson Learned</title>
            <description>&lt;p&gt;
	When working on a Silverlight application that recieved data from a WebService I notice that loading of an array of 1500 items takes a lot of time. With this problem, application launch that has 3 separate webservice lookups, took almost 5 minutes to complete.&lt;/p&gt;
&lt;p&gt;
	The problem became more interesting when my colleague actually ran get the same data bound UI in WinForms in 2 seconds, so there were something fishy going on in out Silverlight application. Let&amp;#39;s see the original code that loaded the data from the webservice:&lt;/p&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;
public virtual void Load()
{
    var client = new ServiceClient();
    client.GetAllFirstLookupCompleted += (s, e) =&amp;gt; Lookup.AddRange(e.Result.MapMany&amp;lt;LookupResult, LookupResultDTO&amp;gt;());
    client.GetAllFirstLookupAsync();
}

public virtual ObservableCollection&amp;lt;LookupResultDTO&amp;gt; Lookup
{
    get; private set;
}
&lt;/pre&gt;
&lt;p&gt;
	WebService call returned a &amp;quot;LookupResult&amp;quot; but we used LookupResultDTO object bound to UI so the first thing we needed to do was to convert between these types. Then our bound collection was a ObservableCollection. Unlike the same type in WPF, ObservableCollction in Silverlight has no construct that accepts an IList so you have to add individual items one by one! To make things &amp;quot;look&amp;quot; niceer, we&amp;#39;ve created an AddRange extension method that add items of an IList to an ObservableCollection.&lt;/p&gt;
&lt;p&gt;
	So, why would the above code take this long?&lt;/p&gt;
&lt;p&gt;
	&lt;span class=&quot;subtitle&quot;&gt;First Mistake&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
	First mistake was to use &lt;a href=&quot;http://www.codeplex.com/AutoMapper&quot;&gt;AutoMapper&lt;/a&gt;! Yes, it makes life easier by automagically map two unconvertable types by using reflection and flatting structure. It even works great if your collections are relatively small but avoid using it if you have a large collection (I don&amp;#39;t even consider a collection of 1000 items large!).&lt;/p&gt;
&lt;p&gt;
	You can always roll your own mappers and it is easy to create and use. Our mapper looked like this:&lt;/p&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;
public class LookupResultMapper
{
    public LookupResultDTO MapOne(LookupResult result)
    {
        return new LookupResultDTO(result.PrimaryKey, result.Name, result.Number);
    }

    public IList&amp;lt;LookupResultDTO&amp;gt; MapMany(LookupResult[] results)
    {
        var list = new List&amp;lt;LookupResultDTO&amp;gt;();

        foreach (var result in results)
        {
            list.Add(MapOne(result));
        }

        return list;
    }
}
&lt;/pre&gt;
&lt;p&gt;
	The reason is obvious! Using reflection is a lot slower and when you&amp;#39;re doing it for 1000 times, you&amp;#39;ll get 1000 times slower! This small change improved the performance more than 50%.&lt;/p&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;
public virtual void Load()
{
    var client = new ServiceClient();
    client.GetAllFirstLookupCompleted += (s, e) =&amp;gt; Lookup.AddRange(new LookupResultMapper().MapMany(e.Result));
    client.GetAllFirstLookupAsync();
}
&lt;/pre&gt;
&lt;p&gt;
	&lt;span class=&quot;subtitle&quot;&gt;Second Mistake&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
	When a UI element is bound to an observable collection, every addition and removal of an Item probably lead to some calculation in the element for things such as draing and layout calculation. Second problem was to add the collection one by one to the observable collection.&lt;/p&gt;
&lt;p&gt;
	Unfortunately, as mentioned, ObservableCollection unlike WPF version has no construct that accepts an IList. You have some options though:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;
		Make your UI element Invisible (Visibility=Collapsed or Hidden) then add the items to the bound collection. This will prevent exhaustive calculation.&lt;br /&gt;
		&amp;nbsp;&lt;/li&gt;
	&lt;li&gt;
		Change the collection type from ObservableCollection to IList, if possible.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
	&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
	Depending on your scenarios, one of the above mentioned solutions may work for you. In our case, we didn&amp;#39;t need to add / remove items from the collection at a later time so switching to IList did the trick.&lt;/div&gt;
&lt;div&gt;
	&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
	After refactoring, application that took almost 5 minutes to launch now runs in seconds!&lt;/div&gt;</description>
            <link>http://www.hightech.ir/SeeSharp/Silverlight-Binding-Lesson-Learned</link>
            <guid isPermaLink="true">http://www.hightech.ir/SeeSharp/Silverlight-Binding-Lesson-Learned</guid>
            <pubDate>Wed, 17 Feb 2010 10:00:00 GMT</pubDate>
            <category>Silverlight</category>
            <category>CodeAnalysis</category>
        </item>
        <item>
            <dc:creator>HEskandari</dc:creator>
            <title>Silverlight Applications with J2EE Backend - Part One</title>
            <description>&lt;p&gt;
	If you have not heard, &lt;a href=&quot;http://www.microsoft.com/SILVERLIGHT/&quot;&gt;Silverlight&lt;/a&gt; is the latest rich client technology from &lt;a href=&quot;http://www.microsoft.com&quot;&gt;Microsoft&lt;/a&gt;. The good thing about it is that it works almost on all browsers and operating systems and you can host your application on non-microsoft stack too, but there are things such as database connectivity that you can&amp;#39;t benefit from when creating Silverlight applications. The only way to create data-driven applications would be to use a service backend. Here I&amp;#39;ll show you how to create a J2EE backend for your Silverlight application and host it on a &lt;a href=&quot;http://www.jboss.org/&quot;&gt;JBoss&lt;/a&gt; application server.&lt;br /&gt;
	&lt;br /&gt;
	To create the backend for the Silverlight application, I&amp;#39;ll show you how you can use &lt;a href=&quot;http://java.sun.com/javaee/&quot;&gt;J2EE&lt;/a&gt; technology which provides rich infrastructure for your backend. I&amp;#39;ll be using &lt;a href=&quot;http://sourceforge.net/projects/jboss/files/JBoss/JBoss-4.2.3.GA/&quot;&gt;JBoss 4.2.3 GA&lt;/a&gt; application server to run this sample, and &lt;a href=&quot;http://www.jetbrains.com/idea/&quot;&gt;IntelliJ IDEA 9.0 Enterprise&lt;/a&gt; to develop the backend.&amp;nbsp; IntelliJ IDEA Enterprise is not a free IDE (there&amp;#39;s a &lt;a href=&quot;http://www.jetbrains.com/idea/free_java_ide.html&quot;&gt;free Community version&lt;/a&gt;), but using it is natural for .NET developers having experience with &lt;a href=&quot;http://www.jetbrains.com/resharper&quot;&gt;Resharper&lt;/a&gt; addin tool for Visual Studio .NET. Of course, both tools are from the &lt;a href=&quot;http://www.jetbrains.com/&quot;&gt;same company&lt;/a&gt;, but if you use Resharper with IntelliJ shortcuts most of them work in IntelliJ IDEA too. You can get a trial version from &lt;a href=&quot;http://www.jetbrains.com/resharper/download&quot;&gt;here&lt;/a&gt; for this experiment if you&amp;#39;re an experienced java developer, you can still use your favorite IDE.&lt;/p&gt;
&lt;p&gt;
	This post is intented for .NET developers with (almost) no experience with Java language. If you&amp;#39;re familiar with NHiberanate, theorethically you should be able to use &lt;a href=&quot;http://en.wikipedia.org/wiki/Java_Persistence_API&quot;&gt;JPA&lt;/a&gt; in no time because of a lot API similarities: JPA provides APIs that are very familiar with (N)Hiberante.&lt;br /&gt;
	&lt;br /&gt;
	&lt;span class=&quot;subtitle&quot;&gt;Project Structure&lt;/span&gt;&lt;br /&gt;
	For our backend we&amp;#39;ll use a standard J2EE project, with support for JPA, &lt;a href=&quot;http://en.wikipedia.org/wiki/Java_API_for_XML_Web_Services&quot;&gt;JAXWS&lt;/a&gt; WebServices and other J2EE services. Fire up your IntellliJ IDEA and create a new project. In the project wizard, select &amp;quot;Java Module&amp;quot; and set path for your project sources. From the technologies page in the wizard, select &amp;quot;JavaEE Persistence&amp;quot;, &amp;quot;Enterprise JavaBeans&amp;quot;, &amp;quot;JavaEE Application&amp;quot; and &amp;quot;WebServices&amp;quot;. The wizard will download the &lt;a href=&quot;http://en.wikipedia.org/wiki/JAR_(file_format)&quot;&gt;JAR&lt;/a&gt; filles and libraries that are necessary from JetBrain&amp;#39;s website and set the project structure for you. Before we&amp;#39;re done with the project structure, we need to do some modifications.&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;
	&lt;img alt=&quot;&quot; src=&quot;/Images/BlogPics/J2EEBackend_NewProject.png&quot; style=&quot;width: 642px; height: 462px;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
	Thanks to &lt;a href=&quot;http://en.wikipedia.org/wiki/Enterprise_JavaBean&quot;&gt;EJB 3.0&lt;/a&gt; and its &lt;a href=&quot;http://en.wikipedia.org/wiki/Java_annotation&quot;&gt;annotation&lt;/a&gt;-based EJBs no xml configuration would be necessary, so open &amp;quot;ejb-jar.xml&amp;quot; file and remove its content (enterprise-beans tag and its content). This file is in META-INF folder in the root of the project. Then open the &amp;quot;persistence.xml&amp;quot; from META-INF folder under the &amp;quot;src&amp;quot; folder and setup the persistence API.&lt;br /&gt;
	&lt;br /&gt;
	&lt;em&gt;Note: If you have hard time finding any class or file use the same shortcut as in Resharper (CTRL+SHIFT+N for files and CTRL+N for classes and interfaces).&lt;br /&gt;
	&lt;/em&gt;&lt;br /&gt;
	This file is almost like hibernate configuration file so most of the content should be self explanatory:&lt;/p&gt;
&lt;pre class=&quot;brush:xml&quot;&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;
&amp;lt;persistence xmlns=&amp;quot;http://java.sun.com/xml/ns/persistence&amp;quot; version=&amp;quot;2.0&amp;quot;&amp;gt;
    &amp;lt;persistence-unit name=&amp;quot;NewPersistenceUnit&amp;quot;&amp;gt;
        &amp;lt;provider&amp;gt;org.hibernate.ejb.HibernatePersistence&amp;lt;/provider&amp;gt;
        &amp;lt;jta-data-source&amp;gt;java:/silverbox-ds&amp;lt;/jta-data-source&amp;gt;
        &amp;lt;properties&amp;gt;
            &amp;lt;property name=&amp;quot;hibernate.connection.url&amp;quot; value=&amp;quot;jdbc:sqlserver://127.0.0.1;databaseName=silverboxdb&amp;quot;/&amp;gt;
            &amp;lt;property name=&amp;quot;hibernate.connection.driver_class&amp;quot; value=&amp;quot;com.microsoft.sqlserver.jdbc.SQLServerDriver&amp;quot;/&amp;gt;
            &amp;lt;property name=&amp;quot;hibernate.connection.username&amp;quot; value=&amp;quot;sa&amp;quot;/&amp;gt;
            &amp;lt;property name=&amp;quot;hibernate.connection.password&amp;quot; value=&amp;quot;12345&amp;quot;/&amp;gt;
            &amp;lt;property name=&amp;quot;hibernate.archive.autodetection&amp;quot; value=&amp;quot;class&amp;quot;/&amp;gt;
            &amp;lt;property name=&amp;quot;hibernate.show_sql&amp;quot; value=&amp;quot;true&amp;quot;/&amp;gt;
            &amp;lt;property name=&amp;quot;hibernate.format_sql&amp;quot; value=&amp;quot;true&amp;quot;/&amp;gt;
            &amp;lt;property name=&amp;quot;hbm2ddl.auto&amp;quot; value=&amp;quot;update&amp;quot;/&amp;gt;
        &amp;lt;/properties&amp;gt;
    &amp;lt;/persistence-unit&amp;gt;
&amp;lt;/persistence&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
	Other than database configuration, notice the &amp;quot;jta-data-source&amp;quot; tag which points to a specified datasource name. We&amp;#39;ll learn about this in a bit.&lt;br /&gt;
	&lt;br /&gt;
	IntelliJ Enterprise integrates with most of the application servers, including JBoss. First make sure you have a working version of JBoss application server which you can download from &lt;a href=&quot;http://sourceforge.net/projects/jboss/files/JBoss/JBoss-4.2.3.GA/&quot;&gt;here&lt;/a&gt;. Open the Settings menu (CTRL+ALT+S) and from the Application Servers add a new JBoss Server and select the folder where you unzipped the JBoss package. Then in IntelliJ open the &amp;quot;Edit Configurations&amp;quot; submenu from &amp;quot;Run&amp;quot; menu and add a local JBoss Server. The already added JBoss server will be added. Select the default &amp;quot;Server Instance&amp;quot;. Notice the warning message in the dialog that says &amp;quot;Warning: No artifacts marked for deployment&amp;quot;.&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;
	&lt;img alt=&quot;&quot; src=&quot;/Images/BlogPics/J2EEBackend_Artifact.png&quot; style=&quot;width: 556px; height: 278px;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
	&lt;em&gt;Note: Your applications may contain libraries, compiled binaries, services, etc. all will be packaged in a deployable unit. The standard deployment unit in Java is JAR (java archive) but other packages exists, namingly WAR (web archive) and SAR (service archive). All the mentioned packages can be packed and deployed in a single deployment unit named EAR (enterprise archive) file.&lt;br /&gt;
	&lt;/em&gt;&lt;br /&gt;
	To specify what should be contained in the deployable package, we need to specify the deployment artifacts. To do this, go to &amp;quot;Project Structure&amp;quot; dialog (CTRL+ALT+SHIFT+S) and select the &amp;quot;Artifact&amp;quot; from the side list. Add a new artifact by pressing &amp;quot;+&amp;quot; button. Enter a name for your artifact and select the &amp;quot;JavaEE Application: Exploded&amp;quot; as the type. From the &amp;quot;Output Layout&amp;quot; add the &amp;quot;Module Output&amp;quot; and a &amp;quot;JPA Descriptors&amp;quot;. From the bottom of the page, click &amp;quot;Create Manifest&amp;quot; button to create an empty manifest in your &amp;quot;src\META-INF&amp;quot; folder.&lt;br /&gt;
	&lt;br /&gt;
	Open up the &amp;quot;Project Structure&amp;quot; dialog again and check the &amp;quot;Build Artifacts...&amp;quot; check mark and from the dialog select the artifact name you&amp;#39;ve just created.&lt;br /&gt;
	&lt;br /&gt;
	The project structure is almost ready. Now let&amp;#39;s add some code.&lt;br /&gt;
	&lt;br /&gt;
	&lt;span class=&quot;subtitle&quot;&gt;Create Enterprise Beans&lt;/span&gt;&lt;br /&gt;
	Under the &amp;quot;src&amp;quot; folder create two &lt;a href=&quot;http://en.wikipedia.org/wiki/Java_package&quot;&gt;packages&lt;/a&gt; (a.k.a namespaces). I&amp;#39;ve named the packages as &amp;quot;ir.hightech.silverbox.common&amp;quot; and &amp;quot;ir.hightech.silverbox.services&amp;quot;. I will separate the service interfaces and implementation in separate folders. To create a basic loopback service create the following interface in the &amp;quot;common&amp;quot; package:&lt;/p&gt;
&lt;pre class=&quot;brush:java&quot;&gt;
package ir.hightech.silverbox.common;

import javax.ejb.Remote;

@Remote
public interface LoopbackService {
    void echo();
}
&lt;/pre&gt;
&lt;p&gt;
	...and create the implementation in the &amp;quot;services&amp;quot; folder:&lt;/p&gt;
&lt;pre class=&quot;brush:java&quot;&gt;
package ir.hightech.silverbox.services;

import ir.hightech.silverbox.common.LoopbackService;
import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless(name = &amp;quot;LoopbackService&amp;quot;)
@Remote(LoopbackService.class)
public class LoopbackServiceImpl implements LoopbackService{
    public void echo() {
        System.out.println(&amp;quot;Hello, world!&amp;quot;);
    }
}
&lt;/pre&gt;
&lt;p&gt;
	&lt;em&gt;Note: Each EJB can be used locally or remotely so different annotations are required based on usage context. Since we need to only expose the EJBs as a service, we&amp;#39;ve only specified it to be a &amp;quot;Remote&amp;quot; service / interface.&lt;br /&gt;
	&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
	Final step would be to expose the EJB as a Webservice. That is as easy as adding an annotation to our service implementation:&lt;/p&gt;
&lt;pre class=&quot;brush:java&quot;&gt;
@Stateless(name = &amp;quot;LoopbackService&amp;quot;)
@Remote(LoopbackService.class)
@WebService
public class LoopbackServiceImpl implements LoopbackService{
   ...
}
&lt;/pre&gt;
&lt;p&gt;
	&lt;br /&gt;
	&lt;span class=&quot;subtitle&quot;&gt;Data Sources&lt;/span&gt;&lt;br /&gt;
	Before we deploy and run the application, we need to setup the datasource specified as &amp;quot;jta-data-source&amp;quot; in our &amp;quot;persistence.xml&amp;quot; file. JBoss when ran searches the deploy folder for the specified datasource. Datasource file names are ended with &amp;quot;ds&amp;quot; suffix by convention. The datasource file contains connection information that will be used in our application. Here the &amp;quot;jndi-name&amp;quot; is important and it should be the same thing as what we&amp;#39;ve specified in persistence.xml file. To use MS-SQL 2005 database create the following file and copy the file in the &amp;quot;deploy&amp;quot; folder of the JBoss. You can create an &lt;a href=&quot;http://ant.apache.org/&quot;&gt;ANT script&lt;/a&gt; and automatically copy this file into the destination folder, if you prefer.&lt;/p&gt;
&lt;pre class=&quot;brush:java&quot;&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; standalone=&amp;quot;no&amp;quot;?&amp;gt;
&amp;lt;datasources&amp;gt;
    &amp;lt;local-tx-datasource&amp;gt;
        &amp;lt;jndi-name&amp;gt;silverbox-ds&amp;lt;/jndi-name&amp;gt;
        &amp;lt;connection-url&amp;gt;jdbc:sqlserver://127.0.0.1;databaseName=silverboxdb&amp;lt;/connection-url&amp;gt;
        &amp;lt;driver-class&amp;gt;com.microsoft.sqlserver.jdbc.SQLServerDriver&amp;lt;/driver-class&amp;gt;
        &amp;lt;user-name&amp;gt;sa&amp;lt;/user-name&amp;gt;
        &amp;lt;password&amp;gt;12345&amp;lt;/password&amp;gt;
        &amp;lt;metadata&amp;gt;
            &amp;lt;type-mapping&amp;gt;MS SQLSERVER2000&amp;lt;/type-mapping&amp;gt;
        &amp;lt;/metadata&amp;gt;
    &amp;lt;/local-tx-datasource&amp;gt;
&amp;lt;/datasources&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
	&lt;span class=&quot;subtitle&quot;&gt;Deployment&lt;/span&gt;&lt;br /&gt;
	We&amp;#39;re almost there now. From the main toolbar, click the &amp;quot;Run&amp;quot; button to fire up the JBoss server. Notice the logs that appear in Output window of the IntelliJ IDEA. If you see that artifact is deployed successfully, you&amp;#39;re good. Fire up your browser and enter the services url to see the automatically generated WSDL. The deployed URL would be http://localhost:8080/Silverbox/LoopbackService?WSDL but if you&amp;#39;ve named your artifact and service something different it is in the format of http://jbossmachine:port/artifactname/Servicename (you can also see the registered Endpoint in the output window).&lt;/p&gt;
&lt;p&gt;
	Next we&amp;#39;ll see how to extend the service and create a Silverlight client that accesses our backend. Get the source files for this part from &lt;a href=&quot;http://cid-4962b6ceabc2cbd7.skydrive.live.com/self.aspx/BlogFiles/SilverBox-PartOne.zip&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</description>
            <link>http://www.hightech.ir/SeeSharp/Silverlight-J2EE-Backend-Part-One</link>
            <guid isPermaLink="true">http://www.hightech.ir/SeeSharp/Silverlight-J2EE-Backend-Part-One</guid>
            <pubDate>Sat, 13 Feb 2010 16:21:09 GMT</pubDate>
            <category>Silverlight</category>
            <category>J2EE</category>
            <category>Java</category>
        </item>
        <item>
            <dc:creator>HEskandari</dc:creator>
            <title>Must Read Books For Programmers</title>
            <description>&lt;p&gt;
	It&amp;#39;s been a while that I wanted to prepare a list of books that, in my opinion, every programmer should read. These are the most influential books I&amp;#39;ve read so far in my career. It is not supposed to be a complete list of any kind and this is just my idea of a must read. Books mentioned here may or may not be specific to a programming language, so check it before you buy the book.&lt;/p&gt;
&lt;p&gt;
	With the fact (or is it a fiction?) that &lt;a href=&quot;http://www.codinghorror.com/blog/archives/001108.html&quot;&gt;programmers don&amp;#39;t like to read books&lt;/a&gt; in mind, I limited the list to only ten books, but there are certainly other great books out there that either I&amp;#39;m not aware of or may be have not read yet. I was already a programmer when I read most of these books, but these really helped me open my mind towards how professionals are doing it and why.&lt;/p&gt;
&lt;p&gt;
	Enough said, here&amp;#39;s the list:&lt;/p&gt;
&lt;p&gt;
	&lt;a href=&quot;http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612&quot; target=&quot;_blank&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;../../../Images/url_icon.gif&quot; style=&quot;width: 13px; height: 13px;&quot; /&gt;&lt;/a&gt; 10- &lt;strong&gt;Design Patterns: Elements of Reusable Object-Oriented Software&lt;/strong&gt; by &lt;em&gt;Gang of Four&lt;/em&gt;&lt;br /&gt;
	&lt;a href=&quot;http://www.amazon.com/gp/aw/d.html?a=0132350882&quot; target=&quot;_blank&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;../../../Images/url_icon.gif&quot; style=&quot;width: 13px; height: 13px;&quot; /&gt;&lt;/a&gt; 9- &lt;strong&gt;Clean Code A Handbook of Agile Software Craftsmanship&lt;/strong&gt; by &lt;em&gt;Robert C. Martin&lt;/em&gt;&lt;br /&gt;
	&lt;a href=&quot;http://www.amazon.com/Agile-Project-Management-Microsoft-Professional/dp/073561993X&quot; target=&quot;_blank&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;../../../Images/url_icon.gif&quot; style=&quot;width: 13px; height: 13px;&quot; /&gt;&lt;/a&gt; 8- &lt;strong&gt;Agile Project Management with Scrum&lt;/strong&gt; by &lt;em&gt;Ken Schwaber&lt;/em&gt;&lt;br /&gt;
	&lt;a href=&quot;http://www.amazon.com/Applying-Domain-Driven-Design-Patterns-Examples/dp/0321268202&quot; target=&quot;_blank&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;../../../Images/url_icon.gif&quot; style=&quot;width: 13px; height: 13px;&quot; /&gt;&lt;/a&gt; 7- &lt;strong&gt;Applying Domain-Driven Design and Patterns&lt;/strong&gt; by &lt;em&gt;Jimmy Nilsson&lt;/em&gt;&lt;br /&gt;
	&lt;a href=&quot;http://www.amazon.com/gp/aw/d.html?a=0131177052&quot; target=&quot;_blank&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;../../../Images/url_icon.gif&quot; style=&quot;width: 13px; height: 13px;&quot; /&gt;&lt;/a&gt; 6- &lt;strong&gt;Working Effectively with Legacy Code&lt;/strong&gt; by &lt;em&gt;Michael Feathers&lt;/em&gt;&lt;br /&gt;
	&lt;a href=&quot;http://www.amazon.com/Patterns-Enterprise-Application-Architecture-Martin/dp/0321127420&quot; target=&quot;_blank&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;../../../Images/url_icon.gif&quot; style=&quot;width: 13px; height: 13px;&quot; /&gt;&lt;/a&gt; 5- &lt;strong&gt;Patterns of Enterprise Application Architecture&lt;/strong&gt; by &lt;em&gt;Martin Fowler&lt;/em&gt;&lt;br /&gt;
	&lt;a href=&quot;http://www.amazon.com/Refactoring-Improving-Design-Existing-Code/dp/0201485672&quot; target=&quot;_blank&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;../../../Images/url_icon.gif&quot; style=&quot;width: 13px; height: 13px;&quot; /&gt;&lt;/a&gt; 4- &lt;strong&gt;Refactoring: Improving the Design of Existing Code&lt;/strong&gt; by &lt;em&gt;Martin Fowler&lt;/em&gt;&lt;em&gt;&lt;br /&gt;
	&lt;/em&gt;&lt;a href=&quot;http://www.amazon.com/gp/aw/d.html?a=0321146530&quot; target=&quot;_blank&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;../../../Images/url_icon.gif&quot; style=&quot;width: 13px; height: 13px;&quot; /&gt;&lt;/a&gt; 3- &lt;strong&gt;Test Driven Development By Example&lt;/strong&gt; by &lt;em&gt;Kent Beck&lt;/em&gt;&lt;br /&gt;
	&lt;a href=&quot;http://www.amazon.com/Agile-Principles-Patterns-Practices-C/dp/0131857258&quot; target=&quot;_blank&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;../../../Images/url_icon.gif&quot; style=&quot;width: 13px; height: 13px;&quot; /&gt;&lt;/a&gt; 2- &lt;strong&gt;Agile Principles Patterns and Practices&lt;/strong&gt; by &lt;em&gt;Robert C. Martin&lt;/em&gt;&lt;br /&gt;
	&lt;a href=&quot;http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215&quot; target=&quot;_blank&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;../../../Images/url_icon.gif&quot; style=&quot;width: 13px; height: 13px;&quot; /&gt;&lt;/a&gt; 1- &lt;strong&gt;Domain-Driven Design: Tackling Complexity in the Heart of Software&lt;/strong&gt; by &lt;em&gt;Eric Evans&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
	Thoughts?&lt;/p&gt;</description>
            <link>http://www.hightech.ir/SeeSharp/Must-Read-Books-For-Programmers</link>
            <guid isPermaLink="true">http://www.hightech.ir/SeeSharp/Must-Read-Books-For-Programmers</guid>
            <pubDate>Thu, 21 Jan 2010 08:00:00 GMT</pubDate>
            <category>Books</category>
            <category>Programming</category>
        </item>
        <item>
            <dc:creator>HEskandari</dc:creator>
            <title>Java and .NET Interoperability</title>
            <description>&lt;p&gt;
	We&amp;#39;ve been doing a lot of Java / .NET &lt;a href=&quot;http://en.wikipedia.org/wiki/Interoperability&quot;&gt;interop&lt;/a&gt; for last couple of years in our enterprise applications. On one hand, our customers used their existing expertise and infrastructure that worked better with Java based technologies like their existing Unix based servers, Sybase database (on Unix) and the knowledge of their DBA team, and we using our existing Java developers and expertise. But what Java based technology provides that we don&amp;#39;t have on .NET world? Is it important at all?&lt;br /&gt;
	&lt;br /&gt;
	With rise of Silverlight presentation framework working the interop becomes more important. You can now create a sophisticated user interface and make it work with your existing EJBs on your JBoss backend and you have best of both worlds. As a .NET developer you might not be familiar what this provides, so here&amp;#39;s a few things you&amp;#39;ll get right out of the box:&lt;br /&gt;
	&lt;br /&gt;
	&lt;span class=&quot;subtitle&quot;&gt;Standard JPA (Java Persistance API)&lt;/span&gt;&lt;br /&gt;
	When you create applications on top of &lt;a href=&quot;http://java.sun.com/javaee/reference/faq/ejb.jsp&quot;&gt;EJB 3.0&lt;/a&gt; using any EJB compliant application server, one thing you get is a standard, pluggable persistence API. The idea behind &lt;a href=&quot;http://en.wikipedia.org/wiki/Java_Persistence_API&quot;&gt;JPA&lt;/a&gt; was to provide a standard set of interfaces to application developers and let the application server provide different implementations of these interfaces using various persistence frameworks. Great idea, right? This means you don&amp;#39;t need to learn how to use another persistence framework, because now they all support an standard interface.&lt;/p&gt;
&lt;p&gt;
	&lt;a href=&quot;http://www.jboss.org/&quot;&gt;JBoss&lt;/a&gt; actually uses &lt;a href=&quot;https://www.hibernate.org/&quot;&gt;Hiberante&lt;/a&gt; under the hood to implement this API but other application servers may have different engines. If you&amp;#39;re familiar with NHibernate, you&amp;#39;ll feel right at home!&lt;br /&gt;
	&lt;br /&gt;
	&lt;span class=&quot;subtitle&quot;&gt;JAX-WS WebServices&lt;/span&gt;&lt;br /&gt;
	Should you need to expose your business logic through WebServices, all you need to do is to decorate your existing services with an &lt;a href=&quot;http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html&quot;&gt;Annotation&lt;/a&gt; to signify the application service that the class provides a WebService endpoint. An &lt;em&gt;Annotation&lt;/em&gt; is actually a Java term for almost the same thing we call an &lt;em&gt;Attribute&lt;/em&gt; in .NET world.&lt;br /&gt;
	&lt;br /&gt;
	&lt;span class=&quot;subtitle&quot;&gt;Infrastructural Services&lt;/span&gt;&lt;br /&gt;
	There are a lot of cross cutting concerns your application server provides. Things such as standard logging framework, inversion of control container, queues, scheduling, &lt;a href=&quot;http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/EJBConcepts5.html&quot;&gt;Message Driven Bean&lt;/a&gt; (which is a very simple &lt;a href=&quot;http://en.wikipedia.org/wiki/Enterprise_service_bus&quot;&gt;ESB&lt;/a&gt;) , hot deployment of applications, etc.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
	&lt;span class=&quot;subtitle&quot;&gt;Price Tag&lt;/span&gt;&lt;br /&gt;
	Yes, the best part of it is that all of this comes as free (as in free food). You don&amp;#39;t have to pay for licenses for your application server, you don&amp;#39;t have to pay for IDEs because there are a lot of great IDEs available, you don&amp;#39;t have to buy license for your Linux based Server and there&amp;#39;s a large community out there that can help you freely, if you&amp;#39;re stuck. Of course there are paid services too, mostly for services such as support, but you hardly need it. If you&amp;#39;re pairing Silverlight frontend with Java backend, you can use &lt;a href=&quot;http://www.eclipse4sl.org&quot;&gt;Eclipse&lt;/a&gt; to handle both in one place!&lt;/p&gt;
&lt;p&gt;
	This is just a scratch on the surface. I&amp;#39;ll shed more light on the subject on upcoming posts.&lt;/p&gt;</description>
            <link>http://www.hightech.ir/SeeSharp/Java-Interop-With-NET</link>
            <guid isPermaLink="true">http://www.hightech.ir/SeeSharp/Java-Interop-With-NET</guid>
            <pubDate>Mon, 18 Jan 2010 00:00:00 GMT</pubDate>
            <category>NET</category>
            <category>Java</category>
            <category>Interop</category>
        </item>
        <item>
            <dc:creator>HEskandari</dc:creator>
            <title>WPF Application with Caliburn - Part Two</title>
            <description>&lt;p&gt;
	In this part of the tutorial, we&amp;#39;ll enhance our shell view to display other presenters and add Save and Print support for presenters supporting it. You can use generalize this example and learn how to implement generic features in your shell.&lt;/p&gt;
&lt;p&gt;
	Caliburn has several existing classes that can be used as a base class for our presenters. Let&amp;#39;s explain some of them and see where do they fit:&lt;/p&gt;
&lt;p&gt;
	&lt;span style=&quot;color: rgb(255, 140, 0);&quot;&gt;&lt;em&gt;&lt;strong&gt;Presenter&lt;/strong&gt;&lt;/em&gt;&lt;/span&gt;&lt;br /&gt;
	This class implements IPresenter interface and provides life time methods such as Initialize, Shutdown, Active and Deactive. You can use this class for other views that are going to be displayed in the Shell View.&lt;/p&gt;
&lt;p&gt;
	&lt;span style=&quot;color: rgb(255, 140, 0);&quot;&gt;&lt;em&gt;&lt;strong&gt;Presenter Manager&lt;/strong&gt;&lt;/em&gt;&lt;/span&gt;&lt;br /&gt;
	Using a presenter manager, as the name implies, you can host and display a single instance of a Presenter. It only allows displaying of one Presenter only so you have only one view displayed to the user when using it.&lt;/p&gt;
&lt;p&gt;
	&lt;span style=&quot;color: rgb(255, 140, 0);&quot;&gt;&lt;em&gt;&lt;strong&gt;MultiPresenterManager&lt;/strong&gt;&lt;/em&gt;&lt;/span&gt;&lt;br /&gt;
	Same as PresenterManager but can host multiple Presenters at the same time, but only one of them is Active at a time.&lt;/p&gt;
&lt;p&gt;
	When we display a Presenter, the PresenterManager will call the lifetime methods on the Presenter instance. The following diagram shows this flow:&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;
	&lt;img alt=&quot;&quot; src=&quot;/Images/BlogPics/Caliburn_PartTwo_Presenter.png&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
	For now, it is obvious that we need to change the base class of our &lt;em&gt;ShellViewModel&lt;/em&gt; to &lt;em&gt;PresenterManager&lt;/em&gt; (too keep it simple for the time being), but how should we change the xaml code to host the views (other Presenters)? Your first guess would be to bind &lt;em&gt;CurrentPresenter&lt;/em&gt; property of the &lt;em&gt;PresenterManager&lt;/em&gt; to a &lt;em&gt;ContentControl&lt;/em&gt; in the Shell.xaml, but you don&amp;#39;t need to do that as Caliburn will do the plumbing for us, just add a &lt;em&gt;ContentControl&lt;/em&gt; to the Shell view where you want the views to be displayed and name it as &amp;quot;CurrentPresenter&amp;quot;.&lt;/p&gt;
&lt;p&gt;
	To make Caliburn bind Views to the content control, you need to enable additional &lt;em&gt;Conventions&lt;/em&gt; that are not enabled by default. Placing the following snippet in Caliburn configuration will do the trick:&lt;/p&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;
var binder = (DefaultBinder)Container.GetInstance&amp;lt;IBinder&amp;gt;();
binder.EnableBindingConventions();
&lt;/pre&gt;
&lt;p&gt;
	The bare bone of the shell view that displays a single presenter is this:&lt;/p&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;
&amp;lt;Window x:Class=&amp;quot;PayRoll.Views.ShellView&amp;quot;
    xmlns=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;
    xmlns:x=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot; Width=&amp;quot;640&amp;quot; Height=&amp;quot;400&amp;quot;&amp;gt;
    &amp;lt;DockPanel LastChildFill=&amp;quot;True&amp;quot;&amp;gt;

        &amp;lt;ScrollViewer&amp;gt;
            &amp;lt;ContentControl x:Name=&amp;quot;CurrentPresenter&amp;quot; /&amp;gt;
        &amp;lt;/ScrollViewer&amp;gt;

    &amp;lt;/DockPanel&amp;gt;
&amp;lt;/Window&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
	&lt;span class=&quot;subtitle&quot;&gt;&lt;br /&gt;
	Adding Custom Controls&lt;/span&gt;&lt;br /&gt;
	Now to handle user interaction with the shell and the open presenter, we need some kind of a Toolbar / Menu control. WPF comes with a very basic menu and toolbar control, but to show how to use custom controls and see how Caliburn facilitates this, let&amp;#39;s use DevExpress BarManager control in this example.&lt;/p&gt;
&lt;p&gt;
	DevExpress, among other nice things, provides presentation layer controls for WPF such as Editors, Bars (MenuBar, ToolBar and soon Ribbon), Grid and Printing. One other thing they&amp;#39;ve also done is that they also have skinned native WPF controls so if used together with DevExpress controls, you&amp;#39;ll have a seemless look and feel.&lt;/p&gt;
&lt;p&gt;
	To add the BarManager control to our ShellView, you need to download DevExpress WPF controls (trial version here) and install it. The add the necessary reference to devexpress WPF assemblies and import the xml namespaces. Here&amp;#39;s the snippet that creates the BarButtonItems which is equivalent of a ToolbarItem. (Get the accompanied zip for complete source code):&lt;/p&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;
&amp;lt;dxb:BarManager.Items&amp;gt;
	&amp;lt;!--General Actions--&amp;gt;
	&amp;lt;dxb:BarButtonItem Name=&amp;quot;bSave&amp;quot;  CategoryName=&amp;quot;File&amp;quot; Content=&amp;quot;Save&amp;quot; Glyph=&amp;quot;/Images/Save.png&amp;quot;  KeyGesture=&amp;quot;Ctrl+S&amp;quot; /&amp;gt;
	&amp;lt;dxb:BarButtonItem Name=&amp;quot;bPrint&amp;quot; CategoryName=&amp;quot;File&amp;quot; Content=&amp;quot;Print&amp;quot; Glyph=&amp;quot;/Images/Print.png&amp;quot; KeyGesture=&amp;quot;Ctrl+P&amp;quot; /&amp;gt;
	&amp;lt;dxb:BarButtonItem Name=&amp;quot;bExit&amp;quot;  CategoryName=&amp;quot;File&amp;quot; Content=&amp;quot;Exit&amp;quot; Glyph=&amp;quot;/Images/Exit.png&amp;quot; KeyGesture=&amp;quot;Ctrl+X&amp;quot; /&amp;gt;
	
	&amp;lt;!--Status Bar--&amp;gt;
	&amp;lt;dxb:BarStaticItem Name=&amp;quot;bStatus&amp;quot; CategoryName=&amp;quot;Status&amp;quot; MinWidth=&amp;quot;300&amp;quot; Content=&amp;quot;Read&amp;quot; AutoSizeMode=&amp;quot;Fill&amp;quot;/&amp;gt;
&amp;lt;/dxb:BarManager.Items&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
	Same as Button object, BarButtonItems provide a Command property which you can bind to an ICommand instance on your ViewModel, but this is rather limited, don&amp;#39;t you think? What if you want to bind another event (other then Click) to your ViewModel?&lt;/p&gt;
&lt;p&gt;
	Caliburn provides Actions and Messages with which you can bind a regular Event on your View and Controls to a method on your ViewModel, without using ICommand. You can even run the event handler (on your VM) asynchronously! There are a few ways to do this but the best way, in my opinion, is to this format:&lt;/p&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;
cal:Message.Attach=&amp;quot;[Event EventNameOnControl] = [Action ActionNameOnViewModel()]&amp;quot;
&lt;/pre&gt;
&lt;p&gt;
	Note that you can actually bind two actions to one event, just like you can have two methods listening to a regular event. Let&amp;#39;s change the snippet above and add the messaging support:&lt;/p&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;
&amp;lt;dxb:BarButtonItem Name=&amp;quot;bSave&amp;quot; cal:Message.Attach=&amp;quot;[Event ItemClick] = [Action SavePresenter()]&amp;quot;/&amp;gt;
	&amp;lt;dxb:BarButtonItem Name=&amp;quot;bPrint&amp;quot; cal:Message.Attach=&amp;quot;[Event ItemClick] = [Action PrintPresenter()]&amp;quot;/&amp;gt;
	&amp;lt;dxb:BarButtonItem Name=&amp;quot;bExit&amp;quot; cal:Message.Attach=&amp;quot;[Event ItemClick] = [Action Exit()]&amp;quot; /&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
	Note: Unlike a regular Button, BarButtonItem has no &amp;quot;Click&amp;quot; event and provides an &amp;quot;ItemClick&amp;quot; event instead. Clearly, we need the specified methods on our ViewModel, so let&amp;#39;s add them too:&lt;/p&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;
public class ShellViewModel : PresenterManager, IShellViewModel
{
    public virtual void SavePresenter()
    {
    }

    public virtual void PrintPresenter()
    {
    }

    public virtual void Exit()
    {
    }
}
&lt;/pre&gt;
&lt;p&gt;
	At this point, you can run the shell and click the tool bar items, which will call the specified method on the view model. Now the question is, what if a presenter is not in a suitable state to be saved (dirty checking, already saved, etc.). What if a presenter does not even support printing? How can we disable the BarButtonItem responsible for an action, if the presenter does not support it?&lt;/p&gt;
&lt;p&gt;
	With Conventions already enabled, you can have additional &amp;quot;Can*&amp;quot; methods acting as a Filter for your actions. These filters will be checked on runtime for returning false value indicating the action can not be performed, or true value for allowed actions. Caliburn will try to disable the piece of UI through an &amp;quot;IAvailabilityEffect&amp;quot; interface. Let&amp;#39;s see this in more dept.&lt;br /&gt;
	&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
	&lt;span class=&quot;subtitle&quot;&gt;Working with Actions&lt;/span&gt;&lt;br /&gt;
	As mentioned, Caliburn checks for availability of an action automagically if it finds a pairing &amp;quot;Can*&amp;quot; filter method on the presenter. So let&amp;#39;s add those and see how it works:&lt;/p&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;
public virtual bool CanExit()
{
    return base.CanShutdown();
}

public virtual bool CanPrintPresenter()
{
    var presenter = CurrentPresenter as IPrintablePresenter;
    return presenter != null &amp;amp;&amp;amp;
           presenter.CanPrint;
}

public virtual bool CanSavePresenter()
{
    var presenter = CurrentPresenter as ISaveablePresenter;
    return presenter != null &amp;amp;&amp;amp;
           presenter.CanSave;
}
&lt;/pre&gt;
&lt;p&gt;
	When you run the application and set a break point on CanSave and Save methods, you&amp;#39;ll notice two things:&lt;/p&gt;
&lt;p&gt;
	&lt;em&gt;A. While CanPrint method returns False value, UI element (BarButtonItem) is not disabled!&lt;br /&gt;
	&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
	&lt;em&gt;B. By clicking the BarButtonItem, the Save method will not be called.&lt;br /&gt;
	&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
	So there seems to be a problem...Why isn&amp;#39;t BarButtonItem disabled? Wasn&amp;#39;t Caliburn supposed to do that for us? Well...The default &lt;em&gt;&lt;strong&gt;&amp;quot;Disable&amp;quot;&lt;/strong&gt;&lt;/em&gt; effect, only works for &lt;em&gt;UIElements&lt;/em&gt;, but &lt;em&gt;BarButtonItem&lt;/em&gt; is actually a &lt;em&gt;FrameworkContentElement&lt;/em&gt;. Those are two different beasts and Caliburn can not handle our UI control. The solution is easy though. You can create your own &lt;strong&gt;&lt;em&gt;IAvailabilityEffect&lt;/em&gt;&lt;/strong&gt; that works with &lt;em&gt;FrameworkContentElements&lt;/em&gt;.&lt;/p&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;
public class DisableMenuEffect : IAvailabilityEffect
{
    public void ApplyTo(DependencyObject target, bool isAvailable)
    {
        var element = target as FrameworkContentElement;
        if(element == null)
            return;

        element.IsEnabled = isAvailable;
    }
}
&lt;/pre&gt;
&lt;p&gt;
	...and set it along with the action on the View:&lt;/p&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;
&amp;lt;dxb:BarButtonItem cal:Message.Attach=&amp;quot;[Event ItemClick] = [Action SavePresenter()]&amp;quot; cal:Message.AvailabilityEffect=&amp;quot;DisableMenuEffect&amp;quot; /&amp;gt;
    &amp;lt;dxb:BarButtonItem cal:Message.Attach=&amp;quot;[Event ItemClick] = [Action PrintPresenter()]&amp;quot; cal:Message.AvailabilityEffect=&amp;quot;DisableMenuEffect&amp;quot; /&amp;gt;
    &amp;lt;dxb:BarButtonItem cal:Message.Attach=&amp;quot;[Event ItemClick] = [Action Exit()]&amp;quot; cal:Message.AvailabilityEffect=&amp;quot;DisableMenuEffect&amp;quot; /&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
	There&amp;#39;s one last trick to make this work. Caliburn has no way to convert the specified string to an IAvailabilityEffect instance! The existing ValueCovertor, asks our Container for any object named &amp;quot;DisableMenuEffect&amp;quot;. The final piece of code would be to register all instances of IAvailabilityEffects you might want to use on the container:&lt;/p&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;
_container.Register(Component.For&amp;lt;DisableMenuEffect&amp;gt;()
                             .Named(&amp;quot;DisableMenuEffect&amp;quot;)
                             .LifeStyle.Singleton);
&lt;/pre&gt;
&lt;p&gt;
	When running the application, and there&amp;#39;s no open view, the toolbar items are disabled.&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;
	&lt;img alt=&quot;&quot; src=&quot;/Images/BlogPics/Caliburn_PartTwo_DisabledToolbar.png&quot; style=&quot;width: 400px; height: 280px;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
	To show how it works, let&amp;#39;s add a new View and ViewModel to our application, and also add a button to the shell that allows us to open this view. As mentioned before, to do this, you need to create a UserControl for you View and a class deriving from Presenter base class for your ViewModel.&lt;/p&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;
public class PersonViewModel : Presenter
{
    private Person _person;
    private ObservableCollection&amp;lt;Person&amp;gt; _personnelList;
    private ObservableCollection&amp;lt;Job&amp;gt; _jobs;
    private readonly IPersonDataService _service;

    public PersonViewModel(IPersonDataService service)
    {
        _service = service;
    }

    public override void Initialize()
    {
        _jobs = new ObservableCollection&amp;lt;Job&amp;gt;(_service.GetJobs());
        _personnelList = new ObservableCollection&amp;lt;Person&amp;gt;(_service.GetPersonnel());
    }

    public Person CurrentPerson
    {
        get { return _person; }
        set
        {
            _person = value;
            base.NotifyOfPropertyChange(&amp;quot;CurrentPerson&amp;quot;);
        }
    }

    public ObservableCollection&amp;lt;Person&amp;gt; Personnel
    {
        get { return _personnelList; }
    }

    public ObservableCollection&amp;lt;Job&amp;gt; Jobs
    {
        get { return _jobs; }
    }
}
&lt;/pre&gt;
&lt;p&gt;
	Now create a UserControl for PersonView using DevExpress editors looking like this:&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;
	&lt;img alt=&quot;&quot; src=&quot;/Images/BlogPics/Caliburn_PartTwo_PersonView.png&quot; style=&quot;width: 533px; height: 272px;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
	To support saving, let&amp;#39;s add ISaveablePresenter interface to our ViewModel and check if DateOfBirth property of the current person is valid before saving:&lt;/p&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;
public bool CanSave
{
    get 
    {
        return CurrentPerson != null &amp;amp;&amp;amp;
               CurrentPerson.DateOfBirth.Year &amp;gt; 1900;
    }
}

public void Save()
{
    MessageBox.Show(&amp;quot;Saved&amp;quot;);
}
&lt;/pre&gt;
&lt;p&gt;
	You can observe that CanSave property will never get executed! The reason is that when application starts, Caliburn checks if any of the Action methods are available by checking their respective Filters (here CanSavePresenter and CanPrintPresenter), but it never checks them again. In other words, by default action filters (Can* methods) are only called once. How can we change this behavior?&lt;/p&gt;
&lt;p&gt;
	There&amp;#39;s an attribute called &lt;em&gt;&lt;strong&gt;&amp;quot;AutoCheckAvailability&amp;quot;&lt;/strong&gt;&lt;/em&gt; that you can place on your action methods. This will tell WPF Command Manager to look out for availability of the bound action. Doing this will call the action filters periodically, so make sure if you really need it before actually using it. So apply this to our ViewModel, let&amp;#39;s get back to our ShellViewModel class and place the attribute on our actions:&lt;/p&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;
[AutoCheckAvailability]
public virtual void SavePresenter()
{
    var presenter = (ISaveablePresenter)CurrentPresenter;
    presenter.Save();
}

[AutoCheckAvailability]
public virtual void PrintPresenter()
{
    var presenter = (IPrintablePresenter)CurrentPresenter;
    presenter.Print();
}
&lt;/pre&gt;
&lt;p&gt;
	&lt;em&gt;&lt;strong&gt;Note: You need to place this on your actions, not on filters.&lt;br /&gt;
	&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
	...et voila!&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;
	&lt;img alt=&quot;&quot; src=&quot;/Images/BlogPics/Caliburn_PartTwo_Application_View.png&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
	Our shell now can host other views and has generic actions that is automagically enabled / disabled for views supporting it.&lt;/p&gt;
&lt;p&gt;
	In the next post, I&amp;#39;ll show you how to change our existing shell to display a Tabbed interface and display multiple views at the same time. The &lt;a href=&quot;http://cid-4962b6ceabc2cbd7.skydrive.live.com/self.aspx/BlogFiles/PayRoll%20-%20Caliburn%20Part%20Two.zip&quot;&gt;attached zip file&lt;/a&gt; contains the complete source code.&lt;/p&gt;</description>
            <link>http://www.hightech.ir/SeeSharp/WPF-Application-With-Caliburn-Part-Two</link>
            <guid isPermaLink="true">http://www.hightech.ir/SeeSharp/WPF-Application-With-Caliburn-Part-Two</guid>
            <pubDate>Fri, 15 Jan 2010 00:00:00 GMT</pubDate>
            <category>MVVM</category>
            <category>WPF</category>
            <category>Caliburn</category>
        </item>
        <item>
            <dc:creator>HEskandari</dc:creator>
            <title>WPF Application with Caliburn - Part One</title>
            <description>&lt;p&gt;Back in May 2008, I did &lt;a href=&quot;http://www.hightech.ir/SeeSharp/wpf-composite-application-with-caliburn-part-one&quot;&gt;two posts&lt;/a&gt; on how to implement a composite application using &lt;a href=&quot;http://caliburn.codeplex.com/&quot;&gt;Caliburn&lt;/a&gt; framework. By that time, Caliburn was still in pre-alpha stage. With lots of the changes along the way to reach &lt;a href=&quot;http://caliburn.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=32809&quot;&gt;Release Candidate&lt;/a&gt;, those content are not valid anymore, so I thought a new post to show how you can use Caliburn to build a WPF application is in order.&lt;br/&gt;&lt;br/&gt;

In fact, a while ago I started porting my old .NET 1.1 pay roll application to WPF and .NET 3.5 so I naturally chose to use Caliburn and it did facilitate things a lot, but to keep things simple, I'm not going to discuss my own application here. I'll show how you how can create your own application using Caliburn framework and how it will help you out.&lt;/p&gt;

&lt;span class=&quot;subtitle&quot;&gt;Framework Setup&lt;/span&gt;
&lt;p&gt;
First thing you need to do, is to setup a new WPF application. Let's call it PayRoll application. After doing so, add reference to &lt;a href=&quot;http://caliburn.codeplex.com&quot;&gt;Caliburn&lt;/a&gt; and &lt;a href=&quot;http://www.castleproject.org/container/index.html&quot;&gt;Castle&lt;/a&gt; assemblies. Caliburn honors the &lt;a href=&quot;http://en.wikipedia.org/wiki/Dependency_inversion_principle&quot;&gt;Dependency-Injection principle&lt;/a&gt; by depending only on abstractions. The idea is to allow you to easily plug in your own implementation of that abstraction and customize how Caliburn components work. To do so, Caliburn uses an &lt;a href=&quot;http://en.wikipedia.org/wiki/Inversion_of_control&quot;&gt;IoC Container&lt;/a&gt; to lookup the services it requires.&lt;br/&gt;&lt;br/&gt;

If you're new to &lt;a href=&quot;http://martinfowler.com/articles/injection.html&quot;&gt;Dependency Injection&lt;/a&gt; stuff, don't worry, there is a built-in &lt;em&gt;SimpleContainer&lt;/em&gt; which will do the job for you but I'll be using Castle container (Windsor) here. If you're using other IoC containers such as &lt;a href=&quot;http://code.google.com/p/autofac/&quot;&gt;AutoFac&lt;/a&gt;, &lt;a href=&quot;http://ninject.org/&quot;&gt;Ninject&lt;/a&gt;, &lt;a href=&quot;http://structuremap.sourceforge.net/&quot;&gt;StructureMap&lt;/a&gt;, &lt;a href=&quot;http://www.springframework.net/&quot;&gt;Spring&lt;/a&gt; or even &lt;a href=&quot;http://www.codeplex.com/MEF&quot;&gt;MEF&lt;/a&gt; (&lt;a href=&quot;http://devlicio.us/blogs/casey/archive/2009/12/18/what-is-the-difference-between-an-ioc-container-and-mef.aspx&quot;&gt;not technically an IoC container&lt;/a&gt;), Caliburn comes with existing adapters for those too, so no worries. To lookup instances of the services it needs, Caliburn uses the &lt;a href=&quot;http://www.codeplex.com/CommonServiceLocator&quot;&gt;Common ServiceLocator&lt;/a&gt; and &lt;a href=&quot;http://en.wikipedia.org/wiki/Service_locator_pattern&quot;&gt;Service Locator&lt;/a&gt; design pattern. This allows looking up for services from various IoC containers, without actually depending on those containers.&lt;br/&gt;&lt;br/&gt;

Now to initialize Caliburn framework at startup there are various ways, but the easiest would be to create a CaliburnApplication. We already have an application object (App.xaml and App.xaml.cs) so we only need to change them to derive from CaliburnApplication class. To do so, open the App.xaml file first, and include the Caliburn namespace and change the type to Caliburn application. Also, we don't want to specify any StartupUri, so get rid of that too. The result would be this:&lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
&amp;lt;cal:CaliburnApplication x:Class=&quot;PayRoll.App&quot;
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
    xmlns:cal=&quot;http://www.caliburnproject.org&quot;&amp;gt;
    &amp;lt;Application.Resources&amp;gt;
         
    &amp;lt;/Application.Resources&amp;gt;
&amp;lt;/cal:CaliburnApplication&amp;gt;
&lt;/pre&gt;

&lt;p&gt;With this, the project won't compile! Because we've specified different base classes in our Xaml and Code Behind. Since the base class in code behind is inferred, open the code behind and remove the base class. The code would look like this:&lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
namespace PayRoll
{
    /// &amp;lt;summary&gt;
    /// Interaction logic for App.xaml
    /// &amp;lt;/summary&gt;
    public partial class App
    {
    }
}
&lt;/pre&gt;

&lt;p&gt;So far so good. Now, If you look at the Caliburn Application class, there are interesting things that we can do. The obvious one is a method called CreateContainer. The default implementation, as said before, will create a &lt;em&gt;SimpleContainer&lt;/em&gt; but we're going to use our own container, so let's do so:&lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
    protected override IServiceLocator CreateContainer()
    {
        _container = new WindsorContainer();
        return new WindsorAdapter(_container);
    }
&lt;/pre&gt;

&lt;p&gt;Next step would be specifying a RootViewModel (also called ShellViewModel) that will act as ViewModel for our MainWindow. Notice that we have nothing to do with the actual View (UI) that will be displayed, we just specify the ViewModel for it:&lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
    protected override object CreateRootModel()
    {
        return _container.Resolve&amp;lt;IShellViewModel&amp;gt;();
    }
&lt;/pre&gt;

&lt;p&gt;At this point, we need to create a ViewModel and a View for our Shell and register the VM in the container. Create two folders in your solution one named &lt;em&gt;Views&lt;/em&gt; and the other &lt;em&gt;ViewModels&lt;/em&gt;. Create an interface in ViewModels folder named IShellViewModel and create a class implementing this yet empty interface, named ShellViewModel. So now we need to register these (along with other services, view models, etc.) in the container:&lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
    public App()
    {
        RegisterComponents();
    }

    private void RegisterComponents()
    {
        _container.Register(Component.For&amp;lt;IShellViewModel&amp;gt;()
                                     .ImplementedBy&amp;lt;ShellViewModel&amp;gt;()
                                     .LifeStyle.Singleton);
    }
&lt;/pre&gt;
	
&lt;p&gt;Notice that lifestyle of ShellViewModel is set to singleton. This is logical because there's always one instance of the ShellView running. We'll see how this allow us interact from other ViewModels with Shell VM.&lt;/p&gt;

&lt;span class=&quot;subtitle&quot;&gt;Marrying the View and ViewModel&lt;/span&gt;
&lt;p&gt;Now, let's run the application and see what happens. When we run the application, Caliburn throws an exception complaining that a View can not be created for the ShellViewModel. Since we didn't specify any information about our Views, you might say: How is Caliburn supposed to create a View for my ViewModel anyway? Well, Caliburn tries many things before giving up and throwing an exception:&lt;/p&gt;

&lt;ul&gt;
   &lt;li&gt;First, if your ViewModel implements the IMetadataContainer interface, the GetMetadata&lt;T&gt; method is called on your VM, asking for a view. Since this not a good option for us, let's skip it.&lt;/li&gt;
   &lt;li&gt;Second, if the ViewModel is decorated with a ViewAttribute, the specified view will be created. This is relatively easy, but it clutters your ViewModel with an attribute and I don't like it.&lt;/li&gt;
   &lt;li&gt;Third, specified assemblies will be searched to find the matching view according to a specified naming convention. This sound interesting, let's use this method.&lt;/li&gt;
   &lt;li&gt;Fourth, if you don't like any of these behaviors, you can plug in your own IViewStrategy implementation.&lt;/li&gt;
&lt;/ul&gt;
   
&lt;p&gt;If you remember, we've created an empty &lt;em&gt;Views&lt;/em&gt; folder. If you create a UserControl named &lt;em&gt;ShellView&lt;/em&gt; (mind the naming), Caliburn will automagically pick it up as the View for our &lt;em&gt;ShellViewModel&lt;/em&gt;. How is this done, you may ask? According to &lt;a href=&quot;http://en.wikipedia.org/wiki/Convention_over_configuration&quot;&gt;pre-defined naming rules&lt;/a&gt;, Caliburn will look in folders named &lt;em&gt;Presenter&lt;/em&gt;, &lt;em&gt;Model&lt;/em&gt;, &lt;em&gt;ViewModel&lt;/em&gt; and &lt;em&gt;PresentationModel&lt;/em&gt; to create a ViewModel instance, and to create a View instance, it will look for it in the &lt;em&gt;Views&lt;/em&gt; folder. An example might clear it up a little bit. Supposing you have an &lt;em&gt;IShellPresenter&lt;/em&gt;, the implementation would be named &lt;em&gt;ShellPresenter&lt;/em&gt; in the folder named &lt;em&gt;Presenters&lt;/em&gt;. You should name the view for this presenter &lt;em&gt;ShellView&lt;/em&gt; and place it in &lt;em&gt;Views&lt;/em&gt; folder. The ViewModel names refers to different implementation of &lt;a href=&quot;http://martinfowler.com/eaaDev/PresentationModel.html&quot;&gt;Presentation Model&lt;/a&gt; design pattern, but those terms are used interchangeably in Caliburn.&lt;br/&gt;&lt;br/&gt;

So, we're going to use the naming convention (fourth method). Let's create an empty UserControl named &lt;em&gt;ShellView&lt;/em&gt; and put it in &lt;em&gt;Views&lt;/em&gt; folder:&lt;/p&gt;

&lt;pre class=&quot;brush:xml&quot;&gt;
&amp;lt;UserControl x:Class=&quot;PayRoll.Views.ShellView&quot;
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;&amp;gt;
    
&amp;lt;/UserControl&amp;gt;
&lt;/pre&gt;

&lt;p&gt;You can run the application now and it works. Caliburn has created a View (UI) and a POCO class as ViewModel and set the DataContext property of the View to the instance of the ViewModel. If you've noticed, we've set a UserControl as our root View. While you can create a Window object as your root View, if you create a UserControl (or anything other than a Window), Caliburn will create a MainWindow for you and place the View object as its content. This behavior is for Caliburn v1.1 so if you're using older version 1.0 you can either upgrade or use a Window as your root view.&lt;/p&gt;

&lt;span class=&quot;subtitle&quot;&gt;Binding&lt;/span&gt;
&lt;p&gt;To check if View and ViewModel are really bound together, let's create a commmand on the ViewModel and invoke it from the View. When invoked, it will change a Message property on the ViewModel. So first, open the IShellViewModel interface and add the ICommand and Message properties to the interface definition:&lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
    public interface IShellViewModel
    {
        /// &amp;lt;summary&gt;
        /// A command that says hello when invoked.
        /// &amp;lt;/summary&gt;
        ICommand SayHelloCommand { get; }

        /// &amp;lt;summary&gt;
        /// Message
        /// &amp;lt;/summary&gt;
        string Message { get; set; }
    }
&lt;/pre&gt;
	
&lt;p&gt;...and implement the command property on your ShellViewModel class. To facilitate command handling, we'll use a DelegateCommand class (originally &lt;a href=&quot;http://msdn.microsoft.com/en-us/magazine/dd419663.aspx&quot;&gt;RelayCommand&lt;/a&gt;) introduced by &lt;a href=&quot;http://joshsmithonwpf.wordpress.com/&quot;&gt;Josh Smith&lt;/a&gt;. Then instead of just displaying a MessageBox, let's Create a TextBlock and bind it to the Message property on our ViewModel. When binding some element to a property on the ViewModel, we need to implement INotifyPropertyChanged interface on the ViewModel. Fortunately, there's a base class in Caliburn which does exactly that:&lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
    public class ShellViewModel : PropertyChangedBase, IShellViewModel
    {
        private ICommand _sayHelloCommand;
        private string _message;

        public virtual ICommand SayHelloCommand
        {
            get
            {
                if (_sayHelloCommand == null)
                    _sayHelloCommand = new DelegateCommand(SayHello);

                return _sayHelloCommand;
            }
        }

        public virtual string Message
        {
            get { return _message; }
            set
            {
                _message = value;
                NotifyOfPropertyChange(&quot;Message&quot;);
            }
        }

        private void SayHello()
        {
            Message = &quot;Greetings from Caliburn!&quot;;
        }
    }
&lt;/pre&gt;

&lt;p&gt;...Finally, let's place a Button and a TextBlock on the View and bind those to our ViewModel:&lt;/p&gt;

&lt;pre class=&quot;brush:xml&quot;&gt;
&amp;lt;Window x:Class=&quot;PayRoll.Views.ShellView&quot;
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot; Width=&quot;320&quot; Height=&quot;200&quot; &amp;gt;
    &amp;lt;StackPanel&amp;gt;
        
        &amp;lt;Button Command=&quot;{Binding Path=SayHelloCommand}&quot; Content=&quot;Say Hello&quot;  /&amp;gt;
        
        &amp;lt;TextBlock Text=&quot;{Binding Path=Message}&quot; FontSize=&quot;16&quot; FontWeight=&quot;Bold&quot; /&amp;gt;
        
    &amp;lt;/StackPanel&amp;gt;
&amp;lt;/Window&amp;gt;
&lt;/pre&gt;

&lt;p&gt;In next post, I'll show you how to add &lt;a href=&quot;http://www.devexpress.com/Products/NET/Controls/WPF/&quot;&gt;DevExpress components&lt;/a&gt; to the pot and how to create a re-usable shell and along the way. We'll see how to host other ViewModels in our ShellView and how to manage them. You can get the source code of this part from &lt;a href=&quot;http://cid-4962b6ceabc2cbd7.skydrive.live.com/self.aspx/BlogFiles/PayRoll%20-%20Caliburn%20Part%20One.zip&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</description>
            <link>http://www.hightech.ir/SeeSharp/WPF-Application-With-Caliburn-Part-One</link>
            <guid isPermaLink="true">http://www.hightech.ir/SeeSharp/WPF-Application-With-Caliburn-Part-One</guid>
            <pubDate>Wed, 30 Dec 2009 00:00:00 GMT</pubDate>
            <category>MVVM</category>
            <category>WPF</category>
            <category>Caliburn</category>
        </item>
        <item>
            <dc:creator>HEskandari</dc:creator>
            <title>Entity Framework 4 - Part Three : Repository Pattern</title>
            <description>&lt;p&gt;&lt;a href=&quot;http://www.hightech.ir/SeeSharp/Entity-Framework-Four-Part-Deux-Audit-Info&quot;&gt;Last post&lt;/a&gt; showed how to use a “ObjectContext” derivation to automatically generate audit information. Now instead of using a “ObjectContext” directly, a rather different approach, a pattern called “Repository Pattern” is used to encapsulate access to the entities.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: I’m using the bits included in Visual Studio 2010 Beta 2 with additional features included in Entity Framework 4 CTP 2 (not included in VS 2010) which you can download &lt;a href=&quot;http://www.microsoft.com/downloads/details.aspx?FamilyID=13FDFCE4-7F92-438F-8058-B5B4041D0F01&amp;displaylang=en&quot;&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;span class=&quot;subtitle&quot;&gt;Specification Pattern&lt;/span&gt;&lt;br/&gt;

&lt;p&gt;What is &lt;a href=&quot;http://en.wikipedia.org/wiki/Specification_pattern&quot;&gt;“Specification Pattern”&lt;/a&gt; and what does it have to do with &lt;a href=&quot;http://martinfowler.com/eaaCatalog/repository.html&quot;&gt;“Repository Pattern”&lt;/a&gt;? In simple terms, a specification is a piece of business logic that specifies if an entity is matching a certain criteria. Let’s suppose we need to fetch list of high profile Customers. We can hard code this criteria in UI code behind which is a bad practice you probably agree. We can also place this on our entity class, the “Customer”. This may not seem so bad. Maybe it is even a good practice to build the business logics like these into the entity, right? But this will only lead to a polluted interface on our entity class as more business logic is introduced. Using “Specification Pattern” and &lt;a href=&quot;http://en.wikipedia.org/wiki/Separation_of_concerns&quot;&gt;Separation of Concerns&lt;/a&gt;, these pieces of business logic can be placed in a separate class where they belong. Later, we'll pass this criteria as a parameter into the repository to specify which entities we want to be returned.&lt;/p&gt;

&lt;p&gt;The Specification interface we're going to use would look like this:&lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
    public interface ISpecification&amp;lt;TEntity&amp;gt; where TEntity : EntityBase
    {
        Expression&amp;lt;Func&amp;lt;TEntity, bool&amp;gt;&amp;gt; IsSatisfied { get; }
    }
&lt;/pre&gt;

&lt;p&gt;...and implementation for our example above would be this:&lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
    public class HighProfileCustomersSpecification : ISpecification&amp;lt;Customer&amp;gt;
    {
        public Expression&amp;lt;Func&amp;lt;Customer, bool&amp;gt;&amp;gt; IsSatisfied
        {
            get 
            {
                return c =&amp;gt; c.Orders.Count &amp;gt; 50;
            }
        }
    }
&lt;/pre&gt;

&lt;br/&gt;
&lt;span class=&quot;subtitle&quot;&gt;Generic Repository&lt;/span&gt;&lt;br/&gt;
&lt;p&gt;First let’s specify an interface for our repository. We need generic method like FindByKey, Save, Update and methods that accept an ISpecification parameter like FindOne and FindMany. &lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
    public interface IRepository&amp;lt;TEntity&amp;gt; where TEntity : EntityBase
    {
        /// &lt;summary&gt;
        /// Saves the transient entity.
        /// &lt;/summary&gt;
        TEntity Save(TEntity entity);

        /// &lt;summary&gt;
        /// Updates the existing entity.
        /// &lt;/summary&gt;
        TEntity Update(TEntity entity);

        /// &lt;summary&gt;
        /// Finds the entity by its key.
        /// &lt;/summary&gt;
        TEntity FindByKey(long id);

        /// &lt;summary&gt;
        /// Finds entities based on provided criteria.
        /// &lt;/summary&gt;
        IList&amp;lt;TEntity&amp;gt; FindMany(ISpecification&amp;lt;TEntity&amp;gt; criteria);

        /// &lt;summary&gt;
        /// Finds one entity based on provided criteria.
        /// &lt;/summary&gt;
        TEntity FindOne(ISpecification&amp;lt;TEntity&amp;gt; criteria);
    }
&lt;/pre&gt;

&lt;p&gt;Let’s start with the easiest one, FindByKey method. To find an entity an ObjectContext that exposes the entity using IObjectSet&amp;lt;TEntity&amp;gt; is needed so in our generic repository, we’ll create those.&lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
    public abstract class Repository&amp;lt;TEntity&amp;gt; : IRepository&amp;lt;TEntity&amp;gt; where TEntity : EntityBase
    {
        private readonly ObjectContext _context;
        private readonly ObjectSet&amp;lt;TEntity&amp;gt; _entity;

        protected Repository(ObjectContext context)
        {
            _context = context;
            _entity = Context.CreateObjectSet&amp;lt;TEntity&amp;gt;();
        }

        protected ObjectContext Context
        {
            get { return _context; }
        }

        protected IObjectSet&amp;lt;TEntity&amp;gt; Entity
        {
            get { return _entity; }
        }
    }
&lt;/pre&gt;

&lt;p&gt;With this a lot of our implementation would just call the related method on Entity property.&lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
    public TEntity FindByKey(long id)
    {
        return Entity.Where(x =&amp;gt; x.Id == id)
                         .FirstOrDefault();
    }
&lt;/pre&gt;

&lt;p&gt;For Save and Update methods, the ObjectContext does provide a method that has a parameter accepting a fully qualified entity name. What’s FQEN, you may ask? Each object that is in a EntityFramework container has a fully qualified name which is in “ContainerName.EntityName” format. These are the MetaData information that is exposed through EntitySet property and usually are stored in the .edmx file which we don't have because we're using the &quot;All Code&quot; feature, remember? Fortunately, there is one place that we can get the EntitySet metadata for our POCO entity and that is through the ObjectSet instance.&lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
    private string GetEntityName()
    {
        return string.Format(&amp;quot;{0}.{1}&amp;quot;, _entity.EntitySet.EntityContainer, _entity.EntitySet.Name);
    }
&lt;/pre&gt;

&lt;p&gt;Now it is possible to write the generic Save and Update methods as follows: &lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
    public TEntity Update(TEntity entity)
    {
        var fqen = GetEntityName();

        Context.ApplyCurrentValues(fqen, entity);
        Context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);

        return entity;
    }

    public TEntity Save(TEntity entity)
    {
        var fqen = GetEntityName();

        Context.AddObject(fqen, entity);
        Context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);

        return entity;
    }
&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;Note: I’ve wraped the call to SaveChanges in Save and Update methods. You can provide additional SaveChanges method in the repository that will be called explicitly by the code that uses the repository instance.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For remaining query methods that return one or multiple instances of the Entity, we’ll use the generic CreateQuery method on the ObjectContext and feed the ISpecification’s criteria to the Where method.&lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
    public TEntity FindOne(ISpecification&amp;lt;TEntity&amp;gt; spec)
    {
        var entityName = GetEntityName();
        var query = _context.CreateQuery&amp;lt;TEntity&amp;gt;(entityName)
                                      .Where(spec.IsSatisfied());

        return query.FirstOrDefault();
    }

    public IList&amp;lt;TEntity&amp;gt; FindMany(ISpecification&amp;lt;TEntity&amp;gt; spec)
    {
        var entityName = GetEntityName();
        var query = _context.CreateQuery&amp;lt;TEntity&amp;gt;(entityName)
                                     .Where(spec.IsSatisfied());

        return query.ToList();
    }
&lt;/pre&gt;

&lt;p&gt;That sums it up. Our concrete repositories are now just a breeze to create. We can build any missing fine grained methods into our concrete repository using the Entity property. With this approach, you only need one Repository per each &lt;a href=&quot;http://dddstepbystep.com/wikis/ddd/blogged-aggregates-and-aggregate-roots.aspx&quot;&gt;Aggregate Root&lt;/a&gt; and EF4 will take care of any related transient child entities that should be persisted when aggregate root is saved.&lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
    public interface ICustomerRepository : IRepository&amp;lt;Customer&amp;gt;
    {
        IList&amp;lt;Customer&amp;gt; NewlySubscribed();
    }

    public class CustomerRepository : RepositoryBase&amp;lt;Customer&amp;gt;, ICustomerRepository
    {
        public CustomerRepository(ObjectContext context) 
            : base(context)
        {
        }

        public IList&amp;lt;Customer&amp;gt; NewlySubscribed()
        {
            var lastMonth = DateTime.Now.Date.AddMonths(-1);
            return base.Entity.Where(c =&amp;gt; c.Inserted &amp;gt;= lastMonth)
                            .ToList();
        }
    }
&lt;/pre&gt;

&lt;p&gt;You can get the whole project containing complete API from &lt;a href=&quot;http://cid-4962b6ceabc2cbd7.skydrive.live.com/self.aspx/BlogFiles/EF4RepositoryPattern.zip&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</description>
            <link>http://www.hightech.ir/SeeSharp/Entity-Framework-Four-Part-Three-Repository-Pattern</link>
            <guid isPermaLink="true">http://www.hightech.ir/SeeSharp/Entity-Framework-Four-Part-Three-Repository-Pattern</guid>
            <pubDate>Sat, 12 Dec 2009 14:27:00 GMT</pubDate>
            <category>EntityFramework</category>
        </item>
        <item>
            <dc:creator>HEskandari</dc:creator>
            <title>EntityFramework 4 – Part Deux : Audit Info</title>
            <description>&lt;p&gt;In &lt;a href=&quot;http://www.hightech.ir/SeeSharp/Entity-Framework-Four-Part-One-Persistence-Ignorance&quot;&gt;last post&lt;/a&gt; showed how easy it was to use POCO classes in EntityFramework v4, a feature called “All Code”. This new feature, facilitates creating domain models but how about other features you may already have using other ORMs like NHibernate? Let’s see how we can achieve automatic audit logging in EF v4.&lt;/p&gt;&lt;br/&gt;

&lt;p&gt;&lt;em&gt;Note: I’m using the bits included in Visual Studio 2010 Beta 2 with additional features included in Entity Framework 4 CTP 2 (not included in VS 2010) which you can download &lt;a href=&quot;http://www.microsoft.com/downloads/details.aspx?FamilyID=13FDFCE4-7F92-438F-8058-B5B4041D0F01&amp;displaylang=en&quot;&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For running Linq queries, we probably already have an existing “ObjectContext” implementation (see previous post). The “ObjectContext” class provides.  To create audit log, we need to tap into “SavingChanges” event and populate audit properties. Having an “EntityBase” class as layer super-type that contains “Inserted”, “InsertedBy”, “Updated” and “UpdatedBy” audit fields we can add this cross cutting concern, across all of our entities which is what I’ve done here, but obviously instead of using a base class, you can do this with an Interface. Let’s create an “AuditableObjectContext” which will do the auditing part when the entity is saved by either an Update or an Insert:&lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
public class AuditableObjectContext : ObjectContext
{
    public AuditableObjectContext(EntityConnection connection) : base(connection)
    {
        SubscribeEvents();
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            UnSubscribeEvents();
        }

        base.Dispose(disposing);
    }

    private void SubscribeEvents()
    {
        SavingChanges += OnSavingChangesCore;
    }

    private void UnSubscribeEvents()
    {
        SavingChanges -= OnSavingChangesCore;
    }

    private void OnSavingChangesCore(object sender, EventArgs e)
    {
        OnSavingChanges();
    }

    public virtual void OnSavingChanges()
    {
        FillAuditFields();
    }

    private void FillAuditFields()
    {
        var stateManager = ObjectStateManager;
        var inserted = stateManager.GetObjectStateEntries(EntityState.Added);
        var modified = stateManager.GetObjectStateEntries(EntityState.Modified);

        inserted.Where(x =&amp;gt; x.Entity is EntityBase).Select(x =&amp;gt; x.Entity).Cast&amp;lt;EntityBase&amp;gt;().ForEach(x =&amp;gt;
        {
            x.Inserted = DateTime.Now;
            x.InsertedBy = Thread.CurrentPrincipal.Identity.Name;
        });

        modified.Where(x =&amp;gt; x.Entity is EntityBase).Select(x =&amp;gt; x.Entity).Cast&amp;lt;EntityBase&amp;gt;().ForEach(x =&amp;gt;
        {
            x.Updated = DateTime.Now;
            x.UpdatedBy = Thread.CurrentPrincipal.Identity.Name;
        });
    }
}
&lt;/pre&gt;

&lt;br /&gt;&lt;p&gt;Our existing “SalesObjectContext” class from &lt;a href=&quot;http://www.hightech.ir/SeeSharp/Entity-Framework-Four-Part-One-Persistence-Ignorance&quot;&gt;previous post&lt;/a&gt;, now looks like this:&lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
public class SalesObjectContext : AuditableObjectContext
{
    public SalesObjectContext(EntityConnection connection)
        : base(connection)
    {
    }

    public IObjectSet&amp;lt;Category&amp;gt; Categories
    {
        get { return CreateObjectSet&amp;lt;Category&amp;gt;(); }
    }

    public IObjectSet&amp;lt;Product&amp;gt; Products
    {
        get { return CreateObjectSet&amp;lt;Product&amp;gt;(); }
    }
}
&lt;/pre&gt;

&lt;p&gt;It was pretty easy and painless to create auditing fields, don’t you think? In the next post, we'll see how to implement repository pattern using POCO mode and EF v4.&lt;/p&gt;</description>
            <link>http://www.hightech.ir/SeeSharp/Entity-Framework-Four-Part-Deux-Audit-Info</link>
            <guid isPermaLink="true">http://www.hightech.ir/SeeSharp/Entity-Framework-Four-Part-Deux-Audit-Info</guid>
            <pubDate>Tue, 08 Dec 2009 15:24:00 GMT</pubDate>
            <category>EntityFramework</category>
        </item>
    </channel>
</rss>
