Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Understanding and Implementing Repository and Unit of Work Pattern in ASP.NET MVC Application

, 12 Apr 2013
In this article we will try to see what is Repository and Unit of Work Pattern in an ASP.NET MVC application.
RepoUowSample-noexe.zip
RepoUowSample
RepoUowSample
App_Data
SampleDatabase.mdf
SampleDatabase_log.LDF
Controllers
Global.asax
Models
SampleDbModel.edmx
Properties
RepoUowSample.csproj.user
Views
BookEn
Books
Home
RepoUowSample.suo
RepoUowSample.zip
SampleDatabase.mdf
SampleDatabase_log.LDF
bin
Content
Global.asax
SampleDbModel.edmx
obj
Debug
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
edmxResourcesToEmbed
Models
TempPE
Models.SampleDbModel.Designer.cs.dll
RepoUowSample.csproj.user
Scripts
RepoUowSample.suo
//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml.Serialization;

[assembly: EdmSchemaAttribute()]
namespace RepoUowSample.Models
{
    #region Contexts
    
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public partial class SampleDatabaseEntities : ObjectContext
    {
        #region Constructors
    
        /// <summary>
        /// Initializes a new SampleDatabaseEntities object using the connection string found in the 'SampleDatabaseEntities' section of the application configuration file.
        /// </summary>
        public SampleDatabaseEntities() : base("name=SampleDatabaseEntities", "SampleDatabaseEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        /// <summary>
        /// Initialize a new SampleDatabaseEntities object.
        /// </summary>
        public SampleDatabaseEntities(string connectionString) : base(connectionString, "SampleDatabaseEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        /// <summary>
        /// Initialize a new SampleDatabaseEntities object.
        /// </summary>
        public SampleDatabaseEntities(EntityConnection connection) : base(connection, "SampleDatabaseEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        #endregion
    
        #region Partial Methods
    
        partial void OnContextCreated();
    
        #endregion
    
        #region ObjectSet Properties
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Book> Books
        {
            get
            {
                if ((_Books == null))
                {
                    _Books = base.CreateObjectSet<Book>("Books");
                }
                return _Books;
            }
        }
        private ObjectSet<Book> _Books;

        #endregion

        #region AddTo Methods
    
        /// <summary>
        /// Deprecated Method for adding a new object to the Books EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
        /// </summary>
        public void AddToBooks(Book book)
        {
            base.AddObject("Books", book);
        }

        #endregion

    }

    #endregion

    #region Entities
    
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmEntityTypeAttribute(NamespaceName="SampleDatabaseModel", Name="Book")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    public partial class Book : EntityObject
    {
        #region Factory Method
    
        /// <summary>
        /// Create a new Book object.
        /// </summary>
        /// <param name="id">Initial value of the ID property.</param>
        /// <param name="bookName">Initial value of the BookName property.</param>
        /// <param name="authorName">Initial value of the AuthorName property.</param>
        /// <param name="iSBN">Initial value of the ISBN property.</param>
        public static Book CreateBook(global::System.Int32 id, global::System.String bookName, global::System.String authorName, global::System.String iSBN)
        {
            Book book = new Book();
            book.ID = id;
            book.BookName = bookName;
            book.AuthorName = authorName;
            book.ISBN = iSBN;
            return book;
        }

        #endregion

        #region Primitive Properties
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Int32 ID
        {
            get
            {
                return _ID;
            }
            set
            {
                if (_ID != value)
                {
                    OnIDChanging(value);
                    ReportPropertyChanging("ID");
                    _ID = StructuralObject.SetValidValue(value);
                    ReportPropertyChanged("ID");
                    OnIDChanged();
                }
            }
        }
        private global::System.Int32 _ID;
        partial void OnIDChanging(global::System.Int32 value);
        partial void OnIDChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.String BookName
        {
            get
            {
                return _BookName;
            }
            set
            {
                OnBookNameChanging(value);
                ReportPropertyChanging("BookName");
                _BookName = StructuralObject.SetValidValue(value, false);
                ReportPropertyChanged("BookName");
                OnBookNameChanged();
            }
        }
        private global::System.String _BookName;
        partial void OnBookNameChanging(global::System.String value);
        partial void OnBookNameChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.String AuthorName
        {
            get
            {
                return _AuthorName;
            }
            set
            {
                OnAuthorNameChanging(value);
                ReportPropertyChanging("AuthorName");
                _AuthorName = StructuralObject.SetValidValue(value, false);
                ReportPropertyChanged("AuthorName");
                OnAuthorNameChanged();
            }
        }
        private global::System.String _AuthorName;
        partial void OnAuthorNameChanging(global::System.String value);
        partial void OnAuthorNameChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.String ISBN
        {
            get
            {
                return _ISBN;
            }
            set
            {
                OnISBNChanging(value);
                ReportPropertyChanging("ISBN");
                _ISBN = StructuralObject.SetValidValue(value, false);
                ReportPropertyChanged("ISBN");
                OnISBNChanged();
            }
        }
        private global::System.String _ISBN;
        partial void OnISBNChanging(global::System.String value);
        partial void OnISBNChanged();

        #endregion

    
    }

    #endregion

    
}

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)

Share

About the Author

Rahul Rajat Singh
Software Developer (Senior)
India India
I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.
  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4
 
If you like my articles, please visit my website for more: www.rahulrajatsingh.com[^]
Follow on   Twitter   Google+   LinkedIn

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 12 Apr 2013
Article Copyright 2013 by Rahul Rajat Singh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid