MVC series (repository mode, LINQ, EF, IOC framework Castle, jquery, Ajax) (2) models (ORM)

Source: Internet
Author: User

 Learning MVC series (repository mode, LINQ, EF, IOC framework Castle, jquery, Ajax) (1) Database Modeling
MVC series (repository mode, LINQ, EF, IOC framework Castle, jquery, Ajax) (3) models (ORM, repository mode, paging)

We will focus on instances. Please search for the specific MVC theory in the blog, which is everywhere.
After the database is designed, open vs2008 and select Create Project "ASP. net MVC web apllication ", the system will automatically generate an MVC Framework, we start our project on the basis of this MVC framework. After the creation, we will have to admire Microsoft's intimacy. She has automatically generated an MVC Framework for us.ProgramSuch:

Let's see if "M", "V", and "C" are available, and jquery and Ajax are also introduced, which is really a lot of worry-free.
After running, the interface is as follows:

Okay. Now let's start writing our own stuff.
First, we can see that "mondels" is under the "scripts" folder and it looks uncomfortable. It should be tied to "M" and "V", so we move it to the root directory, for example:

Because we need to use the repository mode, we hope you will have some knowledge about it. The following are some comments from netizens:
"

In enterprise architecture model, the translator translates repository into a resource library. The following description is provided:
Coordinates between the domain and the data ing layer by accessing an interface similar to a set of domain objects.

In "Domain-driven design: The Way to cope with software core complexity", the translator translates repository into a warehouse and provides the following description:
A mechanism used to encapsulate storage, read, and search behaviors. It simulates an object set.

The biggest benefit of using this model is to extract the domain model from the customerCodeIt is decoupled from the data ing layer.

"From http://www.cnblogs.com/carysun/archive/2009/03/20/repository.html
Please refer to Baidu for details.

Now, create the required folder. The "models" folder provides interfaces for all object classes, such as "users" and "accounts, and all the operations (add, delete, modify, and query) on these object classes, so we first create three interfaces in the "models" folder, they are "iuser" "iaccount" "iaccounttype" as the interfaces for user, account, and account types. The Code is as follows:

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Namespace Pbaccount. Models
{
Public   Interface Iaccount
{
Guid accountid { Get ; Set ;}
Iuser user { Get ; Set ;}
Bool Inorout { Get ; Set ;}
Float Amount { Get ; Set ;}
Iaccounttype accounttype { Get ; Set ;}
Datetime time { Get ; Set ;}
Accountstatus status { Get ; Set ;}
String Detail { Get ; Set ;}
}

///   <Summary>
/// Account visibility
///   </Summary>
Public   Enum Accountstatus
{
///   <Summary>
/// Private
///   </Summary>
Personal = 0 ,
///   <Summary>
/// Public
///   </Summary>
Open
}

}

UsingSystem;
UsingSystem. Collections. Generic;
UsingSystem. LINQ;
UsingSystem. text;

namespace pbaccount. models
{< br> Public interface iaccounttype
{< br> guid accounttypeid { Get ; set ;}< br> string accounttypename { Get ; set ;}< BR >}

UsingSystem;
UsingSystem. Collections. Generic;
UsingSystem. LINQ;
UsingSystem. text;

Namespace Pbaccount. Models
{
Public Interface Iuser
{
Guid userid { Get ; Set ;}
String Loginname { Get ; Set ;}
String Name { Get ; Set ;}
Bool Sex { Get ; Set ;}
Datetime birthday { Get ; Set ;}
String Sion { Get ; Set ;}
Int Role { Get ; Set ;}

}
}

The above are the three interfaces. The specific content is clear. OK. Now, you still need to create two folders under the models folder, which are "user" and "Account" respectively. These two folders mainly implement specific functions. In models, Dongdong does not directly deal with databases. We use the repository mode to add, query, and modify operations that directly deal with databases. Models directly uses the basic functions provided by repository. Let's leave models here, and set the repository mode below. First, create a repository folder (at the same level as models), for example:

To deal with the database, we think of EF this Orm, for EF, please refer to: http://www.cnblogs.com/xray2005/archive/2009/05/07/1452033.html,
Open "server resource manager" and establish a connection with the database we created, such:

Then, add "ADO. NET Entity Data Model" under the "repository" folder and name it "accountedm ",
Select "generate from database" and select all the three tables we have created. The system automatically generates an EF, for example:

This article introduces the repository mode and the application of EF in ASP. net mvc.
Thank you for your criticism.

Related Article

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.