您的位置:首页 > 编程语言 > ASP

如何做一个基于ASP.NET MVC 网站(五)

2016-07-28 15:05 639 查看
接着,我们来讲讲Service业务逻辑层!

这是个重点,像做什么,无论你是网站还是系统还是Api,Service必须写的好,不然后面的维护总是在坑自己!来,我们看看,如何写好一个Service层,并且你得学会些什么?

Service层,借鉴于面向接口开发的思想,这是很好的方法,有助于我们后续开发的维护。Service层主要分为下面两个方面:1.IService接口 2.类Service 类去实现接口中的抽象方法。

首先,我们先看一下I案例IService的代码

<span style="font-size:18px;"> public interface IGoodsService
{
//Insert
string InsertGoods(List<GoodsIn> listGoodIn, AssetManageFill main);
string InsertAllGoods(AllGoods details);
}</span>在IService里我们自定义了两个取数据的方法,里面也放着对应请求的参数。
接着,我们看一下Service里的东西就是实现IService里的方法,把方法具体实现!

<span style="font-size:18px;"> public class GoodsService : IGoodsService
{
#region 声明
//物品表
private readonly IRepository<Goods> _goodsRepository;
//物品型号表
private readonly IRepository<GoodsVesion> _vesionRepository;
//出入库子表
private readonly IRepository<AssetDetail> _assetDetailRepository;
//物品编号表
private readonly IRepository<GoodsNumber> _goodNumRepository;
//物品入库表
private readonly IRepository<AssetManageFill> _assetDetailFillRepository;

#endregion

#region 构造函数注入
public GoodsService(IRepository<Goods> goodsRepository, IRepository<AssetDetail> assetDetailRepository,
IRepository<GoodsNumber> goodNumRepository, IRepository<GoodsVesion> vesionRepository, IRepository<AssetManageFill> assetDetailFillRepository)
{
//物品表接口
this._goodsRepository = goodsRepository;
//物品编号表接口
this._goodNumRepository = goodNumRepository;
//资产管理子表接口
this._assetDetailRepository = assetDetailRepository;
//物品型号表接口
this._vesionRepository = vesionRepository;
//资产管理入库表接口
this._assetDetailFillRepository = assetDetailFillRepository;
}
#endregion

#region 方法
public string InsertGoods(List<GoodsIn> listGoodIn, AssetManageFill main)
{

string end = "2";
try
{
//入库主表插入数据

AssetManageFill m = new AssetManageFill();
m.Id = Guid.NewGuid().ToString();
m.FillPerson = main.FillPerson;
m.FillDepartment = main.FillDepartment;
m.FillAcceptance = main.FillAcceptance;
m.FillFlowId = main.FillFlowId;
m.FillTime = main.FillTime;
m.Total = main.Total;
m.TotalLower = main.TotalLower;
m.TreasuryNote = main.TreasuryNote;
m.AcceptanceNote = main.AcceptanceNote;
m.Summary = main.Summary;
m.WarehouseRejectedReason = main.WarehouseRejectedReason;
_assetDetailFillRepository.Insert(m);
_assetDetailFillRepository.SaveChanges();

//循环遍历每个对象
foreach (var item in listGoodIn)
{
if (item.GoodsName != "")
{
string goodsType = item.GoodsType;
string goodsUnit = item.GoodsUnit;
string goodsPrice = item.GoodsPrice;
string sourseOfGoods = item.SourseOfGoods;
DateTime storagetime = item.Storagetime;
string fillflowid = item.FillFlowId;
string acceptPerson = item.AcceptPerson;
string fillnumber = item.FillNumber;
string goodsName = item.GoodsName;
//得到GoodsID;
string goodsId = _goodsRepository.FirstOrDefault(s => s.GoodsName == goodsName).GoodsId;

bc46
//得到VersionId
string versionId = _vesionRepository.FirstOrDefault(s => s.GoodsId == goodsId).VersionId;
//得到最后一个GoodsNumber
//string goodsNumber = _assetDetailRepository.All().Where(s => s.GoodsNumber.Contains(goodNumHeader)).Max(s => s.GoodsNumber);
//总数
string acount = item.Acount;
//编号初始化
string goodsNumber = "";

//入库子表插入数据
var list = new List<AssetDetail>();
for (int n = 0; n <= Convert.ToInt32(acount) - 1; n++)
{
//获取对应的表单编号数据
var goodsNum = _goodNumRepository.FirstOrDefault(s => s.GoodsId == goodsId);
//获取编号头
string goodNumHeader = goodsNum.GoodsNumHeader;

//取编号
goodsNumber = goodsNum.GoodsNum;
int num;
if (goodsNumber != null)
{
num = Convert.ToInt32(goodsNumber.Replace(goodNumHeader, ""));
}
else
{
num = 1;
}
AssetDetail newitem = new AssetDetail();
//确认新GoodsNumber
newitem.GoodsNumber = goodNumHeader + GetNum(num);
newitem.GoodsId = goodsId;
newitem.VersionId = versionId;
newitem.GoodsName = goodsName;
newitem.GoodsPrice = goodsPrice;
newitem.GoodsUnit = goodsUnit;
newitem.GoodsType = goodsType;
newitem.SourseOfGoods = sourseOfGoods;
newitem.FillFlowId = fillflowid;
newitem.AcceptPerson = acceptPerson;
newitem.Storagetime = storagetime;
newitem.State = 1;
newitem.IsLock = 0;
newitem.IsDelete = 0;
newitem.FillNumber = fillnumber;
newitem.Id = Guid.NewGuid().ToString();
list.Add(newitem);

//更新编号表
var a = _goodNumRepository.FirstOrDefault(s => s.GoodsNumHeader == goodNumHeader);
a.GoodsNum = GetNum(num + 1);
_goodNumRepository.Update(a);
_goodNumRepository.SaveChanges();
}

_assetDetailRepository.BulkInsert(list);
_assetDetailRepository.SaveChanges();
end = "1";
}
}

//成功1,不成2
return end;
}
catch (Exception ex)
{
LogService.WriteLog(ex.Message + "\r\n" + ex.StackTrace, "wkf", LogService.LogLevel.Error);
return end;
}

}
public string GetNum(int num)
{
string goodsNum = "0000".Substring(0, 4 - num.ToString().Length) + Convert.ToString(num);
return goodsNum;
}
#endregion

public string InsertAllGoods(AllGoods details)
{
throw new NotImplementedException();
}
}</span>在Service层里我们实现了IService里的抽象方法,并返回了我们所需要的数据。
在NET MVC 我们采用Linq语言对数据进行操作。那Linq是什么?Linq语句是有什么组成的呢?

Linq对我而言,感觉就是Sql语句的另一种表现形式,Linq是有什么组成的?Linq是由Lemada表达式组成。那Lemada表达式有什么东西呢?我们数学都学过吧,和现实中的数学差不多的,就是一个个固定表达式  什么firstOrDafault()  什么All()方法明摆着就是对sql语句的另一种展示或优化。

对了,你也许会对这句话表示不明白,那很正常! private readonly IRepository<Goods> _goodsRepository;

这里用到了IOC注入和构造方法。IOC,依赖注入,我这边说的不是那么官方,就正如我之前所说的皇帝临幸三千佳丽的故事一样。要就拿来,不要就不注入进去,然后进行相应的处理。这里我们也看到了接口这个东西,就是数据的接口,是不是瞬间有点豁然开朗。Service就是这边对数据的操作,那Controller和意思一样 控制台 控制台 控制台,起到控制的作用,只要传递对应参数到Service里我们就可以获取数据。是不是很简单?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: