码迷,mamicode.com
首页 > 其他好文 > 详细

DDD领域模型企业级系统Linq的CRUD(一)

时间:2017-03-18 22:36:55      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:back   工厂   tar   prot   hello   cli   lin   rem   interface   

建造一个Product Module类:

 ProductDBContextDataContext dbcontext = new
      ProductDBContextDataContext();
      public List<Product> GetProducts()
      {
          var query = dbcontext.Product.Where(p => p.cid == 1).ToList();
          return query;
      }

      public object GetProductCS()
      {
          var query = from c in dbcontext.ProductCategory
                      join p in dbcontext.Product on c.cid equals p.cid
                      where p.cid == 1
                      select new { CName = c.cname, PName = p.pname, UnitPrice = p.unitprice };
          return query.ToList();
       }

      public List<Product> GetAllProducts(int skipcount, int currentpagecount)
      {
          var query = dbcontext.Product.Skip(skipcount).Take(currentpagecount).ToList();
          return query;
      }
      public void AddCP()
      {
          ProductCategory pc = new ProductCategory();
          pc.cid = 3;
          pc.cname = "c3";

          Product p1 = new Product();
          p1.pid = 5;
          p1.pname = "p5";

          Product p2 = new Product();
          p2.pid = 6;
          p2.pname = "p6";

          pc.Product.Add(p1);
          pc.Product.Add(p2);

          dbcontext.ProductCategory.InsertOnSubmit(pc);
          dbcontext.SubmitChanges();

      }

      public void Modifyp()
      {
          var product = dbcontext.Product.Where(p => p.pid == 5).SingleOrDefault();
          product.unitprice = 200;
          dbcontext.SubmitChanges();
      }

      public void DeleteP()
      {
          var product = dbcontext.Product.Where(p => p.pid == 5).SingleOrDefault();
          dbcontext.Product.DeleteOnSubmit(product);
          dbcontext.SubmitChanges();

      }

      public void DeletePS()
      {
          var product = dbcontext.Product.Where(p => p.pid == 5).ToList();
          dbcontext.Product.DeleteAllOnSubmit(product);
          dbcontext.SubmitChanges();
      }

 静态页面:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="LinqToSQLWeb._Default" %>


<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="显示产品" />
    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="连接显示产品" />
    <br />
    <asp:TextBox ID="TextBox1" runat="server">1</asp:TextBox>
    <br />
    <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="显示所有产品" />
    <asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="上一页" />
    <asp:Button ID="Button5" runat="server" OnClick="Button5_Click" Text="下一页" />

    
    <br />
    <asp:Button ID="Button6" runat="server" OnClick="Button6_Click" Text="添加" />
    <asp:Button ID="Button7" runat="server" OnClick="Button7_Click" Text="修改" />
    <asp:Button ID="Button8" runat="server" OnClick="Button8_Click" Text="删除" />

    

</asp:Content>

 后台类:

  Product.Domain.Products products =
            new Product.Domain.Products();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            TextBox1.Text = "1";
            GetProductsBinding(2);
        }

     

        protected void Button4_Click(object sender, EventArgs e)
        {
            if (int.Parse(TextBox1.Text) > 0)
            {
                TextBox1.Text = (int.Parse(TextBox1.Text) - 1).ToString();
            }
            GetProductsBinding(2);
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            TextBox1.Text = (int.Parse(TextBox1.Text) + 1).ToString();
            GetProductsBinding(2);
        }

        private void GetProductsBinding(int count)
        {
            List<Product.Domain.Product> allproducts;
            if (int.Parse(TextBox1.Text) == 0)
            {
                allproducts = products.GetAllProducts(0, count).ToList();
            }
            allproducts = products.GetAllProducts((int.Parse(TextBox1.Text) - 1) * 2, count);
            GridView1.DataSource = allproducts;
            GridView1.DataBind();
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            products.AddCP();
        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            products.Modifyp();
        }

        protected void Button8_Click(object sender, EventArgs e)
        {
            products.DeleteP();
        }

 EF的实例:

 ProductSystemModelContainer productdbcontext = new ProductSystemModelContainer();

       public List<Product> GetAllProduct(int page,int count)
       {
           var query = productdbcontext.Product.Skip(page * count).Take(count).ToList();
           return query;
       }

       public object GetAllPC(int page, int count)
       {
           var query = productdbcontext.ProductCategory.Join(productdbcontext.Product, c => c.Id, p => p.ProductCategory.Id,
               (c, p) => new { CName = c.CategoryName, PName = p.ProductName, UnitPrice = p.UnitPrice }).OrderBy(p=>p.UnitPrice).Skip(page * count).Take(count).ToList();
           return query;
       }

       public void AddProduct()
       {
           ProductCategory c = new ProductCategory();
           c.Id = Guid.NewGuid();
           c.CategoryName = "c1";
           Product p = new Product();
           p.Id = Guid.NewGuid();
           p.ProductName = "p1";
           p.UnitPrice = 80;
           p.ProductCategory = c;

           productdbcontext.Set<ProductCategory>().Add(c);
           productdbcontext.Set<Product>().Add(p);

           productdbcontext.SaveChanges();


       }

       public void ModifyProduct()
       {
           var product = productdbcontext.Product.Where(p => p.ProductName == "p1").FirstOrDefault();
           product.UnitPrice = 55;
           productdbcontext.Entry(product).State =
               System.Data.Entity.EntityState.Modified;
           productdbcontext.SaveChanges();

       }

       public void RemoveProduct()
       {
           var product = productdbcontext.Product.Where(p => p.ProductName == "p1").FirstOrDefault();
           productdbcontext.Set<Product>().Remove(product);
           productdbcontext.SaveChanges();
       }

 前端代码:

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

    <br />
