From b624a8920aaa2dadea55b7dd09bcb2f8ff20f1eb Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Fri, 27 Dec 2019 11:39:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=9F=BA=E7=A1=80=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 263 ++++++++++++++++++ Falcon.DI.Test/AbstractWithDi1.cs | 14 + Falcon.DI.Test/DITest.cs | 135 +++++++++ Falcon.DI.Test/Falcon.DI.Test.csproj | 20 ++ Falcon.DI.Test/FalconDIInstantType.cs | 33 +++ Falcon.DI.Test/IMyInterface.cs | 44 +++ Falcon.DI.Test/MyClassWithDisable.cs | 20 ++ Falcon.DI.Test/NotFull.cs | 22 ++ Falcon.DI.Test/RegisterToSelf.cs | 11 + Falcon.DI.Test/SingletonTest.cs | 8 + Falcon.DI.sln | 31 +++ Falcon.DI/Falcon.DI.csproj | 20 ++ Falcon.DI/FalconDIRegisterAttribute.cs | 30 ++ Falcon.DI/IFalconDIInstantFactory.cs | 15 + Falcon.DI/IServiceCollectionExtend.cs | 75 +++++ .../PublishProfiles/FolderProfile.pubxml | 13 + Falcon.DI/ServiceLifetime.cs | 21 ++ Readme.md | 60 ++++ 18 files changed, 835 insertions(+) create mode 100644 .gitignore create mode 100644 Falcon.DI.Test/AbstractWithDi1.cs create mode 100644 Falcon.DI.Test/DITest.cs create mode 100644 Falcon.DI.Test/Falcon.DI.Test.csproj create mode 100644 Falcon.DI.Test/FalconDIInstantType.cs create mode 100644 Falcon.DI.Test/IMyInterface.cs create mode 100644 Falcon.DI.Test/MyClassWithDisable.cs create mode 100644 Falcon.DI.Test/NotFull.cs create mode 100644 Falcon.DI.Test/RegisterToSelf.cs create mode 100644 Falcon.DI.Test/SingletonTest.cs create mode 100644 Falcon.DI.sln create mode 100644 Falcon.DI/Falcon.DI.csproj create mode 100644 Falcon.DI/FalconDIRegisterAttribute.cs create mode 100644 Falcon.DI/IFalconDIInstantFactory.cs create mode 100644 Falcon.DI/IServiceCollectionExtend.cs create mode 100644 Falcon.DI/Properties/PublishProfiles/FolderProfile.pubxml create mode 100644 Falcon.DI/ServiceLifetime.cs create mode 100644 Readme.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e7dc26f --- /dev/null +++ b/.gitignore @@ -0,0 +1,263 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +zx_web/Files/errs/ + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +project.fragment.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +#*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc \ No newline at end of file diff --git a/Falcon.DI.Test/AbstractWithDi1.cs b/Falcon.DI.Test/AbstractWithDi1.cs new file mode 100644 index 0000000..04f09fa --- /dev/null +++ b/Falcon.DI.Test/AbstractWithDi1.cs @@ -0,0 +1,14 @@ +锘縰sing System; +using System.Collections.Generic; +using System.Text; + +namespace Falcon.DI.Test +{ + [FalconDIRegister] + public abstract class AbstractWithDi + { + public AbstractWithDi() { } + } + + public class ClassFromAbstractWithDi:AbstractWithDi { } +} diff --git a/Falcon.DI.Test/DITest.cs b/Falcon.DI.Test/DITest.cs new file mode 100644 index 0000000..3742bc6 --- /dev/null +++ b/Falcon.DI.Test/DITest.cs @@ -0,0 +1,135 @@ +using System.Linq; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Falcon.DI.Test +{ + [TestClass] + public class UseFalconDITest + { + /// + /// 一般获取服务测试 + /// + [TestMethod] + public void DITest() { + IServiceCollection ser = new ServiceCollection(); + ser.UseFalconDI(this.GetType().Assembly); + using(var pd = ser.BuildServiceProvider()) { + Assert.IsNotNull(pd.GetServices()); + System.Console.WriteLine(pd.GetServices().Count()); + Assert.IsTrue(pd.GetServices().Count() > 1); + Assert.IsNotNull(pd.GetService()); + } + } + + /// + /// 测试释放资源 + /// + [TestMethod] + public void disableTest() { + IServiceCollection ser = new ServiceCollection(); + ser.UseFalconDI(this.GetType().Assembly); + var pd = ser.BuildServiceProvider(); + + var obj = pd.GetService(); + Assert.IsNotNull(obj); + Assert.AreEqual(1,MyClassWithDisable.Count); + + pd.Dispose(); + Assert.AreEqual(0,MyClassWithDisable.Count); + + using(pd = ser.BuildServiceProvider()) { + obj = pd.GetService(); + Assert.IsNotNull(obj); + Assert.AreEqual(1,MyClassWithDisable.Count); + } + Assert.AreEqual(0,MyClassWithDisable.Count); + } + + [TestMethod] + [Description("测试不提供完整构造注入参数")] + public void NotFullTest() { + IServiceCollection ser = new ServiceCollection(); + ser.UseFalconDI(this.GetType().Assembly); + var pd = ser.BuildServiceProvider(); + + var obj = pd.GetService(); + Assert.IsNotNull(obj); + Assert.IsNotNull(obj.F1); + Assert.IsNull(obj.F2); + } + + /// + /// 测试服务工厂 + /// + [TestMethod] + public void IFalconDIFactoryTest() { + IServiceCollection ser = new ServiceCollection(); + ser.UseFalconDI(this.GetType().Assembly); + using(var pd = ser.BuildServiceProvider()) { + var obj = pd.GetService(); + Assert.IsNotNull(obj); + Assert.AreEqual(obj.Instance().Val,2); + + var obj2 = pd.GetService>(); + Assert.IsNotNull(obj2); + Assert.AreEqual(obj2.Instance().Val,2); + + } + } + + /// + /// 测试注册到自身 + /// + [TestMethod] + public void RegisterToSelf() { + IServiceCollection ser = new ServiceCollection(); + ser.UseFalconDI(this.GetType().Assembly); + using(var pd = ser.BuildServiceProvider()) { + var obj = pd.GetService(); + Assert.IsNotNull(obj); + } + } + + /// + /// 测试单例 + /// + [TestMethod] + public void SingletonTest() { + IServiceCollection ser = new ServiceCollection(); + ser.UseFalconDI(this.GetType().Assembly); + //单例。两次获取为同一对象 + using(var pd = ser.BuildServiceProvider()) { + var obj = pd.GetService(); + Assert.IsTrue(obj.Val == 0); + obj.Val = 1; + var obj2 = pd.GetService(); + Assert.IsTrue(obj.Val == 1); + } + //释放Provider后,释放单例对象 + using(var pd = ser.BuildServiceProvider()) { + var obj = pd.GetService(); + Assert.IsTrue(obj.Val == 0); + obj.Val = 1; + var obj2 = pd.GetService(); + Assert.IsTrue(obj.Val == 1); + } + } + + /// + /// 测试虚类不能注册 + /// + [TestMethod] + public void AbstractTest() { + IServiceCollection ser = new ServiceCollection(); + ser.UseFalconDI(this.GetType().Assembly); + using(var pd = ser.BuildServiceProvider()) { + var obj = pd.GetService(); + System.Console.WriteLine(obj.GetType().FullName); + Assert.IsNotNull(obj); + var cla = obj as ClassFromAbstractWithDi; + Assert.IsNotNull(cla); + } + } + } +} diff --git a/Falcon.DI.Test/Falcon.DI.Test.csproj b/Falcon.DI.Test/Falcon.DI.Test.csproj new file mode 100644 index 0000000..4cba7e1 --- /dev/null +++ b/Falcon.DI.Test/Falcon.DI.Test.csproj @@ -0,0 +1,20 @@ +锘 + + + netcoreapp3.1;netcoreapp3.0 + + false + + + + + + + + + + + + + + diff --git a/Falcon.DI.Test/FalconDIInstantType.cs b/Falcon.DI.Test/FalconDIInstantType.cs new file mode 100644 index 0000000..fc5d6db --- /dev/null +++ b/Falcon.DI.Test/FalconDIInstantType.cs @@ -0,0 +1,33 @@ +锘縰sing System; + +namespace Falcon.DI.Test +{ + public interface IFDIIT + { + int Val { get; set; } + } + + [FalconDIRegister] + public class FalconDIInstantType:IFDIIT + { + public int Val { get; set; } = 1; + } + + + public interface IFalconDIInstantTypeFactory:IFalconDIInstantFactory { } + + [FalconDIRegister] + public class FalconDIInstantTypeFactory:IFalconDIInstantTypeFactory, IFalconDIInstantFactory + { + public IServiceProvider Provider { get; set; } + + public FalconDIInstantTypeFactory(IServiceProvider sp = null) { + this.Provider = sp; + } + + public IFDIIT Instance() { + return this.Provider == null ? new FalconDIInstantType { Val = 3 } : + new FalconDIInstantType { Val = 2 }; + } + } +} diff --git a/Falcon.DI.Test/IMyInterface.cs b/Falcon.DI.Test/IMyInterface.cs new file mode 100644 index 0000000..8b599e4 --- /dev/null +++ b/Falcon.DI.Test/IMyInterface.cs @@ -0,0 +1,44 @@ +锘縩amespace Falcon.DI.Test +{ + public interface IMyInterface + { + string Getname(); + } + + public interface IMyInterface2 + { + string Getname(); + } + + [FalconDIRegister(typeof(IMyInterface))] + public class MyClassInterface:IMyInterface + { + public string Getname() { + return this.GetType().Name; + } + } + + [FalconDIRegister] + public class MyClassInterfaces:IMyInterface, IMyInterface2 + { + public string Getname() { + return this.GetType().Name; + } + } + + [FalconDIRegister] + public class MyClassDefault:IMyInterface + { + public string Getname() { + return this.GetType().Name; + } + } + + [FalconDIRegister(typeof(MyClassSelf))] + public class MyClassSelf:IMyInterface + { + public string Getname() { + return this.GetType().Name; + } + } +} diff --git a/Falcon.DI.Test/MyClassWithDisable.cs b/Falcon.DI.Test/MyClassWithDisable.cs new file mode 100644 index 0000000..58e447f --- /dev/null +++ b/Falcon.DI.Test/MyClassWithDisable.cs @@ -0,0 +1,20 @@ +锘 +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.Extensions.DependencyInjection; + +namespace Falcon.DI.Test +{ + [FalconDIRegister(typeof(MyClassWithDisable),Lifetime =ServiceLifetime.Transient)] + public class MyClassWithDisable:IDisposable + { + public static int Count { get; set; } = 0; + + public MyClassWithDisable() => Count += 1; + + public void Dispose() { + Count -= 1; + } + } +} diff --git a/Falcon.DI.Test/NotFull.cs b/Falcon.DI.Test/NotFull.cs new file mode 100644 index 0000000..3c98c28 --- /dev/null +++ b/Falcon.DI.Test/NotFull.cs @@ -0,0 +1,22 @@ +锘縩amespace Falcon.DI.Test +{ + public interface INfi1 { } + public interface INfi2 { } + + [FalconDIRegister] + public class NotFull:INfi1 + { + } + + [FalconDIRegister(typeof(NotFullObj))] + public class NotFullObj + { + public INfi1 F1 { get; set; } + public INfi2 F2 { get; set; } + + public NotFullObj(INfi1 f1,INfi2 f2=null) { + this.F1 = f1; + this.F2 = f2; + } + } +} diff --git a/Falcon.DI.Test/RegisterToSelf.cs b/Falcon.DI.Test/RegisterToSelf.cs new file mode 100644 index 0000000..99826af --- /dev/null +++ b/Falcon.DI.Test/RegisterToSelf.cs @@ -0,0 +1,11 @@ +锘縰sing System; +using System.Collections.Generic; +using System.Text; + +namespace Falcon.DI.Test +{ + [FalconDIRegister] + public class RegisterToSelf + { + } +} diff --git a/Falcon.DI.Test/SingletonTest.cs b/Falcon.DI.Test/SingletonTest.cs new file mode 100644 index 0000000..e3de758 --- /dev/null +++ b/Falcon.DI.Test/SingletonTest.cs @@ -0,0 +1,8 @@ +锘縩amespace Falcon.DI.Test +{ + [FalconDIRegister(Lifetime = ServiceLifetime.Singleton)] + class SingletonTest + { + public int Val { get; set; } = 0; + } +} diff --git a/Falcon.DI.sln b/Falcon.DI.sln new file mode 100644 index 0000000..e49a315 --- /dev/null +++ b/Falcon.DI.sln @@ -0,0 +1,31 @@ +锘 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29613.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Falcon.DI", "Falcon.DI\Falcon.DI.csproj", "{896E75AF-6E5D-4FBE-954A-901743A0CBEF}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Falcon.DI.Test", "Falcon.DI.Test\Falcon.DI.Test.csproj", "{09D91A95-46AB-4461-9486-E45530FB9CA8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {896E75AF-6E5D-4FBE-954A-901743A0CBEF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {896E75AF-6E5D-4FBE-954A-901743A0CBEF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {896E75AF-6E5D-4FBE-954A-901743A0CBEF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {896E75AF-6E5D-4FBE-954A-901743A0CBEF}.Release|Any CPU.Build.0 = Release|Any CPU + {09D91A95-46AB-4461-9486-E45530FB9CA8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {09D91A95-46AB-4461-9486-E45530FB9CA8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {09D91A95-46AB-4461-9486-E45530FB9CA8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {09D91A95-46AB-4461-9486-E45530FB9CA8}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B6456D19-8A99-411A-AC5C-FF0B15873EEB} + EndGlobalSection +EndGlobal diff --git a/Falcon.DI/Falcon.DI.csproj b/Falcon.DI/Falcon.DI.csproj new file mode 100644 index 0000000..8434992 --- /dev/null +++ b/Falcon.DI/Falcon.DI.csproj @@ -0,0 +1,20 @@ +锘 + + + NET461;netstandard2.0;netstandard2.1 + true + 瀵.NET CORE杩涜鎵╁睍锛屽姞鍏alconDIRegisterAttribute鐗规ц繘琛屾湇鍔℃敞鍐屻 + 1.0.2 + false + Falcon + Falcon + bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml + http://39.105.71.191/Falcon/Falcon.Di + http://39.105.71.191/Falcon/Falcon.Di + + + + + + + diff --git a/Falcon.DI/FalconDIRegisterAttribute.cs b/Falcon.DI/FalconDIRegisterAttribute.cs new file mode 100644 index 0000000..5b9c130 --- /dev/null +++ b/Falcon.DI/FalconDIRegisterAttribute.cs @@ -0,0 +1,30 @@ +锘縰sing System; + +namespace Falcon.DI +{ + /// + /// 鑷姩娉ㄥ唽鏈嶅姟鐗规с傚鏋滆娉ㄥ唽鍒扮壒瀹氭湇鍔″彲浠ユ寚瀹氾紝鍚﹀垯娉ㄥ唽鍒版墍鏈夊疄鐜扮殑鎺ュ彛鍜屽熀绫伙紝濡傛灉鏈疄鐜颁换浣曟帴鍙f敞鍐屽埌绫诲瀷鏈韩銆 + /// + [AttributeUsage(AttributeTargets.Class)] + public class FalconDIRegisterAttribute:Attribute + { + /// + /// 榛樿娉ㄥ唽鍒版墍鏈夊疄鐜扮殑鍩虹鎺ュ彛 + /// + public FalconDIRegisterAttribute() { } + /// + /// 娉ㄥ唽鍒版彁渚涚殑鏈嶅姟绫诲瀷 + /// + /// 鏈嶅姟绫诲瀷 + public FalconDIRegisterAttribute(params Type[] type) => this.ServiceTypes = type; + + /// + /// 娉ㄥ唽鐨勬湇鍔$被鍨嬮泦鍚 + /// + public Type[] ServiceTypes { get; set; } = null; + /// + /// 鐢熷懡鍛ㄦ湡 + /// + public ServiceLifetime Lifetime { get; set; } = ServiceLifetime.Scoped; + } +} diff --git a/Falcon.DI/IFalconDIInstantFactory.cs b/Falcon.DI/IFalconDIInstantFactory.cs new file mode 100644 index 0000000..11e7b09 --- /dev/null +++ b/Falcon.DI/IFalconDIInstantFactory.cs @@ -0,0 +1,15 @@ +锘縰sing System; + +namespace Falcon.DI +{ + /// + /// 渚濊禆娉ㄥ叆瀹炰緥鍖栧伐鍘 + /// + public interface IFalconDIInstantFactory + { + /// + /// 閫氳繃缁欏畾瀹瑰櫒瀹炰緥鍖栧璞 + /// + T Instance(); + } +} diff --git a/Falcon.DI/IServiceCollectionExtend.cs b/Falcon.DI/IServiceCollectionExtend.cs new file mode 100644 index 0000000..d5cae79 --- /dev/null +++ b/Falcon.DI/IServiceCollectionExtend.cs @@ -0,0 +1,75 @@ +锘縰sing System; +using System.Reflection; +using Microsoft.Extensions.DependencyInjection; +using msdi = Microsoft.Extensions.DependencyInjection; +using System.Linq; +using System.Collections.Generic; + +namespace Falcon.DI +{ + /// + /// 鏈嶅姟闆嗗悎鏂规硶鎵╁睍 + /// + public static class IServiceCollectionExtend + { + /// + /// 瀹炵幇鑷姩DI娉ㄥ唽 + /// + /// 鏈嶅姟闆嗗悎 + /// 瑕佹敞鍐岀殑绋嬪簭闆嗛泦鍚堬紝濡傛灉鏈寚瀹氬垯涓篊urrentDomain鎵鏈夊紩鍏ョ殑绋嬪簭闆 + public static IServiceCollection UseFalconDI(this IServiceCollection services,params Assembly[] assemblies) { + if(assemblies == null || assemblies.Length == 0) { + return services.UseFalconDI(AppDomain.CurrentDomain.GetAssemblies()); + } + foreach(Assembly ass in assemblies) { + foreach(Type type in ass.GetTypes()) { + if(!type.CanInstance()) { + continue; + } + var ra = type.GetCustomAttribute(true); + if(ra != null) { + //濡傛灉鏈彁渚涙湇鍔$被鍨嬶紝娉ㄥ唽鍒版墍鏈夊疄鐜扮殑鎺ュ彛鍜屽熀绫 + if(ra.ServiceTypes == null || ra.ServiceTypes.Length == 0) { + var sts = new List(); + //娣诲姞鎵鏈夊疄鐜扮殑鎺ュ彛 + sts.AddRange(type.GetInterfaces()); + //娣诲姞闈瀘bject鍩虹被 + var baseType = type.BaseType; + if(baseType != typeof(object)) { + sts.Add(baseType); + } + //娣诲姞绫诲瀷鏈韩 + sts.Add(type); + + ra.ServiceTypes = sts.ToArray(); + } + foreach(var ser in ra.ServiceTypes) { + services.Add(new ServiceDescriptor(ser,type,convertLifetime(ra.Lifetime))); + } + } + } + } + return services; + } + /// + /// 杞崲鐢熷瓨鏈熸灇涓 + /// + /// 鐢熷瓨鏈 + /// + private static msdi.ServiceLifetime convertLifetime(ServiceLifetime lt) { + return (msdi.ServiceLifetime)(int)lt; + } + + /// + /// 鑾峰彇绫诲瀷鏄惁鍙互瀹炰緥鍖栥 + /// + /// 绫诲瀷 + /// 鏄惁鍙疄渚嬪寲 + public static bool CanInstance(this Type type) { + if(!type.IsClass || type.IsAbstract) { + return false; + } + return true; + } + } +} diff --git a/Falcon.DI/Properties/PublishProfiles/FolderProfile.pubxml b/Falcon.DI/Properties/PublishProfiles/FolderProfile.pubxml new file mode 100644 index 0000000..91fcb8f --- /dev/null +++ b/Falcon.DI/Properties/PublishProfiles/FolderProfile.pubxml @@ -0,0 +1,13 @@ +锘 + + + + FileSystem + Release + Any CPU + netcoreapp2.2 + bin\Debug\netcoreapp2.2\publish\ + + \ No newline at end of file diff --git a/Falcon.DI/ServiceLifetime.cs b/Falcon.DI/ServiceLifetime.cs new file mode 100644 index 0000000..0e8cf4d --- /dev/null +++ b/Falcon.DI/ServiceLifetime.cs @@ -0,0 +1,21 @@ +锘縩amespace Falcon.DI +{ + /// + /// 鏈嶅姟鐢熷瓨鏈 + /// + public enum ServiceLifetime + { + /// + /// 鍗曚緥 + /// + Singleton = 0, + /// + /// 姣忎釜Scoped鍐呭崟渚 + /// + Scoped = 1, + /// + /// 鐬椂锛屾瘡娆¤繑鍥炰笉鍚岀殑瀵硅薄 + /// + Transient = 2 + } +} diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..b0df53f --- /dev/null +++ b/Readme.md @@ -0,0 +1,60 @@ +锘**浣跨敤璇存槑** + +1. 瀹夎锛氬彲浠ラ氳繃涓嬭浇婧愮爜瀹夎锛屼篃鍙互閫氳繃nuget鍖呭畨瑁呫 +1. 寮曞叆鍚嶅瓧绌洪棿 +~~~ +using Microsoft.Extensions.DependencyInjection; +using Falcon.DI +~~~ +1. 鍒濆鍖栧鍣細浣跨敤UseFalconDI鏂规硶娉ㄥ唽鎵鏈夋湇鍔° +~~~ +IServiceCollection ser = new ServiceCollection(); +ser.UseFalconDI(); +~~~ +1. 娣诲姞娉ㄥ唽鐗规э紝鏈嶅姟鍙互娉ㄥ唽鍒板熀纭鎺ュ彛鎴栬嚜韬 +~~~ +public interface IMyInterface +{ + string Getname(); +} + +[FalconDIRegister] +public class MyClassInterfaces:IMyInterface +{ + public string Getname() { + return this.GetType().Name; + } +} +~~~ +1. 娉ㄥ叆鍙互浣跨敤ServiceCollection鑾峰彇娉ㄥ唽鐨勬湇鍔° + ~~~ + using(var pd = ser.BuildServiceProvider()) { + var service = pd.GetServices(); + // Do something + } + + ~~~ + 鎴栬呬娇鐢ㄦ瀯閫犳敞鍏 + ~~~ + public interface INfi1 { } + public interface INfi2 { } + + [FalconDIRegister] + public class NotFull:INfi1 + { + } + + [FalconDIRegister(typeof(NotFullObj))] + public class NotFullObj + { + public INfi1 F1 { get; set; } + public INfi2 F2 { get; set; } + + public NotFullObj(INfi1 f1,INfi2 f2=null) { + this.F1 = f1; + this.F2 = f2; + } + } + ~~~ +浠ヤ笂渚嬪瓙涓鍏堟敞鍏ヤ簡NotFull绫诲瀷瀹炵幇INfi1鏈嶅姟锛岀劧鍚庡張娉ㄥ唽浜哊otFullObj绫诲瀷锛屽苟閫氳繃鏋勯犳敞鍏ユ柟寮忔秷璐逛簡INfi1鏈嶅姟銆侷Nfi2鍥犱负娌℃湁娉ㄥ唽鎵浠ヤ娇鐢ㄩ粯璁ゅ艰烦杩囨敞鍏ャ +