手把手教你写ORM(六)

最近越来越不知道该吃什么了。唉。

现在到了比较激动的地方了,ORM,说白了最主要的工作就是将对象和数据库表格的映射建立起来。
这里我们就不用XML文件来配置了,第一会使配置文件结构变复杂加大解析难度,第二我来看看另外一种做映射的方法。

这里介绍一下.NET的Attribute,在早些时候被翻译作属性,和property混淆了。现在一般都翻译为特性或特征,它可以给一个类或者类的成员附加一些额外的特征或者功能。在.NET里面的System.Arribute作为其基类,所有继承自他的,并且类名以Attribute结尾的类型都可以作为Attribute实用,这里有一个约束,定义的时侯类名要以Attribute结尾,但是使用的时候要去掉Attibute。.NET类库预定义了很多的特性来实现很多的功能,这里我们通过Attribute类来标示类成员的特性,并且通过反射来获取来标示类成员与数据库的映射。

首先我们通过一个例子来看看Attribute的特性


执行结果自然是一个异常,异常的message是error:can not run this method!

ok,现在是否明白了它的工作原理?还有点晕?晕不要紧,做了再说。

class TestAttribute:System.Attribute
{
    public TestAttribute(string message)
    {
        throw new Exception("error:"+message);
    }
}

class Tester
{
    [Test("Can not run this method!")]
    public void Cannotrun()
    {
    }

    public static Main(sting[] args)
    {
        Tester t=new Tester();
        t.Cannotrun();
    }
}

我们现在就是来构造一个Attribute的类来存储一个属性的类型,长度,映射字段等数据

 

None.gif namespace  Alexander.Xbase.Interface
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [AttributeUsage(AttributeTargets.All, AllowMultiple 
= true)]
InBlock.gif    
public class ParamAttribute:Attribute
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string _parameterType;
InBlock.gif
InBlock.gif        
public string ParameterType
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _parameterType; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _parameterType = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private int _parameterLength;
InBlock.gif
InBlock.gif        
public int ParameterLength
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _parameterLength; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _parameterLength = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string _srccolumn;
InBlock.gif
InBlock.gif        
public string Srccolumn
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _srccolumn; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _srccolumn = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public ParamAttribute(string ptype, int len)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _parameterType 
= ptype;
InBlock.gif            _parameterLength 
= len;
InBlock.gif            
//throw new Exception("can not use");
ExpandedSubBlockEnd.gif
        }

InBlock.gif        
public ParamAttribute(string ptype, int len,string src)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _parameterType 
= ptype;
InBlock.gif            _parameterLength 
= len;
InBlock.gif            _srccolumn 
= src;
InBlock.gif            
//throw new Exception("can not use");
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

使用的时候
定义一个实体类:
None.gif class  tb
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private string _aaa;
InBlock.gif
InBlock.gif        [Param(
"NChar",10)]
InBlock.gif        
public string aaa
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _aaa; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _aaa = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string _bbb;
InBlock.gif
InBlock.gif        [Param(
"NChar"10)]
InBlock.gif        
public string bbb
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _bbb; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _bbb = value; }
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

这样子就把映射的类型,长度都存储到特征里面。

To be continue  太累了,今晚休息了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值