ASP.NET MVC Tip #23 – 使用 POCO LINQ to SQL 实体

ASP.NET MVC Tip #23 – 使用 POCO LINQ to SQL 实体
ASP.NET MVC Tip #23 – Use POCO LINQ to SQL Entities

美语原文:http://weblogs.asp.net/stephenwalther/archive/2008/07/22/asp-net-tip-23-use-poco-linq-to-sql-entities.aspx
国语翻译:http://www.cnblogs.com/mike108mvp

译者注:在下水平有限,翻译中若有错误或不妥之处,欢迎大家批评指正。谢谢。

译者注:ASP.NET MVC QQ交流群 1215279 欢迎对 ASP.NET MVC 感兴趣的朋友加入

在这篇帖子中,我将演示如何创建不包含任何特殊特性的LINQ to SQL实体。我将展示如何使用外部XML文件来将LINQ to SQL实体映射到数据库对象。

In this tip, I demonstrate how you can create LINQ to SQL entities that do not contain any special attributes. I show you how you can use an external XML file to map LINQ to SQL entities to database objects.

我最近已经与一些人谈过,他们都对Visual Studio对象关系设计器生成的LINQ to SQL类包含特性这个事情很困扰。他们想利用对象关系设计器来生成实体类,但他们不想让生成的实体类被一堆的特性所装饰。

I’ve talked to several people recently who are deeply bothered by the fact that the LINQ to SQL classes generated by the Visual Studio Object Relational Designer contain attributes. They want to take advantage of the Object Relational Designer to generate their entity classes. However, they don’t like the fact that the generated entities are decorated with a bunch of attributes.

例如,如果你作用对象关系设计器来生成Movies数据库表对应的LINQ to SQL类,那么你将会得到 Listing 1中的类。这个生成的类位于Movies.Designer.cs文件中。

For example, if you use the Object Relational Designer to generate a LINQ to SQL class that corresponds to the Movies database table, then you get the class in Listing 1. This class is generated in the Movies.Designer.cs file.

Listing 1 – Movie Class (abbreviated)

这个代码不便粘贴,请看原文:http://weblogs.asp.net/stephenwalther/archive/2008/07/22/asp-net-tip-23-use-poco-linq-to-sql-entities.aspx

注意,Listing 1中的这个类包含 [Table] 和 [Column] 特性。LINQ to SQL使用这些特性将类和属性映射为数据库的表和表中字段。

Notice that the class in Listing 1 includes both [Table] and [Column] attributes. LINQ to SQL uses these attributes to map classes and properties to database tables and database table columns.

一些人被这些特性所困扰。他们不想将他们的持久化逻辑与域实体混合起来,他们想为他们的实体使用POCO 对象(Plain Old CLR Objects)。

Some people are disturbed by these attributes. They don’t want to mix their persistence logic with their domain entities. They want to use POCO objects (Plain Old CLR Objects) for their entities.

幸运的是,LINQ to SQL支持映射类到数据库对象的两种方法。取代使用AttributeMappingSource的方法是,你可以使用XmlMappingSource。当你使用XmlMappingSource时,你使用一个外部的xml文件来映射类到数据库对象中。

Fortunately, LINQ to SQL supports two methods of mapping classes to database objects. Instead of using the default AttributeMappingSource, you can use the XmlMappingSource. When you use the XmlMappingSource, you use an external XML file to map classes to database objects.

你可以手动创建xml文件,或者使用SqlMetal.exe命令行工具来创建。你可以从Visual Studio命令提示符(Command Prompt)中运行它(开始菜单 -> 所有程序 -> Visual Studio 2008 -> Visual Studio Tools -> Visual Studio 2008 Command Prompt) 。

You can create the XML file by hand or you can use the SqlMetal.exe command line tool. You run SqlMetal.exe from the Visual Studio Command Prompt (Start, All Programs, Microsoft Visual Studio 2008, Visual Studio Tools, Visual Studio 2008 Command Prompt).