<asp:TextBox ID="TextBox1" runat="server">0</asp:TextBox>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="创建" />
    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="上一页" />
    <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="下一页" />
    <asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="修改" />
    <asp:Button ID="Button5" runat="server" OnClick="Button5_Click" Text="销毁" />


</asp:Content>

 后台代码:

 LINQEFService efservice = new LINQEFService();
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {
            var result = efservice.GetAllPC(int.Parse(TextBox1.Text), 2);
            GridView1.DataSource = result;
            GridView1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            efservice.AddProduct();
            Bind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (int.Parse(TextBox1.Text) > 0)
            {
                TextBox1.Text = (int.Parse(TextBox1.Text) - 1).ToString();
                Bind();
            }
        

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            TextBox1.Text = (int.Parse(TextBox1.Text) + 1).ToString();
            Bind();
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            efservice.ModifyProduct();
            Bind();
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            efservice.RemoveProduct();
            Bind();
        }

 

直接实例化服务端的弊端:

 技术分享

 Service  Locator体系架构模式:

技术分享

实例:

定义接口:IPrintService

public interface IPrintService
    {
        string Print(string msg);
    }

 子类PrintSerivceNew:

public class  PrintSerivceNew:IPrintService
    {
        public string Print(string msg)
        {
            return "SerivceNew:" + msg;
        }
    }

 子类:PrintService

 public class PrintService : IPrintService
    {
        public string Print(string msg)
        {
            return "Serivce1:" + msg;
        }
    }

 服务工厂ServiceFactory

 public abstract class ServiceFactory
    {
        public object GetService()
        {
            return this.DoGetService();
        }

        public abstract object DoGetService();

        public abstract Type SerivceType { get; }
    }

 具体实现工厂:PrintServiceFactory

public class PrintServiceFactory:ServiceFactory
    {
        public override object DoGetService()
        {
            return new PrintSerivceNew();
        }

        public override Type SerivceType
        {
            get
            {
                return typeof(IPrintService);
            }
        }
    }

 

具体的业务:

  public class ServiceLocator
    {
        private Dictionary<Type, ServiceFactory> servicedics =
            new Dictionary<Type, ServiceFactory>();
        public ServiceLocator()
        {
            foreach(var type in this.GetType().Assembly.GetExportedTypes())
            {
                if(type.IsSubclassOf(typeof(ServiceFactory)))
                {
                    var factory = (ServiceFactory)Activator.CreateInstance(type);
                    servicedics.Add(factory.SerivceType, factory);
                }
            }
        }

        public object GetServiceByType(Type type)
        {
            var factory = servicedics[type];
            return factory.GetService();
        }
    }

 调用:

static void Main(string[] args)
        {
            ServiceLocator servicelocator =
                new ServiceLocator();
            var objectservice=servicelocator.GetServiceByType(typeof(IPrintService));
            Console.WriteLine((objectservice as IPrintService).Print("hello"));
            Console.ReadLine();
        }

 

DDD领域模型企业级系统Linq的CRUD(一)

标签:back   工厂   tar   prot   hello   cli   lin   rem   interface   

原文地址:http://www.cnblogs.com/sunliyuan/p/6576281.html

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