Merge pull request 'change - 修改注入方式,实现Repository层接口实现自动注入' (#1) from master into dev

Reviewed-on: #1
pull/2/head
wenjy 8 months ago
commit 487e8e7cba

@ -0,0 +1,13 @@
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/modules.xml
/projectSettingsUpdater.xml
/.idea.SlnMesnac.iml
/contentModel.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise">
<file url="file://$PROJECT_DIR$/SlnMesnac/Program.cs" charset="GBK" />
<file url="PROJECT" charset="GBK" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

Binary file not shown.

@ -45,17 +45,5 @@ namespace SlnMesnac.Repository
return Db;
});
}
/// <summary>
/// 注册Repository、Service服务
/// </summary>
/// <param name="services"></param>
public static void AddServices(this IServiceCollection services)
{
//services.AddSingleton<Repository<BaseUser>>();
services.AddSingleton(typeof(Repository<>));
services.AddSingleton<IBaseUserService, BaseUserServiceImpl>();
services.AddSingleton<IBaseMaterialService, BaseMaterialServiceImpl>();
}
}
}

@ -12,6 +12,11 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Autofac.Extensions.DependencyInjection;
using SlnMesnac.Common;
using SlnMesnac.Repository.service;
using SlnMesnac.Repository.service.Impl;
using SlnMesnac.Plc.Factory;
namespace SlnMesnac.WPF
{
@ -41,13 +46,13 @@ namespace SlnMesnac.WPF
var host = CreateHostBuilder(e.Args).Build();//生成宿主。
ServiceProvider = host.Services;
await host.StartAsync();
//var baseMaterialService = ServiceProvider.GetRequiredService<MelsecBinaryFactory>();
var appConfig = host.Services.GetService<AppConfig>();
var logPath = $"{appConfig.logPath}/Logs/{DateTime.UtcNow:yyyy-MM-dd}/";
Log.Information($"系统初始化完成,日志存放路径:{appConfig.logPath}");
await host.StartAsync();
}
/// <summary>
@ -58,6 +63,7 @@ namespace SlnMesnac.WPF
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();

@ -37,6 +37,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Lierda.WPFHelper" Version="1.0.3" />
<PackageReference Include="MvvmLightLibs" Version="5.4.1.1" />
</ItemGroup>

@ -11,8 +11,10 @@ using SlnMesnac.Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using Microsoft.Extensions.Configuration;
using SlnMesnac.Mqtt;
using SlnMesnac.Rfid;
@ -48,9 +50,6 @@ namespace SlnMesnac.WPF
//注册ORM
services.AddSqlSugarSetup();
//注册Service
services.AddServices();
//注册Quartz
//services.AddQuartzSetUp();
@ -66,6 +65,30 @@ namespace SlnMesnac.WPF
//注册RFID
services.AddRfidSetup();
}
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(Repository<>));
RegisterImplementations(builder, typeof(Repository<>).Assembly);
}
private static void RegisterImplementations(ContainerBuilder builder, Assembly assembly)
{
//自动注入仓储层的接口实现类
var types = assembly.GetTypes()
.Where(t => t.IsClass && !t.IsAbstract && !t.IsGenericType)
.ToList();
foreach (var type in types)
{
var interfaces = type.GetInterfaces();
foreach (var @interface in interfaces)
{
builder.RegisterType(type).As(@interface);
}
}
}
/// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

@ -79,7 +79,7 @@ namespace SlnMesnac
services.AddSqlSugarSetup();
//注册服务
services.AddServices();
//services.AddServices();
//注册任务调度
services.AddQuartzSetUp();

Loading…
Cancel
Save