您的位置:知识库 » 数据库

走进Linq-Linq to SQL源代码赏析之Provider的初始化

作者: 横刀天笑  来源: 博客园  发布时间: 2008-09-24 12:03  阅读: 6779 次  推荐: 0   原文链接   [收藏]  

系列文章导航:

走进Linq--Linq横空出世篇

走进Linq-辉煌的背后

走进Linq-Linq大观园

不能不说的C#特性-对象集合初始化器

不能不说的C#特性-匿名类型与隐式类型局部变量

不能不说的C#特性-扩展方法

不能不说的C#特性-匿名方法和Lambda表达式

不能不说的C#特性-迭代器(上)及一些研究过程中的副产品

不能不说的C#特性-迭代器(下),yield以及流的延迟计算

走进Linq-Linq to Objects(上)基础篇

走进Linq-Linq to Objects(下)实例篇

走进Linq-Linq to SQL感性认识篇

走进Linq-Linq to SQL How do I(1)

走进Linq-Linq to SQL How do I(2)

走进Linq-Linq to SQL How do I(3)

走进Linq-How do I(4)拾遗补零篇第一节

走进Linq-Linq to SQL源代码赏析 Table的获取过程

走进Linq-Linq to SQL源代码赏析之Provider的初始化

走进Linq-Linq to SQL源代码赏析,通过Linq to SQL看Linq


话说Linq to SQL理论上应该支持多种数据库的,而且应该支持多种数据库,到最后却落的这个局面,是为了商业考虑还是本来技术就不成熟?不得而知。不过不管怎么说Linq to SQL的体系结构确实是支持扩展的。

在System.Data.Linq.Mapping这个命名空间下微软提供了一个特性:ProviderAttribute,使用强类型的DataContext或使用Xml做映射的时候,该特性可以用来指定具体的数据库提供者。如下:

[Database(“dbo.cnblogs”)]
[Provider(
typeof(SqlProvider))]
Public CnBlogDataContext : DataContext
{

}

这就表明我们的Linq to SQL是基于Sql Server数据库了,SqlProvider是实现了IProvider接口的(该接口存在于System.Data.Linq.Provider命名空间下)。

在DataContext初始化时执行的Init方法里有这样几行代码:

if (model.ProviderType == null)
    {
        
throw Error.ProviderTypeNull();
    }
    Type providerType 
= model.ProviderType;
    
if (!typeof(IProvider).IsAssignableFrom(providerType))
    {
        
throw Error.ProviderDoesNotImplementRequiredInterface(providerType,
typeof(IProvider));
    }
    
this.provider = (IProvider) Activator.CreateInstance(providerType);
this.provider.Initialize(this.services, connection);


这里是根据modelProviderType创建一个IProvider的实例。Model就是一个MetaModel对象。前面两篇都提到了MetaModel有两个子类,AttributeMetaModelXmlMetaModel,看看你是用哪种方法做映射的,我们这里就用AttributeMetaModel做例子,在AttributeMetaModel的构造函数里有这样几行代码:

ProviderAttribute[] customAttributes = (ProviderAttribute[]) this.contextType.
GetCustomAttributes(
typeof(ProviderAttribute), true);
        
if ((customAttributes != null&& (customAttributes.Length == 1))
        {
            
this.providerType = customAttributes[0].Type;
        }
        
else
        {
            
this.providerType = typeof(SqlProvider);
        }

从DataContext类上找Provider特性,如果没有找到就默认使用SqlProvider了。创建了IProvider的实例就会调用它的Initialize方法进行初始化。Initialize方法需要两个参数IDataService和一个连接对象(DbConnection或是连接字符串)。

0
0

数据库热门文章

    数据库最新文章

      最新新闻

        热门新闻