Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C#

Implementing Repository Pattern With Entity Framework

Rate me:
Please Sign up or sign in to vote.
4.77/5 (56 votes)
2 Nov 2012CPOL6 min read 447.7K   5.7K   215  
Implement Repository Pattern in data access layer with Entity Framework 4.0 and below version (working with ObjectContext and EntityObject )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RepositoryProjectSample.Implementations;

namespace RepositoryProjectSample
{
    class Program
    {
        static void Main(string[] args)
        {
            var unitOfWork = new UnitOfWork<ProjectCodeEntities>();
            var blogRepository = unitOfWork.GetRepository<BlogPost>();
            var catRepository = unitOfWork.GetRepository<Category>();
            using (unitOfWork)
            {
                Guid postId = Guid.NewGuid();
                Guid catId = Guid.NewGuid();
                System.Console.WriteLine("Enter Category of your blog post:");
                var cat = new Category { Id = catId, Name = System.Console.ReadLine() };
                System.Console.WriteLine("Enter Title of your blog post:");
                var post = new BlogPost { Id = postId, Title = System.Console.ReadLine(), Content = "Hello Universe!", PublishDate = new DateTime(2011, 1, 1), Category = cat };

                blogRepository.Add(post);

                int i = unitOfWork.Save();
                if (i > 0)
                    System.Console.WriteLine("Successfully Saved!!");
                else
                    System.Console.WriteLine("Failed to save!");

                System.Console.WriteLine("List of all post:");
                var resultPost = blogRepository.SelectAll();

                foreach (var item in resultPost)
                {
                    System.Console.WriteLine(item.Title);
                }

                System.Console.WriteLine("List of all category:");
                var resultCat = catRepository.SelectAll();

                foreach (var item in resultCat)
                {
                    System.Console.WriteLine(item.Name);
                }

                System.Console.WriteLine("Press any key to quit!");
                System.Console.Read();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader PracticePRO Software Systems Inc
United States United States
In my childhood, my uncle has shown me how to see the cloud in a close look and I understand that one can draw some elements of the Earth in the sky-canvas if he/she wants to. After that the cloud becomes closer to me and It teaches me one thing that, a deeper-look to something will give you some clues to draw your imagination. You can able to see that one which you have build-up in your mind.

Years past, I have started my career as a software engineer and has been looking for passion in my coding and development which I should be to enjoy my profession and has started asking myself- 'am I doing any engineering here?!' Is my code becoming that thing which I have designed in my mind? So to find that answer I have tried that old solution here... I have decided to come closer to my code and start analyzing them. And it is really working for me and at least it gives me the confidence that I can build something that I really want to. I can draw my thinking there through my code and can build-up my vision that I have designed in my mind. It also helps me to think out of the box, solve each problems by making blocks and make me careful on each steps.

• Morshed's Technical Blog site: http://morshedanwar.wordpress.com/

• Morshed's Technical articles those are published in Codeproject site: http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=2992452

• Morshed's Linkedin profile: http://www.linkedin.com/in/morshedanwar

• Morshed's Facebook Profile : http://www.facebook.com/morshed.pulok

Beside all these I like to do - photography and music. Here is my Flickr photos : http://www.flickr.com/photos/morshed_anwar/

Comments and Discussions