开源依旧:再次分享一个进销存系统

开篇

我之前发过一篇博文《两天完成一个小型工程报价系统(三层架构)》,不少朋友向我要源码学习,后来久而久之忘记回复了。今天我再分享一个进销存系统,只为学习,没有复杂的框架和设计模式,有的是我个人的理解,大家互相探讨技术才会提高。当然我的命名不是很规范,兄弟们凑合着看。:)

思想和架构

在传统的三层架构思想上扩展出N层架构,将业务逻辑层换成WCF服务。抽象工厂的引入提高了程序的扩展性,单利+缓存+反射则提升了程序的性能。数据库则换成了Oracle,所以相应的数据访问层也换成了OracleDal,当然你完全可以写SqlServerDal,因为我数据访问层接口都已定义好。

界面和控件的设计美化

总体思路和流程---数据库Oracle

数据库既然选择了Oracle,当然先必须安装好Oracle,然后再装Plsql,这一步也是很简单的。不清楚的话,可去查找资料。 对Oracle而言,数据库已服务形式存在,有几个数据库就对应几个服务,删除了数据库后相应的服务也没了,其次一个兼容程序监听着服务。这些都可以自行配置,我不啰嗦了,毕竟我也不熟。我把Oracle脚本导出了,大家只要复制到Commad Window里粘贴即可,但前期创建表空间和用户我还是稍微提一下:

  • 首先用你用plsql连接一个服务(数据库Orcl),用Connect as SysDBA进入。
  • 创建表空间:注意路径一定要已经存在。
create tablespace invoicing logging
 datafile 'C:\oracle\product\10.2.0\db_1\oradata\invoicing.dbf'
 size 32M
 autoextend on
 next 32M maxsize 1024M
 EXTENT MANAGEMENT LOCAL;
 
  
  • 找到左下角侧用户(Users),创建用户Invoicing,密码:Invoicing,分配权限:dba,connect
  • 用新创建的用户名和密码重新登录。
  • 找到Command Window,把我提供给你的脚本复制进去就OK了。

总体思路和流程---数据访问层IDAL

  • 一个通用的泛型接口:
 public interface IBaseService<T> where T :class
    {
        List<T> GetEntities(string sqlWhere);
        T GetOneEntityByID(int id);
        T AddEntity(T entity);
        bool UpdateEntity(T entity);
        bool DeleteEntity(string sqlWhere);
    }
  • 某个数据访问层接口实继承这个泛型接口
    public interface ICommodityService:IBaseService<Model.CommodityModel>
    {
      
    }

总体思路和流程---抽象工厂Abstract

 public abstract class DalFactory
    {
        public abstract IDAL.ICommodityService CommdityDal
        {
            get;
        }
        public abstract IDAL.IPurchaseCommodityService PurchaseCommdityDal
        {
            get;
        }
        public abstract IDAL.IPurchaseOrdersService PurchaseOrderDal
        {
            get;
        }
        public abstract IDAL.ISalesCommodityService SalesCommodityDal
        {
            get;
        }
        public abstract IDAL.ISalesOrdersService SalesOrderDal
        {
            get;
        }
        public abstract IDAL.IUserService UserDal
        {
            get;
        }
    }
 
 

