好久没有写文章了,以前开了太多的系列,一方面是因为本身对于工做中的思考,另外一方面是本身在业务时间中的对知识的总结,这里也是比较抱歉,由于以前开的系列,一直都是html
开,完整写完的很少,这里实在是对不住你们了,我也是想等过年期间好好的整理下,这些系列和思路,将要写的内容,都梳理清楚,而后在年后,将一一发布,完善以下的几个系列:数据库
一、Step by Step-构建本身的ORM系列-索引设计模式
二、设计模式-系列索引session
四、云计算-从基础到应用架构系列索引测试
固然,我期间可能会还有其余的相关文章会开启,可是我不会影响这些文章的进步,否则就太对不起大伙了,也但愿你们多多提出本身的想法和意见,有了大家的帮助,我才能写的this
更完善。谢谢!云计算
最近在项目中也是遇到以下的问题,也是查看了一些相应的资料,可是都没有很好的解决,问题以下:spa
一、如何只经过XML文件来完成映射,固然这里的XML负责ORM中的数据库表列的相应信息。hibernate
二、不想为数据库中的每一个表都创建实体对象。
三、可以仍然在界面使用的过程当中方便的操做。
四、还要考虑一些适应性和低耦合性方面的要求。
有了上面的几个要求,通过思考,得出一下的几个思路,可是都不是理想的解决方案,还请你们看后如下的几个解决方案,给出意见。
上面也是给出了几个问题,我对这几个问题进行了整理和思考,得出下面的几个解决方案,可是都不是特别的理想,稍微的深刻提出需求,就没法知足。
1、经过NHibernate
固然,Nhibernate自己已经提供了这方面的操做,我特别的举例说明以下,若是想详细的查看,请参考园子里面的高手博客《李永京》的。
我这里将代码贴出:
一、咱们先看映射文件
<class entity-name="Contract"> <id name="Id" type="int"> <generator class="Test"/> </id> <property name="Name" not-null="true" length="25" type="string"/> <property name="Pay" not-null="true" type="decimal"/><property name="CreateTime" not-null="true" type="datetime"/><property name="Description" not-null="true" length="200" type="string"/> </class>
二、其余的配置,我就不详细说了,关于Nhibernate的配置。下面给出相应的示例代码:
using (ISession session = new SessionFactory().OpenSession())
{
using (ITransaction trans = session.BeginTransaction())
{
IDictionary contract = new Hashtable();
contract["Name"] = "合同名称";
contract["Pay"] = 0.0M;//合同的金额
contract["Description"] = "合同的描述信息";//第一个参数为映射中使用的实体名称,第二个参数为实例
session.Save("Contract", contract);
trans.Commit();
}
}上面给出的保存的代码,更新的代码雷同。给出查询代码:
using (ISession session = new SessionFactory().OpenSession())
{
using (ITransaction trans = session.BeginTransaction())
{
IDictionary contract =(IDictionary) session.CreateQuery(“ from Contract where ContractID=:id”).Add(“id”,1);
.UniqueResult();
session.Clear();
trans.Commit();
}
}
经过上面的代码,通过测试,的确可以将数据,经过Nhibernate提供的相应方法,完成访问。其余的相应的操做,我就不给出了,可是经过上面咱们知道,咱们没有经过建立相应的实体,我
们就能完成映射的操做,的确不错,不过,这样的实现,的确有些不便。
这个自定义对象应该如何来作呢?自定义对象的思路以下:
下面来配合这个图给出示例代码,不必定能够运行,具体的公共对象类代码以下:
public class CommonObject : IList
{
private IDictionary<string, Column> columns = null;
public CommonObject()
{
columns = new Dictionary<string, Column>();
}public CommonObject(int capacity)
{
columns = new Dictionary<string, Column>(capacity);
}public Column Add(Column col)
{
this.columns.Add(col.Name,col);return col;
}public bool Remove(Column col)
{
return this.columns.Remove(col.Name);
}public Column this[string key]
{
get
{
return this.columns[key];
}
set
{
this.columns[key] = value;
}
}
public int Add(object value)
{
throw new NotImplementedException();
}public void Clear()
{
throw new NotImplementedException();
}public bool Contains(object value)
{
throw new NotImplementedException();
}public int IndexOf(object value)
{
throw new NotImplementedException();
}public void Insert(int index, object value)
{
throw new NotImplementedException();
}public bool IsFixedSize
{
get
{
throw new NotImplementedException();
}
}public bool IsReadOnly
{
get
{
throw new NotImplementedException();
}
}public void Remove(object value)
{
throw new NotImplementedException();
}public void RemoveAt(int index)
{
throw new NotImplementedException();
}public object this[int index]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}public void CopyTo(Array array, int index)
{
throw new NotImplementedException();
}public int Count
{
get
{
throw new NotImplementedException();
}
}public bool IsSynchronized
{
get
{
throw new NotImplementedException();
}
}public object SyncRoot
{
get
{
throw new NotImplementedException();
}
}public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}具体的代码上面已经给出,下面给出Column的示例代码:
class Column
{
public string Name
{
get;
set;
}public object Value
{
get;
set;
}public System.Data.DbType DataType
{
get;
set;
}public string DbColumnName
{
get;
set;
}public int Length
{
get;
set;
}public bool IsNull
{
get;
set;
}
}固然上面给出的不是完整的,只是为了演示说明。这样就能完成与XML之间的映射,具体的访问的过程当中也能够相对来讲更友好一些。
public void Test()
{
CommonObject comObj = CommonFactory<CommonObject>();comObj.Add(new Column());
comObj["test"] = new Column();
}private T CommonFactory<T>()
{
return (T)Activator.CreateInstance(typeof(T));
}
下面给出实现的思路吧:
咱们经过利用.NET4.0中的dynimic来定义一个内存中的实例,该实例是经过XML文件 中的配置的列属性来动态的建立,具体的代码以下:
static void Main(string[] args)
{
dynamic obj= GetTest();
Console.WriteLine(obj.name);
Console.WriteLine(obj.tt);
System.Threading.Thread.Sleep(10000);
}private static dynamic GetTest()
{
dynamic obj = new ExpandoObject();var person = obj as IDictionary<string, object>;
person["name"] = "test";
person["tt"] = "aaa";return obj;
}经过上面的这个思路,咱们就能够以下来作:
private static dynamic GetTest1(string xmlFile)
{
dynamic obj = new ExpandoObject();var person = obj as IDictionary<string, object>;
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlFile);Property [] list = XMLHelper.GetPropertys(doc);
foreach (Property property in list)
{
person[property.Name] = property.Value;
}person["name"] = "test";
person["tt"] = "aaa";return obj;
}这样就完成了,动态建立对象的过程,而且在应用能够之间使用动态建立的对象,可是有以下问题
经过上图中的描述,我想你们都知道,动态类型是动态运行时的编译,不是静态编译,必须在运行时才能解析出动态对象的属性信息,所以咱们使用起来并不方便,因此我想到以下办法。
我思考完,发现若是要是能够实现这样的插件也就行了,固然目前的思路就到这,查看了相关职能感知的相关文件,可是没有发现API能够直接设置或者能够之间进行控制的方法。
固然我上面只是给了几种可能可行的方案,可是不必定是合适的方案,还请你们多多指教和拍砖,若是您有好的思路或者实现方案,还请您提出来,多谢您的指
教。
下篇若是我有好的实现思路,我会给出完整的实现,固然若是没有思路,那就暂时写到这里,多谢你们的集思广益,可能我钻牛角尖了,也说不定。