这里演示了如何使用SqlMetal.exe从SQL Express数据库MoviesDB.mdf,创建一个xml映射文件。

Here’s how you use SqlMetal.exe to create an XML mapping file from a RANU SQL Express database named MoviesDB.mdf:

1、导航到包含MoviesDB.mdf数据库文件的目录。
1. Navigate to the folder containing the MoviesDB.mdf database

2、执行下面的命令:
2. Execute the following command:

SqlMetal /dbml:movies.dbml MoviesDB.mdf

3、执行下面的命令:
3. Execute the following command:

SqlMetal /code:movies.cs /map:movies.map movies.dbml

执行完这些命令后,你将得到3个文件:
After you execute these commands, you will end up with three files:

· movies.dbml – movies数据库标记文件
· movies.dbml – The movies database markup file

· movies.cs – 对应于数据库对象的movie类
· movies.cs – The movie classes that correspond to the database objects

· movies.map –将类与数据库对象进行映射的XML文件
· movies.map – The XML map file that maps the classes to the database objects

当你生成这些文件之后,你可以添加movies.cs 和 movies.map文件到你的ASP.NET MVC application 的Models 目录中。

After you generate these files, you can add the movies.cs and movies.map file to your ASP.NET MVC application’s Models folder.

Listing 2包含了movies.cs中的C# Movie类文件。Listing 2中的这个文件几乎和Listing 1中的文件一模一样,除了它没有包含特殊的特性外。Listing 2 中的Movie 类是一个POCO 对象。

The C# Movie class file from the movies.cs is contained in Listing 2. The file in Listing 2 is almost exactly the same as the file in Listing 1 except for the fact that the file does not contain any special attributes. The Movie class in Listing 2 is a POCO object.

Listing 2 – Movie Class (abbreviated)

这个代码不便粘贴,请看原文:http://weblogs.asp.net/stephenwalther/archive/2008/07/22/asp-net-tip-23-use-poco-linq-to-sql-entities.aspx

Listing 3中的文件包含SqlMetal.exe 工具生成的XML 映射文件。你可以手动创建它。

The file in Listing 3 contains the XML mapping file generated by the SqlMetal.exe tool. You could create this file by hand.

Listing 3 – movies.map

这个代码不便粘贴,请看原文:http://weblogs.asp.net/stephenwalther/archive/2008/07/22/asp-net-tip-23-use-poco-linq-to-sql-entities.aspx

当你创建完一个外部的XML映射文件之后,在你初始化DataContext时,你必须将这个映射文件传递给DataContext对象。例如,Listing 4中的controller 在Index()方法中使用了movies.map文件。

After you create an external XML mapping file, you must pass the mapping file to a DataContext object when you initialize the DataContext. For example, the controller in Listing 4 uses the movies.map file within its Index() method.

Listing 4 – HomeController.cs

这个代码不便粘贴,请看原文:http://weblogs.asp.net/stephenwalther/archive/2008/07/22/asp-net-tip-23-use-poco-linq-to-sql-entities.aspx

Index()方法先从web.config中获得connection string,接着,从Models 目录中加载XML 映射文件。当DataContext 被创建时,connection string和映射源被传递给DataContext 构造函数。最后,从数据库中取出一批movies记录发送给View。

The Index() starts by retrieving a connection string from the Web configuration file. Next, the XML mapping file is loaded from the Models folder. The connection string and mapping source are passed to the DataContext constructor when the DataContext is created. Finally, a list of movies is retrieved from the database and sent to the view.

这篇帖子的重点是演示使用一个外部XML文件而不是使用带特性的LINQ to SQL 类来进行类与数据库对象的映射。一些人不想用数据库持久化逻辑来弄脏他们的类。LINQ to SQL足够灵活,能让这些人很happy。

The point of this tip was to demonstrate that you can use an external XML file instead of attributes within your LINQ to SQL classes to map your classes to your database objects. Some people don’t want to dirty their classes with database persistence logic. LINQ to SQL is flexible enough to make these people happy.

 

你可能感兴趣的:(asp.net)