Ado.net entity data model搭建三层架构

这个三层不像MVC框架,因为只是业界的规范,所以理论上分开3层就可以了。

首先建立新的解决方案,建立类库Models,BLL,DAL,还有新建个网站WebForm,

如图

这个层次结构比较清晰的。Models层的edmx是如何建立的呢,接着看下面:

这里新建个项目,在安装的模版选择

ADO.NET Entity Data Model,然后是导航式向导,最后就可以建立了如图一的ORM框架映射了。如果说Java有hibernate,那么.net这个也可以算类似的框架了。

然后看看service层我们是如何操作数据库的:

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Data;
using Models;
using System.Data.Linq;
using System.Data.EntityModel;
using System.Data.EntityClient;
using System.Data.Entity;

namespace DAL
{
    
public static partial class TypesService
    {
        
private static CnaWineEntities db = new CnaWineEntities();

        
private static bool SaveChangesWithDB()
        {
            
if (db.SaveChanges() > 0)
            {
                
return true;
            }
            
else
            {
                
return false;
            }
        }

        
public static bool InsertEntity(Types type)
        {
            db.AddToTypes(type);
            
return SaveChangesWithDB();
        }

        
public static bool DeleteEntity(Types type)
        {
            db.DeleteObject(type);
            
return SaveChangesWithDB();
        }

        
public static bool DeleteEntityByKey(int id)
        {
            Types type 
= db.Types.Where(c => c.Id == id).SingleOrDefault();
            db.DeleteObject(type);
            
return SaveChangesWithDB();
        }

        
public static bool ModifyEntity(Types type)
        {
            
return SaveChangesWithDB();
        }

        
public static IList<Types> GetAllEntities()
        {
            
return db.Types.ToList();
        }

        
public static Types FetchEntityByKey(int id)
        {
            
return db.Types.Where(c => c.Id == id).SingleOrDefault();
        }
    }
}

这里部分代码没测试过的,例如有人说single方法支持不太好,可能会报错。这里先写原理吧。具体再调整。方法调整好后,可以利用codesmith等工具,生成自己的DAL,BLL模版,实现了EDM+三层结构方式。那DAL层如何写呢,这里也列举一下:

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Data;
using Models;
using DAL;
using System.Data.Linq;
using System.Data.EntityModel;
using System.Data.EntityClient;
using System.Data.Entity;

namespace BLL
{
    [DataObject]
    
public static partial class TypesManager
    {
        [DataObjectMethod(DataObjectMethodType.Insert)]
        
public static bool InsertEntity(Types type)
        {
            
return TypesService.InsertEntity(type);
        }

        [DataObjectMethod(DataObjectMethodType.Delete)]
        
public static bool DeleteEntity(Types type)
        {
            
return TypesService.DeleteEntity(type);
        }

        [DataObjectMethod(DataObjectMethodType.Delete)]
        
public static bool DeleteEntityByKey(int id)
        {
            
return TypesService.DeleteEntityByKey(id);
        }

        [DataObjectMethod(DataObjectMethodType.Update)]
        
public static bool ModifyEntity(Types type)
        {
            
return TypesService.ModifyEntity(type);
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        
public static IList<Types> GetAllEntities()
        {
            
return TypesService.GetAllEntities();
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        
public static Types FetchEntityByKey(int id)
        {
            
return TypesService.FetchEntityByKey(id);
        }
    }
}

 

ContractedBlock.gif ExpandedBlockStart.gif Code
//这里是页面的后台代码,只是普通的绑定而已
public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!IsPostBack) {
            GridView1.DataSource 
= TypesManager.GetAllEntities();
            GridView1.DataBind();
        }
    }
}

最后,记得在web.config配置连接字符串,把app.config里面的般过去就可以了。Linq to sql好像不用配置那个,web层不直接引用不用配也可以。反正是报错了有可能是这个配置问题。

然后一切正常,按ASP.net的写法开始写项目吧

转载于:https://www.cnblogs.com/drek_blog/archive/2009/11/06/1597455.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值