在长期的Coding的实践过程中发现未必需要一个ORM框架来作为低层数据访问的工具,实际的业务逻辑所需要的对数据访问的功能往往只是ORM框架一个小的子集,甚至连Mirco级别(如PetaPoco, Dapper)的ORM框架都可以不用。当然这是只针对在一种类型数据库平台上开发而言,不涉及到系统要有跨数据库平台的特性。其实现实系统中真正需要跨数据库平台的系统并不多。
本人并不否认大型的ORM框架如EF、NHibernate对业界所作出的贡献,但在使用之前需要搞清楚:
1. 需要解决的问题是什么?
2. 框架的引入需要付出什么?
3. 有没有更简单的替代方法?
其实我关注的是第3点,在实现项目中我使用过Mirco ORM如Dapper,但逐渐也没再用了,还是直接写数据访问的代码,为了提高编码的效率,编写了一个在SQL Server中使用的存储过程,用来生成C#代码。
CREATE PROCEDURE [dbo].[GenerateEntity] @tableName VARCHAR(60), @getset BIT = 0ASBEGIN DECLARE @output AS VARCHAR(MAX); DECLARE @newline AS VARCHAR(2) = CHAR(13) + CHAR(10) DECLARE @lines AS TABLE ( line VARCHAR(255) ) DECLARE @line VARCHAR(255) SET NOCOUNT ON INSERT INTO @lines SELECT 'public ' + CASE WHEN DATA_TYPE='varchar' OR DATA_TYPE='nvarchar' THEN 'string' WHEN DATA_TYPE='tinyint' THEN 'byte' WHEN DATA_TYPE='bit' THEN 'bool' WHEN DATA_TYPE='datetime' OR DATA_TYPE='datetime2' THEN 'DateTime' ELSE DATA_TYPE END + CASE WHEN IS_NULLABLE='YES' AND NOT (DATA_TYPE='varchar' OR DATA_TYPE='nvarchar') THEN '? ' ELSE ' ' END + COLUMN_NAME + CASE WHEN @getset = 0 THEN ';' ELSE ' { get; set; }' END FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @tableName SET @output = 'public class ' + @tableName + @newline + '{ ' + @newline; DECLARE cur CURSOR FOR SELECT line FROM @lines OPEN cur FETCH cur INTO @line WHILE @@FETCH_STATUS = 0 BEGIN SET @output += ' ' + @line + @newline FETCH cur INTO @line END CLOSE cur DEALLOCATE cur --SELECT @output + '}' -- 在text output 窗口不能完整输出 SET @output += '}' + @newline DECLARE @variableName VARCHAR(255) = LOWER(LEFT(@tableName, 1)) + RIGHT(@tableName, LEN(@tableName)-1) DELETE @lines INSERT INTO @lines SELECT @variableName + '.' + COLUMN_NAME + ' = '+ CASE WHEN IS_NULLABLE='YES' THEN 'reader["'+COLUMN_NAME+'"] == DBNull.Value ? null : ' ELSE '' END +'(' + CASE WHEN DATA_TYPE='varchar' OR DATA_TYPE='nvarchar' THEN 'string' WHEN DATA_TYPE='tinyint' THEN 'byte' WHEN DATA_TYPE='bit' THEN 'bool' WHEN DATA_TYPE='datetime' OR DATA_TYPE='datetime2' THEN 'DateTime' ELSE DATA_TYPE END + CASE WHEN IS_NULLABLE='YES' AND NOT (DATA_TYPE='varchar' OR DATA_TYPE='nvarchar') THEN '?' ELSE '' END + ')reader["' + COLUMN_NAME + '"];' FROM INFORMATION_SCHEMA.[COLUMNS] WHERE TABLE_NAME = @tableName SET @output += @tableName + ' ' + @variableName + ' = new ' + @tableName + '();' + @newline; DECLARE cur CURSOR FOR SELECT line FROM @lines OPEN cur FETCH cur INTO @line WHILE @@FETCH_STATUS = 0 BEGIN SET @output += @line + @newline FETCH cur INTO @line END CLOSE cur DEALLOCATE cur PRINT @output;END
如果觉得在SQL生成代码不够爽,可以考虑使用:Razor Engine