comparison Source/Data/DataProvider/Sql2008DataProvider.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections.Generic;
3 using System.Data;
4 using System.Linq;
5
6 namespace BLToolkit.Data.DataProvider
7 {
8 using Sql.SqlProvider;
9
10 public sealed class Sql2008DataProvider : SqlDataProviderBase
11 {
12 static readonly List<Func<Type,string>> _udtTypeNameResolvers = new List<Func<Type,string>>();
13
14 static Sql2008DataProvider()
15 {
16 AddUdtTypeNameResolver(ResolveStandartUdt);
17 }
18
19 public static void AddUdtTypeNameResolver(Func<Type, string> resolver)
20 {
21 _udtTypeNameResolvers.Add(resolver);
22 }
23
24 static string ResolveStandartUdt(Type type)
25 {
26 return type.Namespace == "Microsoft.SqlServer.Types" ? type.Name.Replace("Sql", "") : null;
27 }
28
29 public override string Name
30 {
31 get { return DataProvider.ProviderName.MsSql2008; }
32 }
33
34 public override ISqlProvider CreateSqlProvider()
35 {
36 return new MsSql2008SqlProvider();
37 }
38
39 public override void SetParameterValue(IDbDataParameter parameter, object value)
40 {
41 base.SetParameterValue(parameter, value);
42 SetUdtTypeName(parameter, value);
43 }
44
45 static void SetUdtTypeName(IDbDataParameter parameter, object value)
46 {
47 var sqlParameter = parameter as System.Data.SqlClient.SqlParameter;
48 var valueType = value.GetType();
49
50 if (sqlParameter != null)
51 sqlParameter.UdtTypeName = _udtTypeNameResolvers.Select(_=>_(valueType)).FirstOrDefault(_=>!string.IsNullOrEmpty(_));
52 }
53 }
54 }