Skip to content

Commit

Permalink
Service extension method: AddAssembly()
Browse files Browse the repository at this point in the history
  • Loading branch information
Wayne-KTCSZ committed Aug 4, 2023
1 parent 7bdc50e commit 821b4e9
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 234 deletions.
234 changes: 0 additions & 234 deletions src/EasyFrameWork/.gitignore

This file was deleted.

6 changes: 6 additions & 0 deletions src/EasyFrameWork/Cache/CacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public CacheManager(IMemoryCache memoryCache, ISignals signals)
_signals = signals;
}

public T Get<T>(string key)
{
return _memoryCache.Get<T>(CreateCacheKey(key));
}

public T GetOrCreate<T>(string key, Func<ICacheEntry, T> factory)
{
return _memoryCache.GetOrCreate<T>(CreateCacheKey(key), cacheEntry =>
Expand Down Expand Up @@ -55,5 +60,6 @@ public void RemoveAll()
{
_signals.Trigger(Signals.AllCacheEntryChanged);
}

}
}
1 change: 1 addition & 0 deletions src/EasyFrameWork/Cache/ICacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Easy.Cache
{
public interface ICacheManager<out Category>
{
T Get<T>(string key);
T GetOrCreate<T>(string key, Func<ICacheEntry, T> factory);
Task<T> GetOrCreateAsync<T>(string key, Func<ICacheEntry, Task<T>> factory);
void Remove(string key);
Expand Down
40 changes: 40 additions & 0 deletions src/EasyFrameWork/Extend/ExtServiceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
* http://www.zkea.net/licenses */

using Easy.Cache;
using Easy.IoC;
using Easy.Mvc.StateProviders;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Easy.Extend
Expand All @@ -20,5 +23,42 @@ public static IServiceCollection ConfigureStateProvider<T>(this IServiceCollecti
services.AddScoped<T>();
return services;
}

public static void AddAssembly(this IServiceCollection services, Assembly assembly)
{
var allPublicType = assembly.DefinedTypes.Where(m => !m.IsAbstract && !m.IsInterface && m.IsPublic && m.IsVisible);

foreach (var type in allPublicType)
{
var interfaces = type.GetInterfaces();
if (!interfaces.Any()) continue;

if (Dependency.Transient.IsAssignableFrom(type))
{
foreach (var iType in interfaces.Where(IsNotDependencyInterface))
{
services.AddTransient(iType, type);
}
}
else if (Dependency.Scoped.IsAssignableFrom(type))
{
foreach (var iType in interfaces.Where(IsNotDependencyInterface))
{
services.AddScoped(iType, type);
}
}
else if (Dependency.Singleton.IsAssignableFrom(type))
{
foreach (var iType in interfaces.Where(IsNotDependencyInterface))
{
services.AddSingleton(iType, type);
}
}
}
}
private static bool IsNotDependencyInterface(Type type)
{
return type != Dependency.Transient && type != Dependency.Scoped && type != Dependency.Singleton;
}
}
}
26 changes: 26 additions & 0 deletions src/EasyFrameWork/IoC/Dependency.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Easy.IoC
{
public class Dependency
{
public static Type Transient = typeof(ITransient);
public static Type Scoped = typeof(IScoped);
public static Type Singleton = typeof(ISingleton);
}

public interface ITransient
{
}
public interface IScoped
{
}
public interface ISingleton
{
}

}

0 comments on commit 821b4e9

Please sign in to comment.