在ASP.NET MVC中对表进行通用的增删改

解反射技术

2、了解C#3.0中扩展方法,分布类,Linq to object,Linq to sql

3、了解ASP.NET MVC

在项目中每添加一个表往往都要添加一套增删改代码,而且这些代码很多情况下都很相似,这里我们给出一个通用的解决方案供大家参考。

一、准备工作:

这里我们先要在数据库中添加两个表News和User如下图:然后拖到dbml中生成实体类。

这里我们先准备一个接口:ICommonTable

Code

public interface ICommonTable

{

int id { get; set; }

}

然后让News和User实体都继承于此接口

Code

public partial class News : ICommonTable

{

}

public partial class User : ICommonTable

{

}

二、通用删除操作

分别添加NewsList.aspx和UserList.aspx两个view,添加方式参见ASP.NET MVC实践系列2-简单应用

在这两个View中加入删除链接:

<%= Html.ActionLink("删除", "Delete", new { key = item.id, partialName="News" })%>

<%= Html.ActionLink("删除", "Delete", new { key = item.id, partialName="User" })%>

然后添加一个Controller:

public ActionResult Delete(string partialName, int? key)

{

RepositoryBase repositoryBase = new RepositoryBase(partialName);

repositoryBase.Delete(key ?? 0);

return RedirectToAction(partialName + "List");//返回到list

}

接下来我们介绍一下RepositoryBase :

public class RepositoryBase

{

public Type EntityType { get; private set; }

public RepositoryBase(string entityType)

{

Type type = GetBllTypeByName(entityType);

EntityType = type;

}

public ICommonTable CreateNew()

{

return (ICommonTable)Activator.CreateInstance(EntityType);

}

/// <summary>

/// 通过字符串获得其Type

/// </summary>

/// <param name="typeName"></param>

/// <returns></returns>

private static Type GetBllTypeByName(string typeName)

{

Type type = null;

var ass = AppDomain.CurrentDomain.GetAssemblies()

.Where(p => p.FullName.Contains("CommonCEDemo"));

foreach (var a in ass)

{

type = a.GetTypes().Where(p => p.Name == typeName).FirstOrDefault();

if (type != null)

break;

}

if (type == null)

{

throw new Exception("类型未定义:" + typeName);

}

return type;

}

public RepositoryBase(Type entityType)

{

EntityType = entityType;

}

public ICommonTable Get(int id)

{

DBDataContext db = Context.GetContext();

return db.GetTable(EntityType).Cast<ICommonTable>().FirstOrDefault(p => p.id == id);

}

public void Delete(int id)

{

ICommonTable bllTable = Get(id);

Context.GetContext().GetTable(EntityType).DeleteOnSubmit(bllTable);

Context.GetContext().SubmitChanges();

}

}

这里边重点要理解的就是GetBllTypeByName方法。有了这个方法我们就可以动态的通过名字获得相应的Type了。这里还有个问题就是DataContext是从何而来的,我们这里为了简单起见全程声明了一个DataContext没有考虑多线程的情况

public class Context

{

static DBDataContext context;

static Context()

{

if (context==null)

{

context = new DBDataContext();

}

}

public static DBDataContext GetContext()

{

return context;

相关推荐
yuhaiqiang10 小时前
上线一个人静态网站需要多少钱,难不难?
前端·后端·程序员
小Ti客栈21 小时前
Spring Boot 集成 Springdoc-OpenAPI 与 Knife4j实现接口文档与可视化调试
java·spring boot·后端
Ai拆代码的曹操21 小时前
Spring 事务 REQUIRES_NEW 嵌套调用:连接池翻倍的秘密
java·后端·spring
IT_陈寒1 天前
SpringBoot这个分页坑,我踩了三天才爬出来
前端·人工智能·后端
颜酱1 天前
05 | 召回前置准备:根据业务数据库生成各数据库(读取配置阶段)
前端·人工智能·后端
Wang's Blog1 天前
Go-Zero项目开发24: 基于Bitmap实现群聊消息已读未读
开发语言·后端·golang
Conan在掘金1 天前
鸿蒙报错速查:struct 里嵌 namespace 声明就炸,根因 + 真解法
后端
东方小月1 天前
从零开发一个 Coding Agent(三):EventStream 事件流通道设计与实现
前端·人工智能·后端
卫子miao1 天前
如何评价当前大语言模型的记忆机制?
后端·架构
神奇小汤圆1 天前
为什么 AQS 成为 Java 并发的基石?
后端