Contoso Learning (ix) implementing warehousing and Working unit models

Source: Internet
Author: User
Tags bool implement

In the last tutorial, you used inheritance to eliminate duplicate code between Student and instructor entities. In this tutorial, you will see some ways to add, delete, change, and check using the warehousing and working unit patterns. As in the previous tutorial, you will be modifying the way code works in the pages you have created, rather than the newly created pages.

9-1 Warehousing and Working unit mode

The warehousing and working unit patterns are used to create an abstraction layer between the data access layer and the business logic layer. Implementing these patterns helps isolate changes in data storage and facilitates automated unit testing or test-driven development (TDD). In this tutorial, you will implement a storage class for each entity type. For Student entities, you need to create a warehousing interface and a warehousing class. When the storage object is instantiated in the controller. You will use it through the interface, and when the controller is running on the WEB server, the controller will accept any object references that implement the warehousing interface. Storage management of data by receiving storage objects makes it easy for you to control the test, just as you would use a collection in memory. At the end of the tutorial, you will use multiple warehouses and one work unit class for the Course and Department entities in the Course controller. The work cell class organizes multiple storage objects by creating a database context object for all warehouse shares. If you want to perform automated unit testing, you should also create and use interfaces in the same way for the student class. Anyway, to keep the tutorials simple, you won't be able to create and use these classes through the interface. The screenshot below shows a conceptual diagram between the controller and the context to compare the differences from the mode of not using the warehouse or working unit.

In this tutorial, you will not create unit tests that use the warehousing model for TDD in an MVC application to view the walkthrough:using TDD with asp.net mvc in the MSDN Web site, using Reposit in the EF team blog Ory and unit of the Work patterns with Entity Framework 4.0, and Julie Lerman blog Agile Entity framework 4 Repository series. Note: There are several ways to implement warehousing and working unit patterns. You can use or not use a warehouse class with the work cell class. You can implement a simple storage for all the entity types, or one for each type. If you implement a warehouse for each type, you can also derive it by separating the class, or by the base class of the generic, or by abstracting the base class. You can include business logic in storage, or restrict data access logic only. You can also create an abstraction layer for the database context class by using the Idbset interface in the entity instead of the Dbset class. The goals shown in this tutorial implement the abstraction layer, but one of the considerations is that it does not apply to all scenarios and environments.

9-2 Create Student Storage class

In the DAL folder, create a file named IStudentRepository.cs that replaces the current code with the following code.

Using System;    
Using System.Collections.Generic;    
Using System.Linq;    
Using System.Web;    
Using Contosouniversity.models;    
          
Namespace Contosouniversity.dal    
{public    
    interface istudentrepository:idisposable    
    {    
        IEnumerable <Student> getstudents ();    
        Student Getstudentbyid (int studentid);    
        void Insertstudent (Student Student);    
        void deletestudent (int studentid);    
        void Updatestudent (Student Student);    
        void Save ();    
    }    

The code defines a set of typical methods for adding, deleting, changing, and checking. Includes two read methods – one that returns all student entities and one that queries individual entities by ID. In the DAL folder, create a class file named StudentRepository.cs, replacing the original code with the following code, which implements the Istudentrepository interface.

Using System;    
Using System.Collections.Generic;    
Using System.Linq;    
Using System.Data;    
          
Using Contosouniversity.models;    
        Namespace Contosouniversity.dal {public class studentrepository:istudentrepository, IDisposable {    
          
        Private Schoolcontext context;    
        Public studentrepository (Schoolcontext context) {This.context = context; Public ienumerable<student> getstudents () {The return context.    
        Students.tolist (); Public Student Getstudentbyid (int id) {return context.    
        Students.find (ID); } public void Insertstudent (Student Student) {context.    
        Students.add (student); public void deletestudent (int studentid) {Student Student = ContexT.students.find (StudentID); Context.    
        Students.remove (student); } public void Updatestudent (Student Student) {context. Entry (student).    
        state = entitystate.modified; The public void Save () {context.    
        SaveChanges ();    
          
        private bool disposed = false;    
                protected virtual void Dispose (bool disposing) {if (!this.disposed) { if (disposing) {context.    
                Dispose ();    
        } this.disposed = true;    
            public void Dispose () {Dispose (true); Gc.    
        SuppressFinalize (this); }    
    }    
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.