必赢亚洲手机app下载


女厕所的第三间

短篇悬疑丨夜行动物

在MVC中行使泛型仓储情势和依赖注入实现增删查改

原稿链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pattern-and-dep/

一系列目录:

 

 

这篇随笔,我将介绍如何在MVC项目中,使用泛型仓储形式和倚重注入实现增删查改。

源代码下载:https://github.com/caofangsheng93/IocSln

这篇作品的章节如下:

1.介绍【Introduction】

2.凭借倒置原则(DIP)【Dependency Inversion
Principle】

 
a.真实的事例【Real-Life
Example】

 
b.实际不接纳看重倒置(DIP)的例证【Practical Example without DIP】

 
c.实际使用倚重倒置(DIP)的例子【Practical
Example with DIP】

3.说了算反转(IOC)情势【Inversion of Control (IoC)
Pattern】

4.依赖注入(Dependency Injection)【DI】

  a.紧耦合【Tight Coupling】

 
b.松耦合【Loose Coupling】

5.凭借注入的容器【Dependency Injection (DI)
Container】

6.顺序增删查改的宏图步骤【CRUD operations Application
Design】

   
 a.在先后中定义实体【Define Entities in
Application】

   
 b.定义数据上下文【Define Context
Class】

   
 c.定义映射实体类【Define Mapping of
Entities】

   
 d.成立泛型仓储【Create Generic
Repository】

   
 e.创造服务来对用户表举办操作【Create
Service for User Operations】

7.在MVC程序中应用IOC和DI【An MVC Application Using the IoC and
DI】

   
 a.引入Ninject依赖注入容器【Ninject
Dependency Injection Container】

   
 b.用于增删查改的Model和控制器【 CRUD
Operations Model and Controller】

   
 c.create/Edit视图【reate / Edit User
View】

   
 d.User List视图【User List
View】

   
 e.User Detail视图【User Detail
View】

   
 f.Delete User视图【Delete
User】

8.总结【Conclusion】

9.下载【Download】

 

介绍

A
software developer writes a lot of code that is tightly coupled and when
complexity grows the code will eventually deteriorate into spaghetti code, in other words
application design being a bad design. A bad design that has one of the
following causes.

deteriorate
[ 英 [dɪ’tɪərɪəreɪt]   美 [dɪ’tɪrɪəret]  vi. 恶化,变坏 vt.
恶化] 

spaghetti
【 英 [spə’getɪ]   美 [spə’ɡɛti] n. 意大利式细面条】

 

翻译:一个软件开发者写出了成千上万紧耦合的代码,可是当代码变的很复杂的时候,他写的代码就会恶化成意大利米粉(不堪一击),换句话说程序就变成了一个不佳的筹划,一个欠好的规划是由上面二种元素造成的:

1.Rigid: A design is rigid if it cannot be
easily changed. A single change is heavily dependent on another module
so this change causes a cascade of changes that couldn’t be predicted,
the impact of change could not be estimated.

2.Fragile: A simple change to one part of
the application leads to failures in another part of it that appears
completely unrelated. Fixing those problems leads to even more
problems.

3.Immobile: When the desirable parts of the
design are highly dependent upon other details that are not desired and
that is why one part of the application could not be used in another
application.

Rigid
【 英 [‘rɪdʒɪd]   美 [‘rɪdʒɪd]  adj.
严格的;僵硬的,死板的;坚硬的;精确的】

Fragile
【 英 [‘frædʒaɪl]   美 [‘frædʒəl] adj. 脆的;易碎的】

Immobile
【 英 [ɪ’məʊbaɪl]   美 [ɪ’mobl] adj.
固定的;稳定的;不变的】

 

翻译:

1.死板的:假如规划是刻板的,那么想要改变就很难了。一个微细改变就会制约很多其它的模块,所以这样的改观就会促成一体系不可能预期的更改,并且这么些改变所带动的震慑是无法预估的。

2.易碎的:程序中一个简易的变动,就会导致其他不想关的片段完全瘫痪。修复那一个题目甚至会招致更多的问题。

3.原则性的:当程序设计中的相比较好的有的零件紧紧耦合,其他大家不想提取出来的零部件的时候,那一个时候这一个企划相比好的机件就很难重用到其他的类别中了。

必赢亚洲手机app 1

The
Dependency Inversion Principle is a solution for all these causes,
that’s why in this article I explain it. This article basically focuses
on DIP, IoC, DI and DI containers so before diving into these, I just
want to provide a small introduction in this section. 

翻译:倚重注入原则就是解决地点这一个问题的一个很好的缓解方案。这也是自身怎么要在这篇小说中详尽分解的原委。这篇著作基础性了教学依赖倒置原则,控制反转容器和依赖注入,所以在上课这些往日,我打算先来一个简易的牵线。

The
Dependency Inversion Principle (DIP) is a software design principle that
is the last principle of SOLID while Inversion of Control (IoC) is a
software design pattern. Here I used two terms, one is a principle and
the other is a pattern. So what is the basic difference between
these?

翻译:
看重倒置原则是统筹规范中最终一个。然后决定反转是软件的设计形式。这里自己使用这五个术语,一个是计划性原则此外一个是设计情势。那么他们中间基本的分别是怎么着啊?

 
1. Software Design Principle: Software Design Principles represent a
set of guidelines that help us to avoid having a bad design. These are
less about specific languages or paradigms and more generally “Don’t
Repeat Yourself” (DRY);     the DRY principle is true for all
programming.

翻译:软件设计原则:软件设计原则也就是一多重扶持我们避免不佳的软件设计方针。设计规范,很少是关乎到实际的编程语言依然编程范式的,更多的是涉嫌到“不要再一次你自己”,软件设计原则对富有的语言编程都是相同有效的。

  2. Software
Design Pattern: Software Design Patterns are a general reusable solution
to a commonly occurring problem within a given context in software
design. These are common solutions for object-oriented programming
            problems. Like the Singleton Pattern and Factory
Pattern.

