当前位置: 首页 > news >正文

在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;

http://www.jsqmd.com/news/1132375/

相关文章:

  • Selenium 高级进阶操作详解
  • p006-py文件编译成pyd
  • Linux内核CFS完全公平调度器:从vruntime到负载均衡的深度实现分析
  • How-To: Using the N* Stack, part 3
  • GEO代理接单后总部负责落地吗
  • PowerShell 路径规则详解:从基础到高级
  • 2026杭州初中毕业女生暑假学什么好?选对方向比努力更重要
  • 剪映专业版教程:制作西施跳广场舞效果
  • IPC-2152 标准深度解析:3大常见误区与5个影响通流的关键PCB设计参数
  • MLflow在LLM评估中的工程实践:实现可追溯、可比较、可归因的模型管理
  • 06-高级模式与实战项目——01. Render Props - 共享渲染逻辑
  • AI产品设计的底层逻辑:认知减负与人机信任感构建
  • Windows Mobile下访问Sqlite的Native C++封装
  • 数据分析转大模型:换个角度,从方案设计到上线检查
  • 域名与DNS批量管理实战:OpenClaw自动解析检测、批量修改与监控全攻略
  • Google chrome OS vmdk文件在WMware下运行的办法
  • TFT-LCD 驱动架构对比:4 种 Cs 存储电容布局的优缺点与选型指南
  • 高空航拍地面建筑物数据集7682张VOC+YOLO格式
  • 多品牌集合店营收分配程序,测算设计师品牌,快时尚,奢品搭配销售最优比例。
  • 商用轨道插座怎么选更划算 各品牌性价比盘点帮你避坑少花冤枉钱
  • JD Cloud 验证码逆向
  • 【全文系列目录】风控PM记
  • Burp Suite Intruder 4种攻击模式实战:Sniper/Cluster Bomb 对比与 3 个典型场景应用
  • LLM的“类人认知“,到底是能力涌现还是统计模仿?
  • XCA 2.9.0:企业级PKI证书管理的技术架构与实战解决方案
  • 私密科普:女性经后淋漓不尽,别当成普通经期残留
  • 终极指南:企业级Docker化邮件中继服务部署与架构实践
  • 机房故障换机后应急验证:24 小时 SpeedCE 点检 SOP
  • AI编程助手实战指南:从原理到应用,GitHub Copilot与Cursor深度测评
  • 【操作系统】页面置换算法(CLOCK/改进型CLOCK)