DDD领域模型企业级系统Linq的CRUD(四)
建造一个Product Module类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 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(); } |
静态页面:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <%@ 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> |
后台类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | 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的实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 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(); } |
前端代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <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> |
后台代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 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
1 2 3 4 | public interface IPrintService { string Print( string msg); } |
子类PrintSerivceNew:
1 2 3 4 5 6 7 | public class PrintSerivceNew:IPrintService { public string Print( string msg) { return "SerivceNew:" + msg; } } |
子类:PrintService
1 2 3 4 5 6 7 | public class PrintService : IPrintService { public string Print( string msg) { return "Serivce1:" + msg; } } |
服务工厂ServiceFactory
1 2 3 4 5 6 7 8 9 10 11 | public abstract class ServiceFactory { public object GetService() { return this.DoGetService(); } public abstract object DoGetService(); public abstract Type SerivceType { get; } } |
具体实现工厂:PrintServiceFactory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class PrintServiceFactory:ServiceFactory { public override object DoGetService() { return new PrintSerivceNew(); } public override Type SerivceType { get { return typeof(IPrintService); } } } |
具体的业务:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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(); } } |
调用:
1 2 3 4 5 6 7 8 | static void Main(string[] args) { ServiceLocator servicelocator = new ServiceLocator(); var objectservice = servicelocator.GetServiceByType(typeof(IPrintService)); Console.WriteLine((objectservice as IPrintService). Print ( "hello" )); Console.ReadLine(); } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步