翻译:软件设计情势:软件设计形式:软件设计格局平日是提交,在既定的环境中普遍存在的题材的,一个足以选择的化解方案。软件设计格局是缓解在面向对象编程过程中现身的问题的一个很通用的解决方案,例如:单例形式和工厂形式。

 

借助于倒置原则

It is the
fifth principle of SOLID where “D” stands for Dependency Inversion
Principle. Its main goal is decoupling software modules, in other words
software design should be loosely coupled instead of tightly
coupled.

 

翻译:倚重倒置原则是计划性原则中的第多少个,D代表看重倒置原则。它的重点对象是去除软件设计模块之间的耦合性,换句话说软件设计,应该是松耦合的而不是紧耦合的。

The
principle states:【依赖倒置原则的特点】
     
1.High-level modules should not depend upon low-level modules. Both
should depend upon abstractions.【高层次的模块不应当依靠于低层次的模块,两者都应有借助于抽象层。】
     
2.Abstractions should not depend upon details. Details should depend
upon abstractions.【抽象层不应有依靠于具像层,具像层应该借助于抽象层。】
In short
the higher-level module defines an interface and lower-level module
implements that interface. To explain this sentence we use a real-life
example.【换句话说,高层次的模块定义了一个接口,然后低层次的模块实现了这一个接口,为了诠释这些情景我动用了一个真正的事例。】

 

实在的例证

Suppose
you are sitting on your desk. Your desk has some gadgets, like your
development machine (LCD Monitor or Laptop) and mobile phone. The LCD
Monitor has a cable that connects from the electric port (power cable)
and the same as the mobile phone that also has a charging cable that
also connects to an electric port. You could see that both devices
connect from the electric port so the question occurs of who defined the
port, your device or the cable? You will say that the devices define the
port, in other words we don’t purchase devices depending on port while
the port is designed dependent on devices and the cable is just an
interface that connects both devices and the port so you could say that
a high-level module doesn’t depend on the low-level module but both
should be dependent on abstraction.

gadgets
【 美 [gæ,dʒɪts] n. 小配件;小工具(gadget的复数)】

翻译:假想你正坐在办公桌上,你的桌上有一对组件,例如你的付出机器(液晶彰显器或者台式机电脑)和手机。液晶展现器有一个电缆连接到了插板上,并且手机有一根充电线同样连续到了插板上。你可以见到后日五个设施都连到了这些插板的端口上,那么问题出来了,何人定义了这多少个端口呢,你的设备或者这么些电缆线呢?你恐怕会说,是这一个设备定义了这多少个端口,换句话说,我们不遵照端口买这多少个装备,并且端口的计划是单独于设备的。并且电缆线仅仅是一个接连装置和端口的接口,所以您可以说一个高层次的模块不可以依靠于低层次的模块,不过高层次模块和低层次模块都应该借助于肤浅模块。

 

实际上不使用依赖倒置(DIP)的例子

To
understand DIP, we use an example that explains how an error log can
manage an application. There are two types of log management, the first
one is via text file and the other is by using the event viewer. We
create a high-level Operation class for error log entry. We create two
interfaces, one is IEventViewerLogger and the other is IFileLogger
interface. The IEventViewerLogger interface is implemented by the
EventViewerLogger class while the IFileLogger interface is implemented
by the FileLogger class.

翻译:为了领会倚重倒置,我们使用了一个事例来表明:程序中破绽百出日志怎么管理程序。这里有两类别型的日志系统,第一个是经过文件文件记录,其它一个是利用事件查看器。我们为错误日志实体创设一个高层次的操作类,然后创立三个接口,一个是I伊芙(Eve)ntViewerLogger,此外一个是IFileLogger,伊芙ntViewerLogger类实现I伊芙ntViewerLogger接口,FileLogger类实现IFileLogger接口。

The
preceding Figure 1.2 shows that a high-level Operation class depends on
the interface. We create an instance of each interface in this class and
assign an appropriate object to each instance and use the operation
accordingly. This means that a high-level module Operation class depends
on the low-level module such as interfaces. Now suppose we want to add a
new logger, such as a Database logger. Then we need to add a new
interface and that’s why the Operation class needs to care for all the
interfaces and that’s a violation of the Dependency Inversion
Principle.

violation 【英 [vaɪə’leɪʃn]   美 [,vaɪə’leʃən] n. 违反;妨碍,侵害;违背;强奸】

翻译:下图中,显示了一个高层次的操作类看重于接口ILogger,我们在这么些高层次的操作类中创设了接口了每个接口的实例【实例接口中定义的分子】,并且依照操作分配合适的靶子给各个实例,那样就表示高层次的模块操作类看重于低层次模块,例如接口。现在假如,我们想要添加一个新的Logger,例如一个Database
Logger,这多少个时候,大家就需要充足一个新接口,这也是为啥,这些操作类需要考虑到持有的接口,并且这样违反了借助倒置的尺码。

必赢亚洲手机app 2

 

 

实际上利用依赖倒置(DIP)的事例【Practical
Example with DIP】

DIP
states that a high-level module should not depend on a low-level module,
both should be dependent on abstraction. To implement this principle we
create an ILogger interface that is defined by a high-level module, in
other words by an operation class and implemented by low-level modules,
both EventViewerLogger and FileLogger classes. So when we add a new
Logger, such as a database logger, then we don’t need to update the
Operation class.

翻译:依赖倒置原则阐明:一个高档模块不应有借助于一个初级模块,而是高级模块和低级模块都应当依靠于一个抽象层。为了兑现那几个依靠倒置原则,大家创立一个高档模块定义的ILogger接口,换句话说,这多少个Ilogger接口被一个操作类定义,被低级模块(伊芙(Eve)ntViewerLogger和FileLogger类)实现。所以,当大家抬高一个新的Logger的时候,例如database
Logger,这么些时候,大家就不用去更新这一个操作类了。

必赢亚洲手机app 3

 

 

 

