码迷,mamicode.com
首页 > 移动开发 > 详细

做自己的ORMapping Framework ---- 第六讲 开始ORMapping之旅,ORMapping怎么说

时间:2015-11-26 22:49:37      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

这几天公司有点事情,没怎么来这边,现在来这边介绍下ORMapping实现一些什么东西

说到ORMapping,当然少不了的就是对数据库的增删查改,这边的类图我简单弄了下

技术分享

下面的代码都是我在框架中的源码,这个编辑器很差,贴上来会有点难看,接口里的方法属性都是我平时开发中都会经常用到的,做什么的基本看名称就知道,这里还得说下,代码规范真的很重要,小朋友们写代码时候多注意点。

  

public interface IEntityAccesser
{
  bool Exist<T>(T obj); 存在监测
  void Insert<T>(T obj);增
  void Update<T>(T obj);改
  void Delete<T>(T obj);删
  bool Load<T>(T obj) where T : IEntity; 从数据库加载数据
  List<T> Search<T>(string condition = "", string order = "") where T : IEntity, new(); 查
  List<TEntity> Search<TOwner, TEntity>(TOwner owner, Expression<Func<TOwner, IEnumerable>> exp, string order = "") where TEntity : IEntity, new();查
  int ExecuteCommand(string cmdText);执行sql command

  void ExecuteStoreProcedure(string procedureName, IEnumerable<SPParameter> args); 执行存储过程
  object ExecuteStoreProcedureScalar(string procName, IEnumerable<SPParameter> args);执行存储过程 并获取返回值
}

public interface IORMapper : IEntityAccesser
{
  string And { get; }
  string Comma { get; }
  string ConditionSql { get; }
  string CountSql { get; }
  string DeleteSql { get; }
  string Dot { get; }
  string Equal { get; }
  string FindAttributeSql { get; }
  string FindSql { get; }
  string InsertSql { get; }
  string Join { get; }
  string KeySqlValueParameter { get; }
  string KeySqlValueParameterName { get; }
  string On { get; }
  string OrderSql { get; }
  string SemiColon { get; }
  string SetSql { get; }
  string UpdateSql { get; }
  string UpdateSqlValueParameter { get; }
  string UpdateSqlValueParameterName { get; }
  string WhereSql { get; }
  string LeftSquare { get; }
  string RightSquare { get; }
  string StoreProcedureExistSql { get; }
  string CreateDatabaseSql { get; }
  string DropDatabaseSql { get; }
  string CommandSpitterRegexString { get; }
  string DatabaseExistSql { get; }
  string MasterDatabaseName { get; }

  IDbConnection NewConnection();

  IDbConnection GetDatabaseConnection(string databaseName);

  void CreateDatabase(string databaseName);
  void ExecuteSqlResourceFile(Type typeInResourceAssembly, string fileQualifiedName, IDbConnection connection, bool splitCommands);
  void DropDatabase(string databaseName);
  bool DatabaseExist(string databaseName);
  bool StoreProcedureExist(string databaseName, string storeProceudreName);
  string GetDatabaseName();
  string GetDatabaseHost();
  bool IsSystemDatabase(string databaseName);
  void WaitUntilConnectionBeUsed(IDbConnection connection, int retryTimes = 20, int retryInterval = 500);

  bool ExecuteTrueFalseResultSqlQuery(string sqlStatement);

}

public interface IBatchORMapper : IORMapper
{
  string GetUpdateSetItem(string columnName, int parameterIndex);
  string GetParameterName(int index);

  bool Exist<T>(string condition, params object[] parameterValues);
  void Update<T>(string setSql, string condition, params object[] parameterValues);
  void Delete<T>(string condition, params object[] parameterValues);
  List<T> Search<T>(string condition, params object[] parameterValues) where T:new();
}

 

接下来就要真正开始我们的ORMapping类的实现了

 

public abstract class ORMapper : IBatchORMapper
{

}

 

[Export(typeof(IORMapper))]                    这里采用MEF进行不同数据的ORMapping拓展,我在这就以SQL server为例,其它都一样的
[ExportMetadata("EntityAccesserType", EntityAccesserType.SqlServer)]
public class SqlServerORMapper : ORMapper
{

}

 

public class EntityAccesserFactory           这边是对ORMapping进行管理与使用
{
  public static IEntityAccesser GetEntityAccesser(EntityAccesserType accesserType, string connectionString)
  {
    switch (accesserType)
    {
      case EntityAccesserType.SqlServer:          其它数据库类型类似               
        return new SqlServerORMapper(connectionString);
      default:
        throw new NotSupportedException();
    }
  }
}

 

这样的代码结构大家应该很容易明白吧,没有什么复杂的结构,不懂MEF的朋友可以查阅相关资料,这里就不多说了,今天就介绍到这里,下篇将会介绍库表信息的获取以及怎样自动生成你的SQL command

谢谢。

做自己的ORMapping Framework ---- 第六讲 开始ORMapping之旅,ORMapping怎么说

标签:

原文地址:http://www.cnblogs.com/hwy425/p/4998980.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!