关于ASP.Net的导入导出(4)

本文详细介绍了ModelCollection的设计与实现过程,重点讲述了如何通过模仿DataSet来构建LocalModel,包括使用Column表示列信息、Model表示行信息等内容。同时,还探讨了如何通过反射动态创建实体类并进行导入导出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Com.urp.command.FileFrameWork4

这周真是不顺利,股市又一次经历了80点的大洗礼,自己投资的几个股票也是个个遇难,损失不少,总觉得中国股市的管理层又一次动用了黑手,可我们这些散户又能怎么样呢。就跟工作一样,上面的leader整天在那混日子,做设计的人也是马马乎乎的,我们这些底下的又怎么能写出好代码呢。

这次主要是把ModelCollection给补充完整了。建了个包LocalModel在形式上努力和DataSet保持一直,有些相同的基本操作。如用Column来表示列信息,Model来表示行信息。通过行来访问数据等。类图如下:


基本上是模仿着DataSet写的,ModelCollection继承了CollectionBase,但这里需要注意的是,list里保存的不是LocalModel.Model,而是需要导入到数据库中的实体类,这样子做因为从高层上来看ModelCollection就仅仅是一个Ilist,并且我们可以通过this来访问ModelCollection,直接取到里面的数据,而那些其他方法都是可以忽略不看的。ModelProxy维护Column类,相关字段,属性的创建,调用都是由它来管理。但对于外层来说,他是个代理,是不可见的。访问属性是的创建多用了惰性创建。ModelCollection通过ModelProxy可以访问属性信息,通过Model可以给属性付值和取值。

例如我们采用NorthwindCustomers表来建立实体类(以后所有的例子都将采用Northwind数据库)。

ContractedBlock.gifExpandedBlockStart.gifCustomer
None.gifpublic class Customer
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
private string customerID;
InBlock.gif    
private string companyName;
InBlock.gif    
private string contactName;
InBlock.gif    
private string contactTitle;
InBlock.gif    
private string address;
InBlock.gif    
private string city;
InBlock.gif    
private string region;
InBlock.gif    
private string postalCode;
InBlock.gif    
private string country;
InBlock.gif    
private string phone;
InBlock.gif    
private string fax;
InBlock.gif
InBlock.gif    
public Customer()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public string CustomerID
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gifreturn customerID; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ customerID = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string CompanyName
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gifreturn companyName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ companyName = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string ContactName
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gifreturn contactName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ contactName = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string ContactTitle
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gifreturn contactTitle; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ contactTitle = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string Address
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gifreturn address; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ address = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string City
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gifreturn city; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ city = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string Region
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gifreturn region; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ region = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string PostalCode
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gifreturn postalCode; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ postalCode = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string Country
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gifreturn country; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ country = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string Phone
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gifreturn phone; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ phone = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string Fax
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gifreturn fax; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ fax = value; }
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

实体类的信息我们采用xml来配置它。配置文件如下:

ContractedBlock.gifExpandedBlockStart.gif
None.gif<?xml version="1.0" encoding="utf-8"?>
None.gif
<models xmlns="http://tempuri.org/Model-Config.xsd">   
None.gif   
<Customer modelname="com.urp.command.FileFrameWork.Model.Customer">
None.gif        
<property name="CustomerID" column="CustomerID" type="string" defaultValue=" " import="true" export="true" />
None.gif        
<property name="CompanyName" column="CompanyName" type="string" defaultValue=" " import="false" export="true" />
None.gif        
<property name="ContactName" column="ContactName" type="string" defaultValue=" " import="false" export="true" />
None.gif        
<property name="ContactTitle" column="ContactTitle" type="string" defaultValue=" " import="false" export="true" />
None.gif        
<property name="Address" column="Address" type="string" defaultValue=" " import="false" export="true" />
None.gif        
<property name="City" column="City" type="string" defaultValue=" " import="false" export="true" />
None.gif        
<property name="Region" column="Region" type="string" defaultValue=" " import="true" export="true" />
None.gif        
<property name="PostalCode" column="PostalCode" type="string" defaultValue=" " import="false" export="false" />
None.gif        
<property name="Country" column="Country" type="string" defaultValue=" " import="true" export="true" />
None.gif        
<property name="Phone" column="Phone" type="string" defaultValue=" " import="true" export="true" />
None.gif        
<property name="Fax" column="Fax" type="string" defaultValue="0" import="false" export="false" />
None.gif   
</Customer>
None.gif
</models>
None.gif

根据modelname,利用反射动态创建Customer类。Property中的name是类的属性名, column是要导入的文件里的列名,通过columnname匹配起来。根据import,export来决定是否需要导入,导出。defaultValue用来作为空字段的默认值。对于某些属性我们还能通过formatter来规范它转换格式。

这样配置其实只能对单个的实体类导入导出,所以我们以后的property可以通过修改name,比如通过name = " Contact.ContactName" 来访问联系人的名字。这样子在某种程度上就支持多表的导入导出。关于这个等以后在实现吧。现在导入的代码已经基本上可以形成了,下次整理一下就,放出完整的导入。

转载于:https://www.cnblogs.com/freewiller/archive/2006/07/15/451545.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值