必赢亚洲手机app,It’s a
basic introduction to the Dependency Inversion Principle. If you are
looking for code for this example then you can visit: Constructor
Dependency Injection Pattern Implementation in
C#
.
As I mention in this article that the software design principle is a
guideline, in other words  DIP doesn’t tell us how to solve the
preceding problem. If we want to understand how to solve the preceding
problem then we need to follow a software design pattern and move onto
Inversion of Control.

翻译:这是一个主导的牵线依赖倒置原则的稿子,假使你想这一个事例的话,你可以访问:Constructor Dependency Injection Pattern
Implementation in
C#
.正如我在这篇作品说到的,软件设计原则是一个引导方针,换句话说,倚重倒置原则并不会报告我们怎么去化解眼前提到的题材。假若大家想要领悟,怎么去解决眼前遭遇的题目,我们需要去看下软件设计模式,顺便看看控制反转。

 

控制反转(IOC)形式【Inversion
of Control (IoC) Pattern】

DIP is a
software design principle that defines a guideline to solve a problem
while IoC is a software design pattern that defines how to solve the
problem. In other words the IoC is the pattern by which we can
practically implement DIP in software development. Let’s see an
example.

翻译:依赖倒置原则是软件设计的原则,它定义了一个辅导方针,援救大家去解决问题。但是IOC是软件设计格局,IOC帮忙我们怎么去化解问题。换句话说,IOC是形式,通过它我们可以在软件开发中落实依靠倒置原则。我们来看一个事例吗。

In the
previous error logger example, we define interfaces and implement them
in classes. DIP states that a High-level Module should not be depend on
a low-level module, that means we define the interface according to a
high-level module Operation Class and implemented on a low-level module
classes. The IoC is inverting the control of something switching
control. In other words an outside module or class is responsible for
creating an object of the class instead of directly creating the object
of the low-level module class in the high-level module class so we can
say that an IoC is an abstraction on which both high-level and low-level
modules depend and it inverts the control flow.

invert 【[ɪn’vɜːt]   美 [‘ɪnvɝt] vt. 使…转化;使…颠倒;使…反转;使…前后倒置 n. 颠倒的事物;倒置物;倒悬者 adj. 转化的】

翻译:在前方的荒唐日志的例子中,大家定义了接口,并在类中落实了接口,看重倒置原则表明:一个高层级的模块不应当依靠于低层级的模块,那也就是说,我们遵照高层次的操作类来定义这么些接口,然后在低层次的类中实现这些接口。IOC是控制反转的开关。换句话说一个外在的模块或者一个类是用来创设类的目的的,而不是从来在高层次的模块类中开创低层次的类的目的。所以我们得以说,IOC就是一个抽象层,不仅仅高级模块还有低级模块都看重于它,IOC使控制发生了反转。

In short
we can say that IoC is used to invert the control flow of the
application and an application module interacts with another module via
interface and application classes object are created from one
class.

翻译:简单的讲,大家可以说,IOC是用来反转程序的控制流,并且程序的模块和此外模块的竞相是透过接口,和其余类中创立的类的靶子来实现的。

必赢亚洲手机app 4

 

 

Dependency
Injection (DI)–依赖注入

Dependency
Injection (DI) is a type of IoC, it is a pattern where objects are not
responsible for creating their own dependencies. Dependency injection is
a way to remove hard-coded dependencies among objects, making it easier
to replace an object’s dependencies, either for testing (using mock
objects in unit test) or to change run-time behaviour.

翻译:依赖注入是决定反转的一种,它是一种设计格局:对象不承担创设和谐的借助。倚重注入是移除对象期间硬编码倚重的一种情势。使对象的依赖可以更易于的被交换,或者说为了更便于测试,或者改变目的运行的时候的作为。

Before
understanding Dependency Injection, you should be familiar with the two
concepts of Object Oriented Programming, one is tight coupling and
another is loose coupling, so let’s see each one by one.

了解倚重注入从前,你应当要很熟知面向对象编程中的三个概念,一个是紧耦合,另外一个是松耦合,我们来一个一个的看呢:

Tight
Coupling:
 When a class is dependent on a concrete dependency, it is
said to be tightly coupled to that class. A tightly coupled object is
dependent on another object; that means changing one object in a tightly
coupled application often requires changes to a number of other objects.
It is not difficult when an application is small but in an enterprise
level application, it is too difficult to make the changes.

翻译:紧耦合:当一个类依赖于一个具体的依赖性,也就是说这个类,是密不可分耦合的类。一个紧耦合对象看重于另外一个对象,这代表改变紧耦合程序中的一个对象,通常需要改变很多此外对象的有的代码,当应用程序相比较小的时候,还不是很难,可是一旦应用程序是商店级很大的时候,这样就变得很难修改了。

Loose
Coupling:
 It means two objects are independent and an object can use
another object without being dependent on it. It is a design goal that
seeks to reduce the inter-dependencies among components of a system with
the goal of reducing the risk that changes in one component will require
changes in any other component.

翻译:松耦合:这意味着多少个目的是并行独立的,并且一个目的使用其余一个目标而不用倚重于它。松耦合是先后设计的目的,目的在于谋求裁减系统组件之间的内在的相互倚重,并且当我们改变其中一个组件代码的时候,裁减,需要去修改其他零件代码的风险。

Now in
short, Dependency Injection is a pattern that makes objects loosely
coupled instead of tightly coupled. Generally we create a concrete class
object in the class we require the object and bind it in the dependent
class but DI is a pattern where we create a concrete class object
outside this high-level module or dependent class.

翻译:现在简短,就是依靠注入是一个设计格局,它亦可使对象之间变成松耦合,代替本来对象之间的紧耦合。平时,我们在类中开创一个实际的类的目的的时候,我们需要这么些目标,捆绑在这一个类中,不过依靠注入设计格局下,我们创设一个类的切实可行对象,在那一个高级模块或者这多少个类之外,而不用捆绑在那一个类中。

There are
three types of dependency injections:

  1. Constructor
    Dependency Injection
  2. Setter
    Dependency Injection
  3. Interface
    Dependency Injection

翻译:这里有二种档次的借助注入:

 
    1.构造函数的依靠注入

 
    2.属性倚重注入

 
    3.接口倚重注入