总体思路和流程---数据访问层Dal

  • 为了提高效率,可以考虑缓存
        public override IDAL.ICommodityService CommdityDal
        {
            //缓存里拿
            get {

                OracleDAL.CommodityService instance = System.Web.HttpRuntime.Cache["CommodityDal"] as OracleDAL.CommodityService;
                if (instance == null)
                {
                    instance = new OracleDAL.CommodityService();
                    System.Web.HttpRuntime.Cache.Add("CommodityDal", instance, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
                }
                return instance;

            }
            
        }
  • OracleHelper和System.Data.OracleClient来实现数据访问层
namespace Insigma.Eyes.PSS.OracleDAL
{
    public class CommodityService:ICommodityService
    {

        public List<Model.CommodityModel> GetEntities(string sqlWhere)
        {
            string sql = string.Format("select * from commodity where 1=1 {0}",sqlWhere);
            List<Model.CommodityModel> listCommodities = new List<Model.CommodityModel>();
            //Using 限定对象使用的范围在花括号里面,出了花括号后释放资源
            using (OracleDataReader odr=OracleHelper.ExecuteReader(OracleHelper.ConnectionString, CommandType.Text, sql, null))
            {
                while (odr.Read())
                {
                    Model.CommodityModel commodity = new Model.CommodityModel();
                    commodity.ID = odr.GetInt32(0);
                    commodity.Name = odr.IsDBNull(1) ? "" : odr.GetString(1);
                    commodity.Type = odr.IsDBNull(2) ? "" : odr.GetString(2);
                    commodity.Manufacturer = odr.IsDBNull(3) ? "" : odr.GetString(3);
                    commodity.Inventory = odr.IsDBNull(4) ? 0 : odr.GetInt32(4);
                    commodity.UnitPrice = odr.IsDBNull(5) ? 0 : odr.GetDecimal(5);
                    commodity.Unit = odr.IsDBNull(6) ? "" : odr.GetString(6);
                    listCommodities.Add(commodity);
                }
            }
            return listCommodities;

        }

        public Model.CommodityModel GetOneEntityByID(int id)
        {
            string sqlWhere = string.Format(" and id={0}",id);
            List<Model.CommodityModel> list = GetEntities(sqlWhere);
            return list.SingleOrDefault(c => c.ID == id);
        }
        private int GetNewID()
        {
            string sql = "select s_commodity.nextval from dual";
            return int.Parse(OracleHelper.ExecuteScalar(OracleHelper.ConnectionString,CommandType.Text,sql,null).ToString());
        }

        public Model.CommodityModel AddEntity(Model.CommodityModel entity)
        {
            entity.ID = GetNewID();
            string sql = string.Format(@"insert into Commodity(ID,Name,Type,Manufacturer,Inventory,UnitPrice,Unit) 
                                                    values({0},'{1}','{2}','{3}',{4},{5},'{6}')", entity.ID, entity.Name, entity.Type, entity.Manufacturer, entity.Inventory, entity.UnitPrice, entity.UnitPrice);
            if (OracleHelper.ExecuteNonQuery(OracleHelper.ConnectionString,CommandType.Text,sql,null)>0)
            {
                return entity;
            }
            else
            {
                return null;
            }
        }

        public bool UpdateEntity(Model.CommodityModel entity)
        {
            string sql = string.Format("update Commodity set Name='{0}',Type='{1}',Manufacturer='{2}',Inventory={3},UnitPrice={4},Unit='{5}' where ID={6}",
                                                  entity.Name, entity.Type, entity.Manufacturer, entity.Inventory, entity.UnitPrice, entity.Unit, entity.ID);
            return OracleHelper.ExecuteNonQuery(OracleHelper.ConnectionString,CommandType.Text,sql,null)>0; 
        }

        public bool DeleteEntity(string sqlWhere)
        {
            string sql = string.Format("delete form Commodity where 1=1 {0}",sqlWhere);
            return OracleHelper.ExecuteNonQuery(OracleHelper.ConnectionString, CommandType.Text, sql, null) > 0;
        }

      
    }
}

总体思路和流程---业务逻辑层WCF

  • wcf是什么,最简单理解就是接口,契约,当然你可以更加深入研究。我学的也不深。
namespace Insigma.Eyes.PSS.BLLWCFService
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“ICommodityManagerService”。
    [ServiceContract]
    public interface ICommodityManagerService
    {
        [OperationContract]
        [FaultContract(typeof(Exception))]
        CommodityModel[] GetCommodities(string name,string type,string manufacturer,string priceLow,string priceHigh);

        [OperationContract]
        CommodityModel[] GetCommoditiesByCondition(string condition);

        [OperationContract]
        CommodityModel GetOneCommodity(int id);

        [OperationContract]
        [FaultContract(typeof(Exception))]
        CommodityModel AddCommodity(CommodityModel oneCommodity);

        [OperationContract]
        [FaultContract(typeof(Exception))]
        bool UpdateCommodity(Model.CommodityModel commodity);
    }
}
  • 实现上面定义的接口契约:
    public class PurchaseManagerService : IPurchaseManagerService
    {

        
        //
        private AbstractFactory.DalFactory dataFactory = null;
        public PurchaseManagerService()
        {
            dataFactory = DefaultProviderDal.DefaultDataProviderFactory;
        }



       


        public Model.PurchaseOrdersModel[] GetPurchaseOrders(string orderNumber, string orderDateStart, string orderDateEnd, string status)
        {
            string sqlWhere = "";
            if (!string.IsNullOrWhiteSpace(orderNumber))
            {
                sqlWhere += string.Format(" and orderNumber like '%{0}%'", orderNumber);
            }
            if (!string.IsNullOrWhiteSpace(orderDateStart))
            {
                try
                {
                    DateTime dt = DateTime.Parse(orderDateStart);
                    sqlWhere += string.Format(" and orderDate>=to_date('{0}','yyyy-MM-dd')", dt.ToString("yyyy-MM-dd"));
                }
                catch
                {
                    Exception oe = new Exception();
                    throw new FaultException<Exception>(oe, "查询条件开始时间有误!");
                }
            }

            if (!string.IsNullOrWhiteSpace(orderDateEnd))
            {
                try
                {
                    DateTime dt = DateTime.Parse(orderDateEnd);
                    sqlWhere += string.Format(" and orderDate<=to_date('{0}','yyyy-MM-dd')", dt.ToString("yyyy-MM-dd"));
                }
                catch
                {
                    Exception oe = new Exception();
                    throw new FaultException<Exception>(oe, "查询条件截至时间有误!");
                }
            }
            if (!string.IsNullOrWhiteSpace(status))
            {
                sqlWhere += string.Format(" and Status='{0}'", status);
            }
            //IDAL.IPurchaseOrdersService purchaseOrdersService = new OracleDAL.PurchaseOrderService();
            //return purchaseOrdersService.GetEntities(sqlWhere).ToArray();
            return dataFactory.PurchaseOrderDal.GetEntities(sqlWhere).ToArray();
        }

        public Model.PurchaseCommodityModel[] GetPurchaseCommoditiesByID(int purchaseID)
        {
            string sqlWhere = string.Format(" and PurchaseOrderID={0}",purchaseID);//看看数据库里面的字段
            //IDAL.IPurchaseCommodityService purchaseCommodityService =new OracleDAL.PurchaseCommodityService();
            //return purchaseCommodityService.GetEntities(sqlWhere).ToArray();

            return dataFactory.PurchaseCommdityDal.GetEntities(sqlWhere).ToArray();
        }

        public Model.PurchaseCommodityModel AddPurchaseCommodityModel(Model.PurchaseCommodityModel onePurchaseCommodity)
        {
            //return new OracleDAL.PurchaseCommodityService().AddEntity(onePurchaseCommodity);
            return dataFactory.PurchaseCommdityDal.AddEntity(onePurchaseCommodity);
        }

       //几个ID要分清楚
        public bool PostPurchaseOrder(int id)
        {
            Model.PurchaseOrdersModel oneOrder=GetOnePurchaseOrder(id);
            if (oneOrder.Status.Equals("已入库"))
            {
                Exception oe = new Exception();
                throw new FaultException<Exception>(oe,"订单已经提交,请务重复提交");
            }
            List<Model.PurchaseCommodityModel> purchaseCommoditiesList = GetPurchaseCommoditiesByID(id).ToList();
            IDAL.ICommodityService commodityService = new OracleDAL.CommodityService();
            foreach (Model.PurchaseCommodityModel onePurchaseCommodity in purchaseCommoditiesList)
            {
                Model.CommodityModel commodityModel = new Model.CommodityModel();
                commodityModel.ID = onePurchaseCommodity.CommodityID;
                commodityModel.Manufacturer = onePurchaseCommodity.CommodityManufacturer;
                commodityModel.Name = onePurchaseCommodity.CommodityName;
                commodityModel.Type = onePurchaseCommodity.CommodityType;
                commodityModel.Unit = onePurchaseCommodity.CommodityUnit;
                commodityModel.UnitPrice = onePurchaseCommodity.CommodityUnitPrice;
                commodityModel.Inventory = onePurchaseCommodity.CommodityInventory + onePurchaseCommodity.Count;
                //这儿不会出现异常了吧,否则要回滚
                commodityService.UpdateEntity(commodityModel);
            }
            oneOrder.Status = "已入库";
            return new OracleDAL.PurchaseOrderService().UpdateEntity(oneOrder);

        }

        public Model.PurchaseOrdersModel GetOnePurchaseOrder(int id)
        {
            //return new OracleDAL.PurchaseOrderService().GetOneEntityByID(id);
            return dataFactory.PurchaseOrderDal.GetOneEntityByID(id);
        }


        public Model.PurchaseCommodityModel GetOnePurchaseCommoditiesByID(int purchaseCommodityID)
        {
            //return new OracleDAL.PurchaseCommodityService().GetOneEntityByID(purchaseCommodityID);
            return dataFactory.PurchaseCommdityDal.GetOneEntityByID(purchaseCommodityID);
        }


        public bool UpdatePurchaseCommodity(Model.PurchaseCommodityModel model)
        {
            //return new OracleDAL.PurchaseCommodityService().UpdateEntity(model);
            return dataFactory.PurchaseCommdityDal.UpdateEntity(model);
        }

        public bool DeletePurchaseCommodity(int id)
        {
            string sqlWhere = " and id=" + id;
            //return new OracleDAL.PurchaseCommodityService().DeleteEntity(sqlWhere);
            return dataFactory.PurchaseCommdityDal.DeleteEntity(sqlWhere);
        }


        public Model.PurchaseOrdersModel AddPurchaseOrder(Model.PurchaseOrdersModel purchaseOrder)
        {
            //return new OracleDAL.PurchaseOrderService().AddEntity(purchaseOrder);
            return dataFactory.PurchaseOrderDal.AddEntity(purchaseOrder);
        }

        public bool UpdatePurchaseOrder(Model.PurchaseOrdersModel onePurchaseOrder)
        {
            //return new OracleDAL.PurchaseOrderService().UpdateEntity(onePurchaseOrder);
            return dataFactory.PurchaseOrderDal.UpdateEntity(onePurchaseOrder);
        }


        public bool DeletePurchaseCommoditiesByPurchaseOrderID(int purchaseOrderID)
        {
            string sqlWhere = " and PurchaseOrderID=" + purchaseOrderID;
            //调用另一个模块,调用BLL比较好
            //return new OracleDAL.PurchaseCommodityService().DeleteEntity(sqlWhere);
           return dataFactory.PurchaseCommdityDal.DeleteEntity(sqlWhere);
        }

        public bool DeleteOrderID(int id)
        {
            string sqlWhere = string.Format(" and id={0}", id);
            //return new OracleDAL.PurchaseOrderService().DeleteEntity(sqlWhere);
            return dataFactory.PurchaseOrderDal.DeleteEntity(sqlWhere);
        }
    }
}
  • dataFactory = DefaultProviderDal.DefaultDataProviderFactory;其实是个单利,我只要反射出一次工厂足以。
 public abstract class DalFactory
    {
        public abstract IDAL.ICommodityService CommdityDal
        {
            get;
        }
        public abstract IDAL.IPurchaseCommodityService PurchaseCommdityDal
        {
            get;
        }
        public abstract IDAL.IPurchaseOrdersService PurchaseOrderDal
        {
            get;
        }
        public abstract IDAL.ISalesCommodityService SalesCommodityDal
        {
            get;
        }
        public abstract IDAL.ISalesOrdersService SalesOrderDal
        {
            get;
        }
        public abstract IDAL.IUserService UserDal
        {
            get;
        }
    }

