在实际的项目中,除了集成ABP框架的EntityFrameworkCore以外,在有些特定的场景下不行制止地会利用一些SQL查询语句,一方面是由于此刻的EntityFrameworkCore2.X有些问题没有办理,别的一方面是基于机能方面的思量,在相识本篇内容之前,首先照旧来看看官方文档来给出的说明。
凭据官方的先容整体可以分为下面的步调:1 安装依赖包。2 添加DependsOn属性标签。3 Entity to Table Mapping。 4 Usage 通过上面的4个步调我们就可以或许正常在Asp.Net Core项目中利用ABP Dapper了,下面我们就详细的进程来做进一步的说明。
一 安装包依赖这个不做过多的表明,通过Nuget 包揽理器可能通过措施包揽理节制台来添加Abp.Dapper的引用,在我们实际的项目中整个类库的布局如下图所示,包括Dapper和EntityFrameworkCore两种方案。
二 添加DependsOn属性标签后头我们就需要在我们当前类库项目中独一的SalesDataModule中来做一些初始化和添加DependsOn标签的操纵了。
[DependsOn(typeof(AbpZeroCoreEntityFrameworkCoreModule))] [DependsOn(typeof(AbpDapperModule))] public class SalesDataModule : AbpModule { public override void Initialize() { IocManager.RegisterAssemblyByConvention(typeof(SalesDataModule).GetAssembly()); DapperExtensions.DapperExtensions.SetMappingAssemblies(new List<Assembly> { typeof(SalesDataModule).GetAssembly() }); } }
这里我们应该相识为什么要添加依赖干系?这个我们当前的SalesDataModule会依赖于AbpDapperModule和AbpZeroCoreEntityFrameworkCoreModule,确立了这样的依赖干系后,在ABP框架中就会将当前Module所依赖的其它Module放到List<AbpModule>的前面,这样通过这样对的层层依赖干系举办拓扑排序就可以或许担保被依赖的AbpModule必然先举办初始化操纵,这样就可以或许制止引用干系的错误,从而导致代码逻辑的错误,详细说来:假如A 依赖于B,B依赖于C,那么这三个模块之间的排序为C B A,这样在整个Module系统初始化的时候,会先执行Module C的PreIntialize()、Initialize()、PostInitialize()要领,我们看看ABP中的源码。
public virtual void StartModules() { var sortedModules = _modules.GetSortedModuleListByDependency(); sortedModules.ForEach(module => module.Instance.PreInitialize()); sortedModules.ForEach(module => module.Instance.Initialize()); sortedModules.ForEach(module => module.Instance.PostInitialize()); }
这个内里sortedModules就是通过这种依赖干系举办拓扑排序的,然后依次这行每个模块中的这几个要领举办一些初始化的操纵。
三 Entity to Table Mapping这个凭据官方的表明就是在成立Entity和数据库实体之间的干系,这个雷同于在Domain层实体上面添加【Table】标签(小我私家领略),在这个内里我们还能添加一些其它特性,好比Ignore掉一些导航属性等等......
public sealed class VehicleOrderPlanMapper : ClassMapper<VehicleOrderPlan> { public VehicleOrderPlanMapper() { Table("VehicleOrderPlan"); Map(x => x.Branch).Ignore(); AutoMap(); } }
四 应用这个部门就团结详细的项目来谈一谈一些细节方面的对象,我们先来看看详细的代码。
public class VehicleOrderPlanDapperRepository : DcsDapperRepositoryBase<VehicleOrderPlan>, IVehicleOrderPlanDapperRepository { public VehicleOrderPlanDapperRepository(IActiveTransactionProvider activeTransactionProvider) : base(activeTransactionProvider) { } public IEnumerable<WeeklyOrderPlanSummary> GetWeeklyOrderPlanSummary(int? yearOfPlan, int? weekOfPlan, string provinceName, [CanBeNull]VehicleOrderPlanType[] planType, string marketName, PageRequest page) { var sqlParam = new StringBuilder() .AppendIf(yearOfPlan.HasValue, $" AND p.YearOfPlan = :{nameof(yearOfPlan)}") .AppendIf(weekOfPlan.HasValue, $" AND p.WeekOfPlan = :{nameof(weekOfPlan)}") .AppendIf(!provinceName.IsNullOrWhiteSpace(), $@" AND EXISTS ( SELECT 1 FROM Company C WHERE c.Id = p.DealerId AND c.Status <> {(int)MasterDataStatus.作废} AND c.ProvinceName like '%' || :{nameof(provinceName)} || '%')"); var planTypes = new[] { VehicleOrderPlanType.周度打算, VehicleOrderPlanType.小品种打算, VehicleOrderPlanType.移库打算 }; if (planType != null && planType.Length > 0) planTypes = planTypes.Intersect(planType).ToArray(); var departmentParam = string.Empty; if (!marketName.IsNullOrWhiteSpace()) departmentParam = $" AND (m.Name LIKE '%' || :{nameof(marketName)} || '%')"; var sql = $@" SELECT p.YearOfPlan, p.WeekOfPlan, TRUNC(p.CreateTime) AS CreateTime, TRUNC(p.StartTime) AS StartTime, TRUNC(p.EndTime) AS EndTime, pd.ProductCode, pd.ProductName, pd.ProductType, pd.ProductCategoryCode AS VehicleModelCode, pd.ProductCategoryName AS VehicleModelName, Sum(pd.PlannedQuantity) AS PlannedQuantity, Sum(pd.FirstPlannedQuantity) AS FirstPlannedQuantity, Sum(pd.QuantityOfAssessment) AS QuantityOfAssessment, Sum(pd.ConfirmedQuantity) AS ConfirmedQuantity FROM VehicleOrderPlan p CROSS JOIN VehicleOrderPlanDetail pd WHERE (p.Status <> {(int)VehicleOrderPlanStatus.作废} AND p.Type in {planTypes.ToSqlInParam()} {sqlParam} AND EXISTS ( SELECT 1 FROM DealerMarketDptRelation dm WHERE (((dm.BranchId = p.BranchId) AND (dm.DealerId = p.DealerId)) AND (dm.Status = {(int)BaseDataStatus.有效})) AND EXISTS ( SELECT 1 FROM MarketingDepartment m WHERE ((m.BranchCode = {SunlightConsts.DEFAULT_BRANCH_QRSALESLTD}) AND (m.Status = {(int)BaseDataStatus.有效})) {departmentParam} AND (m.Id = dm.MarketId)))) AND (p.Id = pd.VehicleOrderPlanId) GROUP BY p.YearOfPlan, p.WeekOfPlan, TRUNC(p.CreateTime), TRUNC(p.StartTime), TRUNC(p.EndTime), pd.ProductCode, pd.ProductName, pd.ProductType, pd.ProductCategoryCode, pd.ProductCategoryName"; return QueryPaged<WeeklyOrderPlanSummary>(sql, page, new { yearOfPlan, weekOfPlan, provinceName, marketName }); } }