In this
article we will use Constructor Dependency Injection. This is the most
commonly used Dependency Injection Pattern in Object Oriented
Programming. The Constructor Dependency Injection uses a parameter to
inject dependencies so there is normally one parameterized constructor
always. So in this constructor dependency, the object has no default
constructor and you need to pass specified values at the time of
creation to initiate the object. You can say that your design is loosely
coupled with the use of constructor dependency injection.

翻译:在这篇散文中,我将会选用构造函数注入的措施,这是在面向对象编程中,使用的最多的借助注入形式。构造函数倚重注入,通过动用一个参数来注入倚重,所以一般构造函数都会有一个参数,因而在构造函数倚重中,对象没有了默认的构造函数,你需要在开首化对象的时候,传递一个尽人皆知的值。这时候,你可以说,使用构造函数依赖的话,你的筹划就是是松耦合的了。

**Dependency
Injection (DI) Container 【倚重注入容器】

**The Dependency Injection Container is a framework to create
dependencies and inject them automatically when required. It
automatically creates objects based on requests and injects them when
required. It helps us split our application into a collection of
loosely-coupled, highly-cohesive pieces and then glue them back together
in a flexible manner. By DI container, our code will become easier to
write, reuse, test and modify. In this article we will use a Niject DI
Container. 

翻译:看重注入容器是一个创设看重的框架,当需要的时候,容器就会自动的注入依赖。它会遵照请求,自动地成立对象,并且当需要借助的时候,自动注入依赖。它会协理我们,将次第拆分成松耦合,高内聚的零件,并以灵活的章程,将这多少个组件重新粘贴在一齐。通过倚重注入容器,我们的代码将会变得愈加容易写,越来越容易测试和修改。这篇小说中自我将会采纳Niject
依赖注入容器。

 

好了,到此截至,理论介绍完了,起先履行吧:

 CRUD
operations Application Design【增删查改的应用程序设计】

 

We create
four projects in a solution to implement DIP with generic repository
pattern. These are:

大家将会创建多个项目并行使泛型仓储情势,来兑现依靠倒置原则,他们是:

 

  1. Ioc.Entities
    (class library)
  2. Ioc.Data
    (class library)
  3. Ioc.Service
    (class library)
  4. Ioc.Web
    (web application)

必赢亚洲手机app 5

首先肯定引用关系:Ioc.Data添加Ioc.Entiies引用,Ioc.Service添加Ioc.Entities引用和Ioc.Data引用,最后Ioc.Web添加Ioc.瑟维斯(Service)(Service)和Ioc.Entities引用。

如此我们就搭建好了骨干的框架了。接下来就是开发大家的实体层了。

 
   在那篇作品中,我将会动用EF
Code-First格局,来开发引用程序。实体层【Ioc.Entities】中,我们将会创建六个实体,一个是BaseEntity,一个是User实体,其它一个是UserProfile实体,BaseEntity实体类中有一部分公家的特性,此外多少个实体将会持续BaseEntity实体类。下边实体层的代码:

BaseEntity实体类:

