190 lines
6.9 KiB
C#
190 lines
6.9 KiB
C#
using System;
|
|
using Kreta.Core.ConnectionType;
|
|
using Kreta.DataAccessManual.Interfaces;
|
|
using Kreta.Framework;
|
|
|
|
namespace Kreta.DataAccessManual.Util
|
|
{
|
|
internal abstract class ConnectionHandlerBase
|
|
{
|
|
internal void ActionWithHandler(Action<IDalHandler> actions, IDalHandler handler = null)
|
|
{
|
|
try
|
|
{
|
|
var isNotNestedConnection = handler == null;
|
|
handler = handler ?? new DalHandler();
|
|
actions(handler);
|
|
if (isNotNestedConnection && handler.CommitOnEnd)
|
|
{
|
|
UserContext.Instance.CommitTransaction();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
UserContext.Instance.RollbackTransaction();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
internal T FunctionWithHandler<T>(Func<IDalHandler, T> actions, IDalHandler handler = null)
|
|
{
|
|
try
|
|
{
|
|
var isNotNestedConnection = handler == null;
|
|
handler = handler ?? new DalHandler();
|
|
var result = actions(handler);
|
|
if (isNotNestedConnection && handler.CommitOnEnd)
|
|
{
|
|
UserContext.Instance.CommitTransaction();
|
|
}
|
|
return result;
|
|
}
|
|
catch
|
|
{
|
|
UserContext.Instance.RollbackTransaction();
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
|
|
internal class ConnectionHandler : ConnectionHandlerBase, IConnectionHandler
|
|
{
|
|
public void Run(string sessionId, Action<IDalHandler> actions)
|
|
{
|
|
using (new Connection(sessionId))
|
|
{
|
|
ActionWithHandler(actions);
|
|
}
|
|
}
|
|
|
|
public T Run<T>(string sessionId, Func<IDalHandler, T> actions)
|
|
{
|
|
using (new Connection(sessionId))
|
|
{
|
|
return FunctionWithHandler(actions);
|
|
}
|
|
}
|
|
}
|
|
|
|
internal class OrganizationConnectionHandler : ConnectionHandlerBase, IOrganizationConnectionHandler
|
|
{
|
|
public void Run(string intezmenyAzonosito, Action<IDalHandler> actions)
|
|
{
|
|
using (new OrganizationConnection(intezmenyAzonosito))
|
|
{
|
|
ActionWithHandler(actions);
|
|
}
|
|
}
|
|
|
|
public T Run<T>(string intezmenyAzonosito, Func<IDalHandler, T> actions)
|
|
{
|
|
using (new OrganizationConnection(intezmenyAzonosito))
|
|
{
|
|
return FunctionWithHandler(actions);
|
|
}
|
|
}
|
|
}
|
|
|
|
internal class MobileConnectionHandler : ConnectionHandlerBase, IMobileConnectionHandler
|
|
{
|
|
public void Run(string intezmenyAzonosito, int? tanevId, Action<IDalHandler> actions, int? felhasznaloId = null)
|
|
{
|
|
using (new MobileConnection(intezmenyAzonosito, tanevId, felhasznaloId))
|
|
{
|
|
ActionWithHandler(actions);
|
|
}
|
|
}
|
|
|
|
public T Run<T>(string intezmenyAzonosito, int? tanevId, Func<IDalHandler, T> actions, int? felhasznaloId = null)
|
|
{
|
|
using (new MobileConnection(intezmenyAzonosito, tanevId, felhasznaloId))
|
|
{
|
|
return FunctionWithHandler(actions);
|
|
}
|
|
}
|
|
}
|
|
|
|
internal class SystemConnectionHandler : ConnectionHandlerBase, ISystemConnectionHandler
|
|
{
|
|
public void Run(Action<IDalHandler> actions)
|
|
{
|
|
using (new SystemConnection())
|
|
{
|
|
ActionWithHandler(actions);
|
|
}
|
|
}
|
|
|
|
public T Run<T>(Func<IDalHandler, T> actions)
|
|
{
|
|
using (new SystemConnection())
|
|
{
|
|
return FunctionWithHandler(actions);
|
|
}
|
|
}
|
|
}
|
|
|
|
internal class ServiceSystemConnectionHandler : ConnectionHandlerBase, IServiceSystemConnectionHandler
|
|
{
|
|
public void Run(string connectionString, Action<IDalHandler> actions)
|
|
{
|
|
using (new ServiceSystemConnection(connectionString))
|
|
{
|
|
ActionWithHandler(actions);
|
|
}
|
|
}
|
|
|
|
public T Run<T>(string connectionString, Func<IDalHandler, T> actions)
|
|
{
|
|
using (new ServiceSystemConnection(connectionString))
|
|
{
|
|
return FunctionWithHandler(actions);
|
|
}
|
|
}
|
|
}
|
|
|
|
internal class CustomConnectionHandler : ConnectionHandlerBase, ICustomConnectionHandler
|
|
{
|
|
public void Run(IConnectionType connectionType, Action<IDalHandler> actions)
|
|
{
|
|
switch (connectionType)
|
|
{
|
|
case DalHandlerConnectionType dalHandlerConnectionType:
|
|
ActionWithHandler(actions, dalHandlerConnectionType.DalHandler);
|
|
break;
|
|
case MobileConnectionType mobileConnectionType:
|
|
new MobileConnectionHandler().Run(mobileConnectionType.IntezmenyAzonosito, mobileConnectionType.TanevId, actions, mobileConnectionType.FelhasznaloId);
|
|
break;
|
|
case OrganizationConnectionType organizationConnectionType:
|
|
new OrganizationConnectionHandler().Run(organizationConnectionType.IntezmenyAzonosito, actions);
|
|
break;
|
|
case SessionConnectionType sessionConnectionType:
|
|
new ConnectionHandler().Run(sessionConnectionType.SessionId, actions);
|
|
break;
|
|
case SystemConnectionType systemConnectionType:
|
|
new SystemConnectionHandler().Run(actions);
|
|
break;
|
|
default:
|
|
throw new ArgumentException("Ismeretlen ConnectionTpye", nameof(connectionType));
|
|
}
|
|
}
|
|
|
|
public T Run<T>(IConnectionType connectionType, Func<IDalHandler, T> actions)
|
|
{
|
|
switch (connectionType)
|
|
{
|
|
case DalHandlerConnectionType dalHandlerConnectionType:
|
|
return FunctionWithHandler(actions, dalHandlerConnectionType.DalHandler);
|
|
case MobileConnectionType mobileConnectionType:
|
|
return new MobileConnectionHandler().Run(mobileConnectionType.IntezmenyAzonosito, mobileConnectionType.TanevId, actions, mobileConnectionType.FelhasznaloId);
|
|
case OrganizationConnectionType organizationConnectionType:
|
|
return new OrganizationConnectionHandler().Run(organizationConnectionType.IntezmenyAzonosito, actions);
|
|
case SessionConnectionType sessionConnectionType:
|
|
return new ConnectionHandler().Run(sessionConnectionType.SessionId, actions);
|
|
case SystemConnectionType systemConnectionType:
|
|
return new SystemConnectionHandler().Run(actions);
|
|
default:
|
|
throw new ArgumentException("Ismeretlen ConnectionTpye", nameof(connectionType));
|
|
}
|
|
}
|
|
}
|
|
}
|