Browse Source

optimizing publisher interface

master
Savorboard 7 years ago
parent
commit
d97f1f7f5a
3 changed files with 25 additions and 30 deletions
  1. +19
    -20
      src/DotNetCore.CAP/Abstractions/CapPublisherBase.cs
  2. +4
    -8
      src/DotNetCore.CAP/ICapPublisher.cs
  3. +2
    -2
      test/DotNetCore.CAP.Test/CAP.BuilderTest.cs

+ 19
- 20
src/DotNetCore.CAP/Abstractions/CapPublisherBase.cs View File

@@ -36,22 +36,20 @@ namespace DotNetCore.CAP.Abstractions
return PublishWithTransAsync(name, content);
}

public void Publish<T>(string name, T contentObj, IDbConnection dbConnection,
string callbackName = null, IDbTransaction dbTransaction = null)
public void Publish<T>(string name, T contentObj, IDbTransaction dbTransaction, string callbackName = null)
{
CheckIsAdoNet(name);
PrepareConnectionForAdo(dbConnection, dbTransaction);
PrepareConnectionForAdo(dbTransaction);

var content = Serialize(contentObj, callbackName);

PublishWithTrans(name, content);
}

public Task PublishAsync<T>(string name, T contentObj, IDbConnection dbConnection,
string callbackName = null, IDbTransaction dbTransaction = null)
public Task PublishAsync<T>(string name, T contentObj, IDbTransaction dbTransaction, string callbackName = null)
{
CheckIsAdoNet(name);
PrepareConnectionForAdo(dbConnection, dbTransaction);
PrepareConnectionForAdo(dbTransaction);

var content = Serialize(contentObj, callbackName);

@@ -69,16 +67,22 @@ namespace DotNetCore.CAP.Abstractions
protected virtual string Serialize<T>(T obj, string callbackName = null)
{
var packer = (IMessagePacker)ServiceProvider.GetService(typeof(IMessagePacker));

string content = string.Empty;
if (Helper.IsComplexType(obj.GetType()))
string content;
if (obj != null)
{
var serializer = (IContentSerializer)ServiceProvider.GetService(typeof(IContentSerializer));
content = serializer.Serialize(obj);
if (Helper.IsComplexType(obj.GetType()))
{
var serializer = (IContentSerializer)ServiceProvider.GetService(typeof(IContentSerializer));
content = serializer.Serialize(obj);
}
else
{
content = obj.ToString();
}
}
else
{
content = obj?.ToString();
content = string.Empty;
}

var message = new CapMessageDto(content)
@@ -91,20 +95,15 @@ namespace DotNetCore.CAP.Abstractions

#region private methods

private void PrepareConnectionForAdo(IDbConnection dbConnection, IDbTransaction dbTransaction)
private void PrepareConnectionForAdo(IDbTransaction dbTransaction)
{
DbConnection = dbConnection ?? throw new ArgumentNullException(nameof(dbConnection));
DbTransaction = dbTransaction ?? throw new ArgumentNullException(nameof(dbTransaction));
DbConnection = DbTransaction.Connection;
if (DbConnection.State != ConnectionState.Open)
{
IsCapOpenedConn = true;
DbConnection.Open();
}
DbTransaction = dbTransaction;
if (DbTransaction == null)
{
IsCapOpenedTrans = true;
DbTransaction = dbConnection.BeginTransaction(IsolationLevel.ReadCommitted);
}
}

private void CheckIsUsingEF(string name)


+ 4
- 8
src/DotNetCore.CAP/ICapPublisher.cs View File

@@ -39,21 +39,17 @@ namespace DotNetCore.CAP
/// </summary>
/// <param name="name">the topic name or exchange router key.</param>
/// <param name="contentObj">message body content, that will be serialized of json.</param>
/// <param name="callbackName">callback subscriber name</param>
/// <param name="dbConnection">the connection of <see cref="IDbConnection" /></param>
/// <param name="dbTransaction">the transaction of <see cref="IDbTransaction" /></param>
Task PublishAsync<T>(string name, T contentObj, IDbConnection dbConnection, string callbackName = null,
IDbTransaction dbTransaction = null);
/// <param name="callbackName">callback subscriber name</param>
Task PublishAsync<T>(string name, T contentObj, IDbTransaction dbTransaction, string callbackName = null);

/// <summary>
/// (ado.net) Publish a object message.
/// </summary>
/// <param name="name">the topic name or exchange router key.</param>
/// <param name="contentObj">message body content, that will be serialized of json.</param>
/// <param name="callbackName">callback subscriber name</param>
/// <param name="dbConnection">the connection of <see cref="IDbConnection" /></param>
/// <param name="dbTransaction">the transaction of <see cref="IDbTransaction" /></param>
void Publish<T>(string name, T contentObj, IDbConnection dbConnection, string callbackName = null,
IDbTransaction dbTransaction = null);
/// <param name="callbackName">callback subscriber name</param>
void Publish<T>(string name, T contentObj, IDbTransaction dbTransaction, string callbackName = null);
}
}

+ 2
- 2
test/DotNetCore.CAP.Test/CAP.BuilderTest.cs View File

@@ -124,7 +124,7 @@ namespace DotNetCore.CAP.Test
throw new NotImplementedException();
}

public void Publish<T>(string name, T contentObj, IDbConnection dbConnection, string callbackName = null, IDbTransaction dbTransaction = null)
public void Publish<T>(string name, T contentObj, IDbTransaction dbTransaction, string callbackName = null)
{
throw new NotImplementedException();
}
@@ -159,7 +159,7 @@ namespace DotNetCore.CAP.Test
throw new NotImplementedException();
}

public Task PublishAsync<T>(string name, T contentObj, IDbConnection dbConnection, string callbackName = null, IDbTransaction dbTransaction = null)
public Task PublishAsync<T>(string name, T contentObj, IDbTransaction dbTransaction, string callbackName = null)
{
throw new NotImplementedException();
}


Loading…
Cancel
Save