Making good use of the Entity Framework in a Windows Forms application…

by Mikael Henriksson 27. January 2009 18:59

I have been abusing the Entity Framework lately so I thought I’d share some of what I have learned. First of all I must say there are quite a few things that need improvement. It is good that you can generate a model straight from the database and work towards your generated classes BUT! That’s about all you can do real easy… I could not find an easy enough way to customize these generated classes so I still have to use some “object datasources” if you will.

The most straight forward way to use the Entity Framework in a Windows Forms application is to create sort of a proxy class for the entity objects. Let’s say you have a generated class called Person. Create a class called PersonProxy.cs. In this “proxy” class you keep all business logic. The class should look something like:

namespace YourNamespace 
{ 
    using YourModelNamespace; 
    public class PersonProxy 
    { 
        private YourEntityContext context; 
        public PersonProxy() 
        { 
            context = new YourEntityContext(); 
        }

        public IList<Person> GetPersons() 
        { 
            var query = from p in Person 
                        select p; 
            return query.ToList(); 
        }

        public void CreatePerson(string firstName, string lastName) 
        { 
            Person p = new Person() { FirstName = firstName, LastName = lastName }; 
            context.AddObject("Person", p); 
            context.SaveChanges(); 
        } 
    } 
}

And instead of having the entity context straight in your form1.cs you can separate the business logic from the data logic a bit more than I used to do. Now in your form1.cs or whatever your form is called you instead use the proxy class. For several reasons obviously but my main point here is readability and to cut down the lines of code in the code behind of your form.

The form class would the end up in line with:

namespace WinClient 
{ 
	public partial class Form1 : Form 
	{ 
		private PersonProxy proxy; 
		public Form1() 
		{ 
			InitializeComponent(); 
		} 
		private void Form1_Load(object sender, EventArgs e) 
		{ 
			proxy = new PersonProxy(); 
			dataGridView1.DataSource = proxy.GetPersons(); 
		} 
		private void button1_Click(object sender, EventArgs e) 
		{ 
			proxy.CreatePerson(txtFirsName.Text, txtLastName.txt); 
		} 
	} 
}

You still have an open connection to your entity model, remember that the proxy class took care of this? Any validation against your model is done in realtime so hooking up some validation events of a datagridview should be a walk in the park. You also wont have to watch all those “AddObject”, EntityKey etc. That is visible to the proxy-class where we want it. The proxy class should also handle any kind of logic that is bound to the entity model. If you need to find what the next order number is or if an email is already taken that should be done in the proxy.

I hope this proves useful to anyone working with the Entity Framework. As I wrote earlier, it still feels “restricted” in ways. It is hard to create a complete domain model with the help of entity framework. I still have to take shortcuts but for me it is definitely worth it.

Tags: ,

Entity Framework | Windows Forms

blog comments powered by Disqus

About the author

Life architect specialized in programming