关于ASP.Net的导入导出(4)
Com.urp.command.FileFrameWork(4)
这周真是不顺利,股市又一次经历了80点的大洗礼,自己投资的几个股票也是个个遇难,损失不少,总觉得中国股市的管理层又一次动用了黑手,可我们这些散户又能怎么样呢。就跟工作一样,上面的leader整天在那混日子,做设计的人也是马马乎乎的,我们这些底下的又怎么能写出好代码呢。
这次主要是把ModelCollection给补充完整了。建了个包LocalModel,在形式上努力和DataSet保持一直,有些相同的基本操作。如用Column来表示列信息,Model来表示行信息。通过行来访问数据等。类图如下:
基本上是模仿着DataSet写的,ModelCollection继承了CollectionBase,但这里需要注意的是,list里保存的不是LocalModel.Model,而是需要导入到数据库中的实体类,这样子做因为从高层上来看ModelCollection就仅仅是一个Ilist,并且我们可以通过this来访问ModelCollection,直接取到里面的数据,而那些其他方法都是可以忽略不看的。ModelProxy维护Column类,相关字段,属性的创建,调用都是由它来管理。但对于外层来说,他是个代理,是不可见的。访问属性是的创建多用了惰性创建。ModelCollection通过ModelProxy可以访问属性信息,通过Model可以给属性付值和取值。
例如我们采用Northwind的Customers表来建立实体类(以后所有的例子都将采用Northwind数据库)。
public class Customer

{
private string customerID;
private string companyName;
private string contactName;
private string contactTitle;
private string address;
private string city;
private string region;
private string postalCode;
private string country;
private string phone;
private string fax;
public Customer()
{
}
public string CustomerID
{
get
{ return customerID; }
set
{ customerID = value; }
}
public string CompanyName
{
get
{ return companyName; }
set
{ companyName = value; }
}
public string ContactName
{
get
{ return contactName; }
set
{ contactName = value; }
}
public string ContactTitle
{
get
{ return contactTitle; }
set
{ contactTitle = value; }
}
public string Address
{
get
{ return address; }
set
{ address = value; }
}
public string City
{
get
{ return city; }
set
{ city = value; }
}
public string Region
{
get
{ return region; }
set
{ region = value; }
}
public string PostalCode
{
get
{ return postalCode; }
set
{ postalCode = value; }
}
public string Country
{
get
{ return country; }
set
{ country = value; }
}
public string Phone
{
get
{ return phone; }
set
{ phone = value; }
}
public string Fax
{
get
{ return fax; }
set
{ fax = value; }
}
}
实体类的信息我们采用xml来配置它。配置文件如下:
<?xml version="1.0" encoding="utf-8"?>
<models xmlns="http://tempuri.org/Model-Config.xsd">
<Customer modelname="com.urp.command.FileFrameWork.Model.Customer">
<property name="CustomerID" column="CustomerID" type="string" defaultValue=" " import="true" export="true" />
<property name="CompanyName" column="CompanyName" type="string" defaultValue=" " import="false" export="true" />
<property name="ContactName" column="ContactName" type="string" defaultValue=" " import="false" export="true" />
<property name="ContactTitle" column="ContactTitle" type="string" defaultValue=" " import="false" export="true" />
<property name="Address" column="Address" type="string" defaultValue=" " import="false" export="true" />
<property name="City" column="City" type="string" defaultValue=" " import="false" export="true" />
<property name="Region" column="Region" type="string" defaultValue=" " import="true" export="true" />
<property name="PostalCode" column="PostalCode" type="string" defaultValue=" " import="false" export="false" />
<property name="Country" column="Country" type="string" defaultValue=" " import="true" export="true" />
<property name="Phone" column="Phone" type="string" defaultValue=" " import="true" export="true" />
<property name="Fax" column="Fax" type="string" defaultValue="0" import="false" export="false" />
</Customer>
</models>
根据modelname,利用反射动态创建Customer类。Property中的name是类的属性名, column是要导入的文件里的列名,通过column和name匹配起来。根据import,export来决定是否需要导入,导出。defaultValue用来作为空字段的默认值。对于某些属性我们还能通过formatter来规范它转换格式。
这样配置其实只能对单个的实体类导入导出,所以我们以后的property可以通过修改name,比如通过name = " Contact.ContactName" 来访问联系人的名字。这样子在某种程度上就支持多表的导入导出。关于这个等以后在实现吧。现在导入的代码已经基本上可以形成了,下次整理一下就,放出完整的导入。
posted on 2006-07-15 19:09 whoisyorudady 阅读(1713) 评论(0) 编辑 收藏