必赢亚洲手机app 6必赢亚洲手机app 7

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ioc.Entities
{
    public abstract class BaseEntity
    {

         /// <summary>
        /// Id编号
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 添加时间
        /// </summary>
        public DateTime AddedDate { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime ModifiedDate { get; set; }

        /// <summary>
        /// IP地址
        /// </summary>
        public string IP { get; set; }
    }
}

BaseEntity

必赢亚洲手机app 8

User实体和UserProfile实体之间是一定的关系,一个用户只有一个UserProfile

必赢亚洲手机app 9

User实体:

必赢亚洲手机app 10必赢亚洲手机app 11

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ioc.Entities.User
{
   public class User:BaseEntity
    {
       /// <summary>
       /// 用户名
       /// </summary>
       public string UserName { get; set; }

       /// <summary>
       /// 电子邮件
       /// </summary>
       public string Email { get; set; }

       /// <summary>
       /// 密码
       /// </summary>
       public string Password { get; set; }

       /// <summary>
       /// 导航属性--UserProfile
       /// </summary>
       public virtual UserProfile UserProfile { get; set; }

   }
}

User

UserProfile实体:

必赢亚洲手机app 12必赢亚洲手机app 13

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ioc.Entities.User
{
   public class UserProfile:BaseEntity
    {
       /// <summary>
       /// 姓
       /// </summary>
       public string FirstName { get; set; }

       /// <summary>
       /// 名
       /// </summary>
       public string LastName { get; set; }

       /// <summary>
       /// 住址
       /// </summary>
       public string Address { get; set; }

       /// <summary>
       /// 导航属性--User
       /// </summary>
       public virtual User User { get; set; }
    }
}

UserProfile

实体层的代码到此就写完了,大家看下实体层的构造:

必赢亚洲手机app 14

 

现在起初数据层的开支,因为大家运用EF,所以在Ioc.Data和Ioc.Service以及Ioc.Web项目都引入EF。

先来定义数据上下文接口:

IDbContext接口:

必赢亚洲手机app 15必赢亚洲手机app 16

using Ioc.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ioc.Data
{
    public  interface IDbContext
    {

        /// <summary>
        /// 泛型返回值类型IDbSet<TEntity>方法Set,并且TEntity要继承BaseEntity
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <returns></returns>
        IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity;

        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        int SaveChanges();
    }
}

IDbContext

IocDbContext数据上下文类:

必赢亚洲手机app 17必赢亚洲手机app 18

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Ioc.Data
{
    /// <summary>
    /// 数据上下文类,要继承DbContext类
    /// </summary>
   public class IocDbContext:DbContext,IDbContext
    {
       public IocDbContext()
           : base("name=DbConnectionstring")
       { 

       }
       /// <summary>
       /// 重写DbContext类的OnModelCreating方法
       /// </summary>
       /// <param name="modelBuilder"></param>
       protected override void OnModelCreating(DbModelBuilder modelBuilder)
       {
           var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
          .Where(type => !String.IsNullOrEmpty(type.Namespace))
          .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
           foreach (var type in typesToRegister)
           {
               dynamic configurationInstance = Activator.CreateInstance(type);
               modelBuilder.Configurations.Add(configurationInstance);
           }  
           base.OnModelCreating(modelBuilder);
       }

       /// <summary>
       /// IocDbContext实现接口IDbContext中的 Set<TEntity>() 方法
       /// </summary>
       /// <typeparam name="TEntity"></typeparam>
       /// <returns></returns>
       public new IDbSet<TEntity> Set<TEntity>() where TEntity : Entities.BaseEntity
       {
           return base.Set<TEntity>();
           //throw new NotImplementedException();//可以看出来,接口的默认实现没有实现
       }
    }
}

IocDbContext

Ioc.Data的配置文件中,需要配备数据库连接字符串:

<connectionStrings>
    <add name="DbConnectionstring" connectionString="server=.;database=IocDB;uid=sa;pwd=Password_1" providerName="System.Data.SqlClient"/>
  </connectionStrings>

那么些部署完了,之后,就是实业的映射类了。如故在Ioc.Data项目中:

UserMap实体类:

必赢亚洲手机app 19必赢亚洲手机app 20

using Ioc.Entities.User;
using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ioc.Data.Mapping.User
{
    /// <summary>
    /// 实体映射类,需要继承EntityTypeConfiguration类
    /// </summary>
    public class UserMap : EntityTypeConfiguration<Ioc.Entities.User.User>
    {
        public UserMap()
        { 
           //key  
           HasKey(t => t.Id);  
           //properties  
           Property(t => t.UserName).IsRequired();  
           Property(t => t.Email).IsRequired();  
           Property(t => t.Password).IsRequired();  
           Property(t => t.AddedDate).IsRequired();  
           Property(t => t.ModifiedDate).IsRequired();  
           Property(t => t.IP);  
           //table  
           ToTable("Users");  
        }
    }
}

UserMap

UserProfileMap实体类:

必赢亚洲手机app 21必赢亚洲手机app 22

using Ioc.Entities.User;
using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ioc.Data.Mapping.User
{
    /// <summary>
    /// UserProfileMap实体映射类 ,需要继承EntityTypeConfiguration类
    /// </summary>
   public class UserProfileMap:EntityTypeConfiguration<UserProfile>
    {
       public UserProfileMap()
       {
           //key  
           HasKey(t => t.Id);
           //properties             
           Property(t => t.FirstName).IsRequired().HasMaxLength(100).HasColumnType("nvarchar");
           Property(t => t.LastName).HasMaxLength(100).HasColumnType("nvarchar");
           Property(t => t.Address).HasColumnType("nvarchar");
           Property(t => t.AddedDate).IsRequired();
           Property(t => t.ModifiedDate).IsRequired();
           Property(t => t.IP);
           //table  
           ToTable("UserProfiles");
           //relation            
           HasRequired(t => t.User).WithRequiredDependent(u => u.UserProfile);  
       }
    }
}

UserProfileMap

 

由来实体映射类写完了。现在我们延续支付,写泛型仓储格局,关于泛型仓储情势的介绍,请看这多少个连串前边的篇章的牵线,这里就不再罗嗦了。

大家在Ioc.Data项目中,创造一个泛型仓储接口。

IRepository接口

必赢亚洲手机app 23必赢亚洲手机app 24

using Ioc.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ioc.Data
{
    /// <summary>
    /// 泛型仓储接口
    /// </summary>
    /// <typeparam name="T"></typeparam>
   public interface IRepository<T> where T:BaseEntity  //类型参数和接口名称之间没有任何符号,where关键字和类型参数之间,有一个空格。
    {
       /// <summary>
       /// 泛型方法,通过id获取实体
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
       T GetById(object id);

       /// <summary>
       /// 泛型方法--添加实体
       /// </summary>
       /// <param name="model"></param>
       void Insert(T model);

       /// <summary>
       /// 泛型方法,更新实体
       /// </summary>
       /// <param name="model"></param>
       void Update(T model);

       /// <summary>
       /// 泛型方法--删除实体
       /// </summary>
       /// <param name="model"></param>
       void Delete(T model);

       /// <summary>
       /// 只读Table
       /// </summary>
       IQueryable<T> Table { get; }
    }
}

Irepository

随后创制泛型仓储类,实现这些泛型接口。

必赢亚洲手机app 25必赢亚洲手机app 26

using Ioc.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ioc.Data
{
    /// <summary>
    /// 泛型仓储类,实现泛型仓储接口。
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Repository<T> : IRepository<T> where T : BaseEntity  //约束在最后面。
    {
        private readonly IocDbContext _context;
        private IDbSet<T> _entities;

        /// <summary>
        /// 
        /// </summary>
        public Repository(IocDbContext context)
        {
            this._context = context;
        }

        public IDbSet<T> Entities
        {
            get 
            {
                if (_entities == null)
                {
                    _entities = _context.Set<T>();
                }
                return _entities;
            }
        }

        /// <summary>
        /// 实现泛型接口中的IQueryable<T>类型的 Table属性
        /// 标记为virtual是为了可以重写它
        /// </summary>
        public virtual IQueryable<T> Table
        { 
            get
            {
                return this.Entities;
            }

        }

        public T GetById(object id)
        {
            return this.Entities.Find(id);
            //throw new NotImplementedException();
        }

        public void Insert(T model)
        {
            try
            {
                if(model==null)
                {
            throw new ArgumentNullException("model");
                }
                else
                {
                    this.Entities.Add(model);
                    this._context.SaveChanges();
                }
            }
            catch(DbEntityValidationException ex)//DbEntityValidationException从这个类中获取错误信息
            {
                var msg=string.Empty;
                foreach (var errorList in ex.EntityValidationErrors)
                {
                    foreach (var item in errorList.ValidationErrors)
                    {
                     msg+=string.Format("Property:{0} Error:{1}",item.PropertyName,item.ErrorMessage)+Environment.NewLine;
                    }
                }
                     var fail=new Exception(msg,ex);
                    throw fail;
            }

            //throw new NotImplementedException();
        }

        public void Update(T model)
        {
            try
            {
                //model为空,抛空异常
                if (model == null)
                {
                    throw new ArgumentNullException("model");
                }
                else
                {
                    //直接保存了
                    this._context.SaveChanges();
                }
            }
            catch (DbEntityValidationException ex)
            {
                var msg = string.Empty;//记录错误信息
                foreach (var errorList in ex.EntityValidationErrors)
                {
                    foreach (var item in errorList.ValidationErrors)
                    {
                        msg+=string.Format("Property:{0},Error:{1}",item.PropertyName,item.ErrorMessage)+Environment.NewLine;
                    }

                }
                //创建一个异常实例,把错误信息传递进去
                var fail = new Exception(msg, ex);
                throw fail;
            }
            //throw new NotImplementedException();
        }

        public void Delete(T model)
        {
            try
            {
                if (model == null)
                {
                    throw new ArgumentNullException("entity");
                }
                this.Entities.Remove(model);
                this._context.SaveChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;

                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
                var fail = new Exception(msg, dbEx);
                throw fail;
                // throw new NotImplementedException();
            }
        }
    }
}

Repository

好吧,至此,我们的数量访问层,也就是Ioc.Data就开发完毕了,我们来看下这一个层的结构:

必赢亚洲手机app 27

 

 为了,实现依靠倒置原则,大家创造了一个Ioc.Service层,那些层负责与用户层【UI】,和多少访问层【Ioc.Data】举行通信。因为,依赖倒置原则阐明:高级的模块不能够倚重于初级的模块,所以大家在Ioc.瑟维斯(Service)(Service)项目中开创一个IUserService(Service)接口,负责和UI层的竞相【增删查改】,下边是IUser瑟维斯(Service)接口的代码:

IUser瑟维斯(Service)(Service)接口代码:

必赢亚洲手机app 28必赢亚洲手机app 29

using Ioc.Entities.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ioc.Service
{
    public interface IUserService
    {
        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        IQueryable<User> GetUsers();

        /// <summary>
        /// 通过Id获取用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        User GetUserById(long id);

        /// <summary>
        /// 新增User
        /// </summary>
        /// <param name="model"></param>
        void InsertUser(User model);

        /// <summary>
        /// 更新User
        /// </summary>
        /// <param name="model"></param>
        void UpdateUser(User model);

        /// <summary>
        /// 删除User
        /// </summary>
        /// <param name="model"></param>
        void DeleteUser(User model);

    }
}

IUserService

接着就是创制用户操作类,继承IUser瑟维斯(Service)接口,来做增删查改了。

UserService(Service)类的代码:

必赢亚洲手机app 30必赢亚洲手机app 31

using Ioc.Data;
using Ioc.Entities.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ioc.Service
{
   public class UserService:IUserService
    {
       private IRepository<User> userRepository;
       private IRepository<UserProfile> userProfileRepository;
       /// <summary>
       /// 构造函数注入
       /// </summary>
       /// <param name="userRepository"></param>
       /// <param name="userProfileRepository"></param>
       public UserService(IRepository<User> userRepository, IRepository<UserProfile> userProfileRepository)
       {
           this.userRepository = userRepository;
           this.userProfileRepository = userProfileRepository;
       }


       /// <summary>
       /// 获取所有用户
       /// </summary>
       /// <returns></returns>
       public IQueryable<User> GetUsers()
       {
           return this.userRepository.Table;
           //throw new NotImplementedException();//接口方法的默认实现,是抛异常
       }

       /// <summary>
       /// 通过Id获取用户
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
       public User GetUserById(long id)
       {
           return userRepository.GetById(id);
           //throw new NotImplementedException();
       }

       public void InsertUser(User model)
       {
           userRepository.Insert(model);
           //throw new NotImplementedException();
       }

       public void UpdateUser(User model)
       {
           userRepository.Update(model);
           //throw new NotImplementedException();
       }

       public void DeleteUser(User model)
       {
           userRepository.Delete(model);//删除用户
           userProfileRepository.Delete(model.UserProfile);//删除用户详情
           //throw new NotImplementedException();
       }
    }
}

UserService

好了,到此截至,大家的服务层也支付完了,大家看下这些层的结构:

必赢亚洲手机app 32

 

当今就是我们的Ui层了,也就是Ioc.Web,既然著作初叶已经说了,要做倚重注入,那么现在即令重点了,大家选拔Niject注入容器。

The Ninject is a lightweight dependency
injection framework for .NET applications. It helps us split our
application into a collection of loosely-coupled, highly-cohesive pieces
and then glue them back together in a flexible manner. By using Ninject
to support our application’s architecture, our code will become easier
to write, reuse, test and modify. You can learn more about it
from: http://www.ninject.org/

翻译:Ninject是一个轻量级的倚重注入.net框架.它帮助我们,把程序拆分成松耦合,高内聚的块,然后又以灵活的办法,把它们组成在联名。通过应用Ninject大家的程序框架,将会变得很容易扩大,测试和改动,你可以上这个网站了然更多关于Ninject的知识。http://www.ninject.org/

引入Ninject。

必赢亚洲手机app 33

 

引入Ninject.MVC5之后,大家先后中就有了下边的引用:

必赢亚洲手机app 34

并且在App_Start文件夹下生成了一个类:

必赢亚洲手机app 35

 

 然后我们写注入代码。

瞩目:1.按理说,Ioc.Web不用引入Ioc.Data项目标引用,但此间配置依赖注入,就如故需要添加Ioc.Data的引用了。

2.并且Ioc.Web项目也要引入EF,配置文件中也要布局连接字符串。

自动生成的NinjectWebCommon类代码:

 

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(Ioc.Web.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(Ioc.Web.App_Start.NinjectWebCommon), "Stop")]