总体思路和流程---UI

  • 对WCF而言,实例化对象越多(如CommodityManagerServiceClient类的实例),对服务器压力越大,所以也可以考虑单利。
    public class WCFServiceBLL
    {
       //对WCF而言,对象实例化越多,对服务器压力越大。
        static BLLCommodity.CommodityManagerServiceClient commodityClient;
        static BLLSalesOrders.SalesManagerServiceClient salesClient;
        static BLLUsers.UserManagerServiceClient userClient;
        static BLLPurchaseOrders.PurchaseManagerServiceClient purchaseClient;
        public static CommodityManagerServiceClient GetCommodityService()
        {
            if (commodityClient==null)
            {
                commodityClient = new CommodityManagerServiceClient();
            }
            if (commodityClient.State==CommunicationState.Closed)
            {
                commodityClient = new CommodityManagerServiceClient();
            }
            if (commodityClient.State==CommunicationState.Faulted)
            {
                commodityClient = new CommodityManagerServiceClient();
            }
            return commodityClient;

补充

由于数据库之间的主外键关系以及多表查询,为了方便,我创建了视图,这和SqlServer里面是一样的(Oracle里面是Create Or Replace),当然你也可以建立外键对象,我上个项目就是这么干的,当然ORM我们就不讨论了。

create or replace view v_purchasecommodity as
select pc.id,pc.purchaseorderid,pc.commodityid,c.name CommodityName,
c.type commodityType,c.manufacturer CommodityManufacturer,c.inventory CommodityInventory,
c.unitprice CommodityUnitPrice,c.unit CommodityUnit,pc.count,pc.purchaseprice,pc.totalprice
from purchasecommodity pc inner join commodity c on pc.commodityid = c.id;

再补充一点:Oracle的自动增长列并不是像SqlServer那样设置一下,就会自动增长,Oracle里面需要你自己创建Sequences,图形操作也行,命令也行,我导出的Oracle脚本里面已经包含了相应的Sequences,应该可以看懂的。其余差别不大,相信你能看懂。

create sequence INVOICING.S_USERS
minvalue 1
maxvalue 999999999999999999999999999
start with 1
increment by 1
cache 20;

 关于界面美化的一些心得: Winform程序功能很重要,但能提高用户体验那是最完美的,所以我用了一些图标,设置相应的大小,当然用户控件也是很重要的一部分,用户控件嵌套在窗体里面是很简单的:

  public class LoadControls
    {
        public static void LoadInventory(Control parent)
        {
            parent.Controls.Clear();
            Inventory inventory = new Inventory();
            inventory.Dock = DockStyle.Fill;
            parent.Controls.Add(inventory);
        }
        public static void LoadPurchase(Control parent)
        {
            parent.Controls.Clear();
            Purchase purchase = new Purchase();
            purchase.Dock = DockStyle.Fill;
            parent.Controls.Add(purchase);
        }
        public static void LoadSales(Control parent)
        {
            parent.Controls.Clear();
            Sales sales = new Sales();
            sales.Dock = DockStyle.Fill;
            parent.Controls.Add(sales);
        }
 

总结

没有什么犹豫就写完了这篇博文,我把源代码贡献出来,当然这个例子只为学习,有需要的兄弟们可以拿去参考参考,大家多交流交流,才会相互促进进步,如果您觉得满意的话,不放支持我一下,帮忙顶个,有动力才有精力写出更好的博客,3x:) 下载

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏NetCore

【翻译】在Visual Studio中使用Asp.Net Core MVC创建你的第一个Web API应用(一)

HTTP is not just for serving up web pages. It’s also a powerful platform for bui...

26350
来自专栏魏琼东

分享一个分布式消息总线,基于.NET Socket Tcp的发布-订阅框架,附代码下载

一、分布式消息总线      在很多MIS项目之中都有这样的需求,需要一个及时、高效的的通知机制,即比如当使用者A完成了任务X,就需要立即告知使用者B任务X已经...

41570
来自专栏码农阿宇

EF Core利用Transaction对数据进行回滚保护

What? 首先,说一下什么是EF Core中的Transaction Transaction允许以原子方式处理多个数据库操作,如果事务已提交,则所有操作都应用...

37950
来自专栏魏琼东

分享一个分布式消息总线,基于.NET Socket Tcp的发布-订阅框架,附代码下载

     在很多MIS项目之中都有这样的需求,需要一个及时、高效的的通知机制,即比如当使用者A完成了任务X,就需要立即告知使用者B任务X已经完成,在通常的情况下...

45800
来自专栏dotnet & java

WCF 入门 (21)

其实不太了解为什么第21集才讲这个Binding,下面都是一些概念性的东西,不过作为一个入门视频,了解一下也无妨吧。

9450
来自专栏FreeBuf

Windows Server 2012 R2的提权过程解析

近期,我在进行一项安全评估的过程中遇到了一个麻烦。这是某个组织的一台远程桌面服务器,安装的是Windows Server 2012 R2系统,但是我手中的用户账...

700100
来自专栏Porschev[钟慰]的专栏

Asp.Net统一前后端提示信息方案

要解决的问题 减少弹框前后端代码量 增强可维护性 前后端提示资源统一 可以实现简单的多语言提示方案 增强重用性和易用性       ... ...

22450
来自专栏恰童鞋骚年

自己动手模拟开发一个简单的Web服务器

开篇:每当我们将开发好的ASP.NET网站部署到IIS服务器中,在浏览器正常浏览页面时,可曾想过Web服务器是怎么工作的,其原理是什么?“纸上得来终觉浅,绝知此...

25630
来自专栏hbbliyong

WCF 学习总结2 -- 配置WCF

前面一篇文章《WCF 学习总结1 -- 简单实例》一股脑儿展示了几种WCF部署方式,其中配置文件(App.config/Web.config)都是IDE自动生成...

31670
来自专栏菩提树下的杨过

用VS2010调试微软开放的部分源码

msdn上有一篇讲解如何用vs2008调试源码的文章:http://blogs.msdn.com/b/sburke/archive/2008/01/16/con...

23750

扫码关注云+社区

领取腾讯云代金券