(#15)医疗机构前置机增加IOC模块,数据库模块

This commit is contained in:
falcon 2019-03-22 15:53:23 +08:00
parent aada97e27c
commit 2c00f5902f
10 changed files with 289 additions and 40 deletions

View File

@ -0,0 +1,119 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Timers;
using Autofac;
using CommonClass.Factory;
namespace CmdjyHisFront.Bll
{
public interface ITask
{
void Run();
}
/// <summary>
/// 任务管理器
/// </summary>
public interface ITaskManager
{
/// <summary>
/// 心跳间隔。毫秒
/// </summary>
double BackgroundTaskHeartbeat { get; set; }
/// <summary>
/// 执行任务
/// </summary>
void Start();
/// <summary>
/// 结束任务
/// </summary>
void Stop();
}
/// <summary>
/// 背景任务注册管理器
/// </summary>
public class TaskManagerRegister:IRegister
{
public void Register(ContainerBuilder builder) {
builder.Register<ITaskManager>(c => new TaskManager());
}
}
/// <summary>
/// 背景任务管理器
/// </summary>
public class TaskManager:ITaskManager
{
/// <summary>
/// 任务心跳触发计时器
/// </summary>
public static Timer CTimer { get; private set; }
/// <summary>
/// 要执行的背景线程
/// </summary>
public IEnumerable<ITask> BackTasks { get; set; }
public double BackgroundTaskHeartbeat { get; set; } = 1000;
/// <summary>
/// 创建一个背景任务管理器
/// </summary>
public TaskManager() { }
/// <summary>
/// 通过提供任务和背景心跳间隔创建背景任务管理器
/// </summary>
/// <param name="bt">背景任务枚举</param>
/// <param name="bh">心跳</param>
public TaskManager(IEnumerable<ITask> bt) {
this.BackTasks = bt;
}
/// <summary>
/// 通过提供任务和背景心跳间隔创建背景任务管理器
/// </summary>
/// <param name="bt">背景任务枚举</param>
/// <param name="bh">心跳</param>
public TaskManager(IEnumerable<ITask> bt,double bh) {
this.BackTasks = bt; this.BackgroundTaskHeartbeat = bh;
}
/// <summary>
/// 开始执行背景任务
/// </summary>
public void Start() {
if(this.BackTasks == null || this.BackTasks.Count() == 0) return;
CTimer = CTimer ?? new Timer(this.BackgroundTaskHeartbeat);
CTimer.Elapsed += this.run;
CTimer.AutoReset = false;
CTimer.Start();
}
/// <summary>
/// 任务处理事件
/// </summary>
private void run(object sender,ElapsedEventArgs e) {
//启动具体任务
foreach(var t in this.BackTasks) {
Task.Factory.StartNew(m => {
if(m is ITask task) {
task.Run();
}
},t as object);
}
//任务启动后重启心跳计时器
if(sender is Timer timer) {
timer.Start();
}
}
public void Stop() {
if(CTimer == null) return;
CTimer.Stop();
CTimer.Elapsed -= run;
CTimer = null;
}
}
}

View File

@ -45,6 +45,21 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Autofac, Version=4.9.1.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\packages\Autofac.4.9.1\lib\net45\Autofac.dll</HintPath>
</Reference>
<Reference Include="Autofac.Integration.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\packages\Autofac.Mvc5.4.0.2\lib\net45\Autofac.Integration.Mvc.dll</HintPath>
</Reference>
<Reference Include="CommonClass.Factory, Version=1.2.0.8, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\CommonClass.Factory.1.2.0.8\lib\net45\CommonClass.Factory.dll</HintPath>
</Reference>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.dll</HintPath>
</Reference>
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
<Reference Include="System.Data" />
@ -147,13 +162,19 @@
<Compile Include="App_Start\BundleConfig.cs" />
<Compile Include="App_Start\FilterConfig.cs" />
<Compile Include="App_Start\RouteConfig.cs" />
<Compile Include="Bll\BackgroundTask.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Dal\DbConfig.cs" />
<Compile Include="Dal\HisFrontDbContext.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="IocFactory.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WebSettings.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="CommonClass.Factory.Log.txt" />
<Content Include="Content\bootstrap-theme.css" />
<Content Include="Content\bootstrap-theme.min.css" />
<Content Include="Content\bootstrap.css" />

View File

@ -0,0 +1,2 @@
V8 2018年1月8日
1、特性注入支持属性注入。接口注入方式暂不支持。

View File

@ -0,0 +1,12 @@
using System.Data.Entity.Migrations;
namespace CmdjyHisFront.Dal
{
public class DbConfig:DbMigrationsConfiguration<HisFrontDbContext>
{
public DbConfig() {
AutomaticMigrationsEnabled = WebSettings.AutoMigrations;
AutomaticMigrationDataLossAllowed = false;
}
}
}

View File

@ -0,0 +1,12 @@
using System.Data.Entity;
namespace CmdjyHisFront.Dal
{
/// <summary>
/// HIS前置机数据库h
/// </summary>
public partial class HisFrontDbContext:DbContext
{
public HisFrontDbContext() : base("HisFrontDbContext") { }
}
}

View File

@ -1,16 +1,23 @@
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Autofac.Integration.Mvc;
using CmdjyHisFront.Dal;
namespace CmdjyHisFront
{
public class MvcApplication:System.Web.HttpApplication
{
protected void Application_Start() {
Database.SetInitializer(new MigrateDatabaseToLatestVersion<HisFrontDbContext,DbConfig>());
//配置autofac依赖注入
DependencyResolver.SetResolver(new AutofacDependencyResolver(IocFactory.Factory));
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);

View File

@ -0,0 +1,28 @@
using Autofac;
using Autofac.Integration.Mvc;
using CommonClass.Factory;
namespace CmdjyHisFront
{
public class IocFactory
{
private static IOCFactory _iocFactory = null;
/// <summary>
/// 控制反转容器工厂
/// </summary>
public static ILifetimeScope Factory {
get {
if(_iocFactory == null) {
_iocFactory = new IOCFactory();
_iocFactory.BeforeBuild += _iocFactory_BeforeBuild;
_iocFactory.Init();
}
return _iocFactory.Container.BeginLifetimeScope();
}
}
private static void _iocFactory_BeforeBuild(IOCFactory arg1,ContainerBuilder arg2) {
arg2.RegisterControllers();
}
}
}

View File

@ -4,75 +4,89 @@
https://go.microsoft.com/fwlink/?LinkId=301880
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="HisFrontDbContext" connectionString="Server=FALCON-PC\SQLSERVER2008R2;Database=cmdjyHisFront;User ID=sa;Password=saabc123" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="3.0.0.0"/>
<add key="webpages:Enabled" value="false"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1"/>
<httpRuntime targetFramework="4.6.1"/>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f"/>
<bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2"/>
<assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
<bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.2.1" newVersion="4.0.2.1"/>
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" />
<bindingRedirect oldVersion="0.0.0.0-4.0.2.1" newVersion="4.0.2.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"/>
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0"/>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0"/>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930"/>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-5.2.4.0" newVersion="5.2.4.0"/>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.9.1.0" newVersion="4.9.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.webServer>
<modules>
<remove name="TelemetryCorrelationHttpModule"/>
<add name="TelemetryCorrelationHttpModule"
type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation"
preCondition="integratedMode,managedHandler"/>
<remove name="ApplicationInsightsWebTracking"/>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
preCondition="managedHandler"/>
<remove name="TelemetryCorrelationHttpModule" />
<add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler" />
<remove name="ApplicationInsightsWebTracking" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules>
<validation validateIntegratedModeConfiguration="false"/>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
</compilers>
</system.codedom>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>

View File

@ -0,0 +1,30 @@
using System.Configuration;
namespace CmdjyHisFront
{
/// <summary>
/// 网站配置
/// </summary>
public static class WebSettings
{
/// <summary>
/// 获取配置的值
/// </summary>
public static string GetValue(string key) {
var val = ConfigurationManager.AppSettings[key];
return val;
}
/// <summary>
/// 自动升级数据库
/// </summary>
public static bool AutoMigrations {
get {
var val = GetValue("AutoMigrations");
if(bool.TryParse(val,out bool v)) {
return v;
}
return true;
}
}
}
}

View File

@ -1,7 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Antlr" version="3.5.0.2" targetFramework="net461" />
<package id="Autofac" version="4.9.1" targetFramework="net461" />
<package id="Autofac.Mvc5" version="4.0.2" targetFramework="net461" />
<package id="bootstrap" version="3.3.7" targetFramework="net461" />
<package id="CommonClass.Factory" version="1.2.0.8" targetFramework="net461" />
<package id="EntityFramework" version="6.2.0" targetFramework="net461" />
<package id="jQuery" version="3.3.1" targetFramework="net461" />
<package id="jQuery.Validation" version="1.17.0" targetFramework="net461" />
<package id="Microsoft.ApplicationInsights" version="2.5.1" targetFramework="net461" />