namespace Ioc.Web.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;
    using Ioc.Data;
    using Ioc.Service;

    public static class NinjectWebCommon 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

                RegisterServices(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            //这里面写我们的注入代码
            //Bind方法要绑定的服务, To方法指定实现的类
            kernel.Bind<IDbContext>().To<IocDbContext>().InRequestScope();
            //泛型类型配置麻烦点
            kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>)).InRequestScope();
            kernel.Bind<IUserService>().To<UserService>().InRequestScope();
        }        
    }
}

 

好了,到此结束,我们的先后,大部分都形成了,首要的逻辑。

现在大家来成立一个ViewModel类,用于操作。

UserModel类:

必赢亚洲手机app 36必赢亚洲手机app 37

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Ioc.Web.Models
{
    public class UserModel
    {
        public Int64 ID { get; set; }
        [Display(Name = "First Name")]
        public string FirstName { get; set; }
        [Display(Name = "Last Name")]
        public string LastName { get; set; }
        public string Address { get; set; }
        [Display(Name = "User Name")]
        public string UserName { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        [Display(Name = "Added Date")]
        public DateTime AddedDate { get; set; } 
    }
}

View
Code

终极就是控制器中的代码实现,和视图了,快要大功告成了。

必赢亚洲手机app 38必赢亚洲手机app 39

using Ioc.Entities.User;
using Ioc.Service;
using Ioc.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Ioc.Web.Controllers
{
    public class UserController : Controller
    {
        private IUserService userService;

        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="userService"></param>
        public UserController(IUserService userService)
        {
            this.userService = userService;
        }

        public ActionResult Index()
        {
           IEnumerable<UserModel> users = userService.GetUsers().Select(u => new UserModel
            {
                FirstName = u.UserProfile.FirstName,
                LastName = u.UserProfile.LastName,
                Email = u.Email,
                Address = u.UserProfile.Address,
                ID = u.Id
            });
           return View(users);
        }

        [HttpGet]
        public ActionResult CreateEditUser(int? id)
        {
            UserModel model = new UserModel();
            if (id.HasValue && id != 0)
            {
                User userEntity = userService.GetUserById(id.Value);
                model.FirstName = userEntity.UserProfile.FirstName;
                model.LastName = userEntity.UserProfile.LastName;
                model.Address = userEntity.UserProfile.Address;
                model.Email = userEntity.Email;
                model.UserName = userEntity.UserName;
                model.Password = userEntity.Password;
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult CreateEditUser(UserModel model)
        {
            if (model.ID == 0)
            {
                Ioc.Entities.User.User userEntity = new User
                {
                    UserName = model.UserName,
                    Email = model.Email,
                    Password = model.Password,
                    AddedDate = DateTime.UtcNow,
                    ModifiedDate = DateTime.UtcNow,
                    IP = Request.UserHostAddress,
                    UserProfile = new UserProfile
                    {
                        FirstName = model.FirstName,
                        LastName = model.LastName,
                        Address = model.Address,
                        AddedDate = DateTime.UtcNow,
                        ModifiedDate = DateTime.UtcNow,
                        IP = Request.UserHostAddress
                    }
                };
                userService.InsertUser(userEntity);
                if (userEntity.Id > 0)
                {
                    return RedirectToAction("index");
                }
            }
            else
            {
                Ioc.Entities.User.User userEntity = userService.GetUserById(model.ID);
                userEntity.UserName = model.UserName;
                userEntity.Email = model.Email;
                userEntity.Password = model.Password;
                userEntity.ModifiedDate = DateTime.UtcNow;
                userEntity.IP = Request.UserHostAddress;
                userEntity.UserProfile.FirstName = model.FirstName;
                userEntity.UserProfile.LastName = model.LastName;
                userEntity.UserProfile.Address = model.Address;
                userEntity.UserProfile.ModifiedDate = DateTime.UtcNow;
                userEntity.UserProfile.IP = Request.UserHostAddress;
                userService.UpdateUser(userEntity);
                if (userEntity.Id > 0)
                {
                    return RedirectToAction("index");
                }

            }
            return View(model);
        }

        public ActionResult DetailUser(int? id)
        {
            UserModel model = new UserModel();
            if (id.HasValue && id != 0)
            {
                Ioc.Entities.User.User userEntity = userService.GetUserById(id.Value);
                // model.ID = userEntity.ID;  
                model.FirstName = userEntity.UserProfile.FirstName;
                model.LastName = userEntity.UserProfile.LastName;
                model.Address = userEntity.UserProfile.Address;
                model.Email = userEntity.Email;
                model.AddedDate = userEntity.AddedDate;
                model.UserName = userEntity.UserName;
            }
            return View(model);
        }

        public ActionResult DeleteUser(int id)
        {
            UserModel model = new UserModel();
            if (id != 0)
            {
                Ioc.Entities.User.User userEntity = userService.GetUserById(id);
                model.FirstName = userEntity.UserProfile.FirstName;
                model.LastName = userEntity.UserProfile.LastName;
                model.Address = userEntity.UserProfile.Address;
                model.Email = userEntity.Email;
                model.AddedDate = userEntity.AddedDate;
                model.UserName = userEntity.UserName;
            }
            return View(model);
        }


        [HttpPost]
        public ActionResult DeleteUser(int id, FormCollection collection)
        {
            try
            {
                if (id != 0)
                {
                    Ioc.Entities.User.User userEntity = userService.GetUserById(id);
                    userService.DeleteUser(userEntity);
                    return RedirectToAction("Index");
                }
                return View();
            }
            catch
            {
                return View();
            }
        }  

    }
}

控制器代码

 

 创设和编辑视图:

必赢亚洲手机app 40必赢亚洲手机app 41

@model Ioc.Web.Models.UserModel
@{
    ViewBag.Title = "Create Edit User";
}
<div class="book-example panel panel-primary">
    <div class="panel-heading panel-head">Add / Edit User</div>
    <div class="panel-body">
        @using (Html.BeginForm())
        {
            <div class="form-horizontal">
                <div class="form-group">
                    @Html.LabelFor(model => model.FirstName, new { @class = "col-lg-2 control-label" })
                    <div class="col-lg-9">
                        @Html.TextBoxFor(model => model.FirstName, new { @class = "form-control" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.LastName, new { @class = "col-lg-2 control-label" })
                    <div class="col-lg-9">
                        @Html.TextBoxFor(model => model.LastName, new { @class = "form-control" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.Email, new { @class = "col-lg-2 control-label" })
                    <div class="col-lg-9">
                        @Html.TextBoxFor(model => model.Email, new { @class = "form-control" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.UserName, new { @class = "col-lg-2 control-label" })
                    <div class="col-lg-9">
                        @Html.TextBoxFor(model => model.UserName, new { @class = "form-control" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.Password, new { @class = "col-lg-2 control-label" })
                    <div class="col-lg-9">
                        @Html.PasswordFor(model => model.Password, new { @class = "form-control" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.Address, new { @class = "col-lg-2 control-label" })
                    <div class="col-lg-9">
                        @Html.TextBoxFor(model => model.Address, new { @class = "form-control" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-lg-9"></div>
                    <div class="col-lg-3">
                        @Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-default" })
                        <button class="btn btn-success" id="btnSubmit" type="submit">
                            Submit
                        </button>
                    </div>
                </div>
            </div>
        }
    </div>
</div>  

View
Code

Index视图:

必赢亚洲手机app 42必赢亚洲手机app 43

@{
    ViewBag.Title = "Index";
}

@model IEnumerable<Ioc.Web.Models.UserModel>

<div class="book-example panel panel-primary">
    <div class="panel-heading panel-head">Users Listing</div>
    <div class="panel-body">
        <a id="createEditBookModal" href="@Url.Action("CreateEditUser")" class="btn btn-success">
             User
        </a>

        <table class="table" style="margin: 4px">
            <tr>
                <th>
                    @Html.DisplayName("Name")
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Email)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Address)
                </th>
                <th>
                    Action
                </th>
                <th></th>
            </tr>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.FirstName) @Html.DisplayFor(modelItem => item.LastName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Email)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Address)
                    </td>
                    <td>
                        @Html.ActionLink("Edit", "CreateEditUser", new { id = item.ID }, new { @class = "btn btn-success" })
                        @Html.ActionLink("Details", "DetailUser", new { id = item.ID }, new { @class = "btn btn-primary" })
                        @Html.ActionLink("Delete", "DeleteUser", new { id = item.ID }, new { @class = "btn btn-danger" })
                    </td>
                </tr>
            }

        </table>
    </div>
</div>  

View
Code

Delete视图:

必赢亚洲手机app 44必赢亚洲手机app 45

@model Ioc.Web.Models.UserModel

@{
    ViewBag.Title = "Delete User";
}

<div class="book-example panel panel-primary">
    <div class="panel-heading panel-head">Delete User</div>
    <div class="panel-body">
        <h3>Are you sure you want to delete this?</h3>
        <h1>@ViewBag.ErrorMessage</h1>
        <div class="form-horizontal">
            <div class="form-group">
                @Html.LabelFor(model => model.FirstName, new { @class = "col-lg-2 control-label" })
                <div class="col-lg-9">
                    @Html.DisplayFor(model => model.FirstName, new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.LastName, new { @class = "col-lg-2 control-label" })
                <div class="col-lg-9">
                    @Html.DisplayFor(model => model.LastName, new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.UserName, new { @class = "col-lg-2 control-label" })
                <div class="col-lg-9">
                    @Html.DisplayFor(model => model.UserName, new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Email, new { @class = "col-lg-2 control-label" })
                <div class="col-lg-9">
                    @Html.DisplayFor(model => model.Address, new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Address, new { @class = "col-lg-2 control-label" })
                <div class="col-lg-9">
                    @Html.DisplayFor(model => model.Address, new { @class = "form-control" })
                </div>
            </div>

            @using (Html.BeginForm())
            {
                <div class="form-group">
                    <div class="col-lg-2"></div>
                    <div class="col-lg-9">
                        <input type="submit" value="Delete" class="btn btn-danger" />
                        @Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-success" })
                    </div>
                </div>
            }
        </div>
    </div>
</div>

View
Code

detail视图:

必赢亚洲手机app 46必赢亚洲手机app 47

@model Ioc.Web.Models.UserModel

@{
    ViewBag.Title = "User Detail";
}

<div class="book-example panel panel-primary">
    <div class="panel-heading panel-head">User Detail</div>
    <div class="panel-body">
        <div class="form-horizontal">
            <div class="form-group">
                @Html.LabelFor(model => model.FirstName, new { @class = "col-lg-2 control-label" })
                <div class="col-lg-9">
                    @Html.DisplayFor(model => model.FirstName, new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.LastName, new { @class = "col-lg-2 control-label" })
                <div class="col-lg-9">
                    @Html.DisplayFor(model => model.LastName, new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.UserName, new { @class = "col-lg-2 control-label" })
                <div class="col-lg-9">
                    @Html.DisplayFor(model => model.UserName, new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Email, new { @class = "col-lg-2 control-label" })
                <div class="col-lg-9">
                    @Html.DisplayFor(model => model.Email, new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.AddedDate, new { @class = "col-lg-2 control-label" })
                <div class="col-lg-9">
                    @Html.DisplayFor(model => model.AddedDate, new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Address, new { @class = "col-lg-2 control-label" })
                <div class="col-lg-9">
                    @Html.DisplayFor(model => model.Address, new { @class = "form-control" })
                </div>
            </div>

            @using (Html.BeginForm())
            {
                <div class="form-group">
                    <div class="col-lg-2"></div>
                    <div class="col-lg-9">
                        @Html.ActionLink("Edit", "CreateEditUser", new { id = ViewContext.RouteData.Values["id"] }, new { @class = "btn btn-primary" })
                        @Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-success" })
                    </div>
                </div>
            }
        </div>
    </div>
</div>

View
Code

效果图:

必赢亚洲手机app 48必赢亚洲手机app 49

必赢亚洲手机app 50

必赢亚洲手机app 51

必赢亚洲手机app 52

好了,到此结束,这么些类其余小说就学完了,欢迎我们提意见。

 

相关文章

No Comments, Be The First!
近期评论
    功能
    网站地图xml地图