一直对MS 的ADO.NET DataModel使用不是很习惯,也一直改不过来,也有点疑惑
1.新建DataModel以后为相应的DomainService为什么不自动生成
2.Data Model中随着表越来越多,会影响性能,新增和刷新一张表,甚至会等待几分钟不等……
3. 原先项目中DomainService 还得手动更新,最QF的是新增DomainService,VS还不让找到 Data Model甚至会出现关闭重新打开后才能找到DataModel
最近一直找对应的.net 方面ORM 可以解决我所提到几个问题,能替代ADO.NET Data Model,在逛InfoQ时候,有一种LightSpeed
是针对.net商业化ORM(http://www.infoq.com/cn/news/2012/02/lightspeed),
官网:http://www.mindscapehq.com/
LightSpeed是一种针对.NET的商业化ORM,它拥有多种特性,像实体序列化、健壮的VS设计器、内建的LINQ支持、对DTO的支持等等。
看看官网对LightSpeed说明,基本可以替代ADO.NET Data Model,LightSpeed目前版本不支持.net 4.0 只能在3.5环境下运行。
数据库支持方面:
绝大多数库支持很完整的,不像ADO.NET DATA MODEL就支持自己产品
来个例子,多也不说了,去看官网,使用和ADO.NET DATA MODEL差不多。
例子环境:VS2010 .NET3.5 SQLSERVER2008 LightSpeedExpress
1.先建立库表Test 只包含少许字段
1 USE [db_Eteaching]
2 GO
3
4 /****** Object: Table [dbo].[Test] Script Date: 03/02/2012 15:37:09 ******/
5 SET ANSI_NULLS ON
6 GO
7
8 SET QUOTED_IDENTIFIER ON
9 GO
10
11 CREATE TABLE [dbo].[Test](
12 [ID] [int] IDENTITY(1,1) NOT NULL,
13 [UserName] [nvarchar](50) NULL,
14 [Password] [nvarchar](50) NULL,
15 [RealName] [nvarchar](50) NULL,
16 CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED
17 (
18 [ID] ASC
19 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
20 ) ON [PRIMARY]
21
22 GO
2.用VS连接SQL 2008,可以直接拖动表到LightSpeed中,方便操作
3.新建LightSpeedModel,命名LSModel,将表拖入模型中,LightSpeed会自动绘制实体模型,自动生成实体对象映射,
并生成LightSpeedContexts,如果是多表LightSpeed会自动显示表之间关系
以下是LightSpeed自动生成Test 代码:
1 using System;
2
3 using Mindscape.LightSpeed;
4 using Mindscape.LightSpeed.Validation;
5 using Mindscape.LightSpeed.Linq;
6
7 namespace LightSpeedDemo
8 {
9 [Serializable]
10 [System.CodeDom.Compiler.GeneratedCode("LightSpeedModelGenerator", "1.0.0.0")]
11 [System.ComponentModel.DataObject]
12 public partial class Test : Entity<int>
13 {
14 #region Fields
15
16 [ValidateLength(0, 50)]
17 private string _userName;
18 [ValidateLength(0, 50)]
19 private string _password;
20 [ValidateLength(0, 50)]
21 private string _realName;
22
23 #endregion
24
25 #region Field attribute and view names
26
27 /// <summary>Identifies the UserName entity attribute.</summary>
28 public const string UserNameField = "UserName";
29 /// <summary>Identifies the Password entity attribute.</summary>
30 public const string PasswordField = "Password";
31 /// <summary>Identifies the RealName entity attribute.</summary>
32 public const string RealNameField = "RealName";
33
34
35 #endregion
36
37 #region Properties
38
39
40
41 [System.Diagnostics.DebuggerNonUserCode]
42 public string UserName
43 {
44 get { return Get(ref _userName, "UserName"); }
45 set { Set(ref _userName, value, "UserName"); }
46 }
47
48 [System.Diagnostics.DebuggerNonUserCode]
49 public string Password
50 {
51 get { return Get(ref _password, "Password"); }
52 set { Set(ref _password, value, "Password"); }
53 }
54
55 [System.Diagnostics.DebuggerNonUserCode]
56 public string RealName
57 {
58 get { return Get(ref _realName, "RealName"); }
59 set { Set(ref _realName, value, "RealName"); }
60 }
61
62 #endregion
63 }
64
65
66
67 /// <summary>
68 /// Provides a strong-typed unit of work for working with the LSModel model.
69 /// </summary>
70 [System.CodeDom.Compiler.GeneratedCode("LightSpeedModelGenerator", "1.0.0.0")]
71 public partial class LSModelUnitOfWork : Mindscape.LightSpeed.UnitOfWork
72 {
73
74 public System.Linq.IQueryable<Test> Tests
75 {
76 get { return this.Query<Test>(); }
77 }
78
79 }
80
81 }
LightSpeed将模型代码和查操作方法都放在一起。
4.界面显示采用Winform显示,前台代码实现