发了前面两篇文章后,有的同学说:“分层应该依项目而定,没有什么最好的分层,只有最合适的。” 也有同学说 “分层太复杂,才多大的项目”
在这里我想发表下我的看法,个人之言,权当讨论的话题:首先说分层应该依项目而定,这话总体来说没有错误,但是到底什么样的项目该用什么样的分层
这个没有定数,至少我还不知道这个确切的分界线~况且,我们现在可能做个小项目,到以后也许慢慢发展起来怎么办?
对于这个分层是否过于复杂,个人并不是这么看的,至少这么分层,每个分层都有其中不可少的因素在里面。前期搭建好以后,也许项目真的发展起来后,
会有助于项目真正的开发。当然,如果是小项目,个人也觉得没有必要分太多层。
好了,现在说说,我们这个EF的底层框架怎么搭建。
首先 我们现在数据库建一个表 Product 表结构如下:
在CinDou.EFramework层中建立 CinDouDbContext.cs 文件,代码如下
using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Data.Entity.ModelConfiguration.Conventions; using CinDou.Tools; using CinDou.Model; namespace CinDou.EFramework { string connection="xxxxx"; //数据库链接 public class CinDouDbContext : DbContext { public AppedDbContext() : base(connection) { } public DbSet<Product> Products{get; set;} protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();//移除复数表名的契约 modelBuilder.Conventions.Remove<IncludeMetadataConvention>();//防止黑幕交易 要不然每次都要访问 EdmMetadata这个表 } } }
在 CinDou.Model 项目中建立 Product.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel.DataAnnotations; namespace CinDou.Model { public class Product { public int ID { get; set; } [Column("Name")] public string ProductName { get; set; } [Column("Price")] public decimal ProductPrice { get; set; } public DateTime CreateDate { get; set; } } }
在BFactoy和DFactory层中分别建立BLLFactory。cs 和 DALFactory.cs
using System; using System.Collections.Generic; using System.Reflection; using System.Linq; using System.Text; using CinDou.Tools; using CinDou.IBLL; using CinDou.BLL; namespace CinDou.BFactory { public sealed class BLLFactory { static string bllLayer = WebSite.BLLAssembly; public static CinDou.IBLL.IProduct CreateProduct() { return (CinDou.IBLL.IProduct)Assembly.Load(bllLayer).CreateInstance(bllLayer + ".Product"); } } }
using System.Reflection; using CinDou.Tools; using CinDou.IDAL; using CinDou.DAL; namespace CinDou.DFactory { public sealed class DALFactory { static string dalLayer = WebSite.DALAssembly; public static CinDou.IDAL.IProduct CreateProduct() { return (CinDou.IDAL.IProduct)Assembly.Load(dalLayer).CreateInstance(dalLayer + ".Product"); } } }
这里,我们使用了反射和工厂模式。这样便于解耦和层的封闭
在CinDou.IBLL和CinDou.IDAL中分别建立IProduct.cs文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using CinDou.Model; namespace CinDou.IBLL { public interface IProduct { bool Add(CinDou.Model.Product product); IEnumerable<CinDou.Model.Product> List(); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using CinDou.Model; namespace CinDou.IDAL { public interface IProduct { bool Add(Product product); IEnumerable<Product> List(); } }
然后再在CinDou.BLL和CinDou.DAL中建立Product.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using CinDou.IBLL; using CinDou.DFactory; using CinDou.IDAL; using CinDou.DAL; using CinDou.Model; namespace CinDou.BLL { public class Product : CinDou.IBLL.IProduct { public bool Add(CinDou.Model.Product product) { return CinDou.DFactory.DALFactory.CreateProduct().Add(product); } public IEnumerable<CinDou.Model.Product> List() { return CinDou.DFactory.DALFactory.CreateProduct().List(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Entity; using CinDou.IDAL; using CinDou.EFramework; using CinDou.Model; using CinDou.Tools; namespace CinDou.DAL { public class Product : CinDou.IDAL.IProduct { private CinDouDbContext CinDouDB = new CinDouDbContext; public bool Add(CinDou.Model.Product product) { bool flag = false; try { CinDouDB.Products.Add(product); CinDouDB.SaveChanges(); flag = true; } catch{ } return flag; } public IEnumerable<CinDou.Model.Product> List() { return CinDouDBProducts.ToList(); } } }
我们再在CinDou.Route项目中建立 Controllers文件夹,然后建立ProductController.cs文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; using CinDou.IBLL; using CinDou.BFactory; namespace CinDou.Route.Controllers { public class ProductController : Controller { public ViewResult Index() { return View(CinDou.BFactory.BLLFactory.CreateProduct().List()); } public ActionResult Create() { return View(new CinDou.Model.Product()); } // // POST: /Department/Create [HttpPost] public ActionResult Create(CinDou.Model.Product product) { bool flag = CinDou.BFactory.BLLFactory.CreateProduct().Add(product); return RedirectToAction("Index"); } } }
在CinDou.Web项目中的Views文件夹下建立Product文件夹,在里面建立Index.cshtml文件
在建立的时候,借助vs的智能感应,能非常方便的建立index.cshtml文件中的内容。
@model IEnumerable<CinDou.Model.Product> <p> @Html.ActionLink("Create New", "Create") </p> <table> <tr> <th> ProductName </th> <th> ProductPrice </th> <th> CreateDate </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.ProductName) </td> <td> @Html.DisplayFor(modelItem => item.ProductPrice) </td> <td> @Html.DisplayFor(modelItem => item.CreateDate) </td> <td> @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | @Html.ActionLink("Details", "Details", new { id=item.ID }) | @Html.ActionLink("Delete", "Delete", new { id=item.ID }) </td> </tr> } </table>
项目至此,已经差不多建立完整了。最后再附上我的源码。便于理解吧~