Skip to content

Commit

Permalink
Remove SubscriptionService
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofPajak committed Feb 19, 2019
1 parent d7ddde4 commit 9f0a56d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 100 deletions.
1 change: 0 additions & 1 deletion Grand.Framework/Infrastructure/DependencyRegistrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder, G
builder.RegisterType<ResourceManager>().As<IResourceManager>().InstancePerLifetimeScope();

builder.RegisterType<EventPublisher>().As<IEventPublisher>().SingleInstance();
builder.RegisterType<SubscriptionService>().As<ISubscriptionService>().SingleInstance();

//TASKS
builder.RegisterType<QueuedMessagesSendScheduleTask>().InstancePerLifetimeScope();
Expand Down
75 changes: 15 additions & 60 deletions Grand.Services/Events/EventPublisher.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Grand.Core.Infrastructure;
using Grand.Core.Plugins;
using Grand.Services.Logging;
using System;
using System.Linq;
using System.Reflection;

namespace Grand.Services.Events
{
Expand All @@ -12,85 +10,42 @@ namespace Grand.Services.Events
/// </summary>
public class EventPublisher : IEventPublisher
{
private readonly ISubscriptionService _subscriptionService;

/// <summary>
/// Ctor
/// </summary>
/// <param name="subscriptionService"></param>
public EventPublisher(ISubscriptionService subscriptionService)
public EventPublisher()
{
_subscriptionService = subscriptionService;
}
#region Methods

/// <summary>
/// Publish to cunsumer
/// Publish event to consumers
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="x">Event consumer</param>
/// <param name="eventMessage">Event message</param>
protected virtual void PublishToConsumer<T>(IConsumer<T> x, T eventMessage)
public virtual void Publish<T>(T eventMessaget)
{
//Ignore not installed plugins
var plugin = FindPlugin(x.GetType());
if (plugin != null && !plugin.Installed)
return;
//get all event consumers
var consumers = EngineContext.Current.ResolveAll<IConsumer<T>>().ToList();

try
{
x.HandleEvent(eventMessage);
}
catch (Exception exc)
foreach (var consumer in consumers)
{
//log error
var logger = EngineContext.Current.Resolve<ILogger>();
//we put in to nested try-catch to prevent possible cyclic (if some error occurs)
try
{
logger.Error(exc.Message, exc);
consumer.HandleEvent(eventMessaget);
}
catch (Exception)
catch (Exception exception)
{
//do nothing
try
{
EngineContext.Current.Resolve<ILogger>()?.Error(exception.Message, exception);
}
catch { }
}
}
}

/// <summary>
/// Find a plugin descriptor by some type which is located into its assembly
/// </summary>
/// <param name="providerType">Provider type</param>
/// <returns>Plugin descriptor</returns>
protected virtual PluginDescriptor FindPlugin(Type providerType)
{
if (providerType == null)
throw new ArgumentNullException("providerType");

if (PluginManager.ReferencedPlugins == null)
return null;

foreach (var plugin in PluginManager.ReferencedPlugins)
{
if (plugin.ReferencedAssembly == null)
continue;

if (plugin.ReferencedAssembly.FullName == providerType.GetTypeInfo().Assembly.FullName)
return plugin;
}

return null;
}

/// <summary>
/// Publish event
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="eventMessage">Event message</param>
public virtual void Publish<T>(T eventMessage)
{
var subscriptions = _subscriptionService.GetSubscriptions<T>();
subscriptions.ToList().ForEach(x => PublishToConsumer(x, eventMessage));
}
#endregion

}
}
17 changes: 0 additions & 17 deletions Grand.Services/Events/ISubscriptionService.cs

This file was deleted.

22 changes: 0 additions & 22 deletions Grand.Services/Events/SubscriptionService.cs

This file was deleted.

0 comments on commit 9f0a56d

Please sign in to comment.