Skip to content
This repository has been archived by the owner on Aug 2, 2020. It is now read-only.

Commit

Permalink
更改IOC至Autofac
Browse files Browse the repository at this point in the history
当前版本因DllExport问题暂无法使用,预计星期内DllExport释出修正版本;

详情:
3F/DllExport#125
  • Loading branch information
kotone committed Dec 11, 2019
1 parent 2cc7814 commit d2b1a4d
Show file tree
Hide file tree
Showing 25 changed files with 207 additions and 177 deletions.
11 changes: 8 additions & 3 deletions App/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.Linq;
using System.Text;
using Native.Csharp.Sdk.Cqp;
using Unity;
using Autofac;

namespace native.cqp.NETCore.App
namespace native.csharp.NETCore.App
{
/// <summary>
/// 用于存放 App 数据的公共类
Expand All @@ -32,10 +32,15 @@ public static class Common
/// </summary>
public static bool IsRunning { get; set; }

/// <summary>
/// 获取或设置当前 App 使用的依赖注入容器
/// </summary>
public static ContainerBuilder ContainerBuilder { get; set; }

/// <summary>
/// 获取或设置当前 App 使用的依赖注入容器实例
/// </summary>
public static IUnityContainer UnityContainer { get; set; }
public static IContainer Container { get; set; }

/// <summary>
/// 获取或设置当前 App 使用的 酷Q Api 接口实例
Expand Down
117 changes: 60 additions & 57 deletions App/Core/LibExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
using native.cqp.NETCore.App.Event;
using native.csharp.NETCore.App.Event;
using Native.Csharp.Sdk.Cqp;
using Native.Csharp.Sdk.Cqp.EventArgs;
using Native.Csharp.Sdk.Cqp.Interface;
using Native.Csharp.Sdk.Cqp.Model;
using Native.Csharp.Sdk.Cqp.Expand;
using Unity;
using Autofac;

namespace native.cqp.NETCore.App.Core
namespace native.csharp.NETCore.App.Core
{
public class LibExport
{
Expand All @@ -29,18 +29,22 @@ public class LibExport
static LibExport ()
{
_defaultEncoding = Encoding.GetEncoding ("GB18030");

// 初始化依赖注入容器
Common.UnityContainer = new UnityContainer ();
Common.ContainerBuilder = new ContainerBuilder ();

// 程序开始调用方法进行注册
Event_AppMain.Registbackcall (Common.UnityContainer);
Event_AppMain.Registbackcall (Common.ContainerBuilder);

// 注册完毕调用方法进行分发
Event_AppMain.Resolvebackcall (Common.UnityContainer);
Event_AppMain.Resolvebackcall (Common.ContainerBuilder);

//建立依赖注入容器实例
Common.Container = Common.ContainerBuilder.Build();

// 分发应用内事件
ResolveAppbackcall ();

}
#endregion

Expand All @@ -58,11 +62,10 @@ private static string AppInfo ()
Common.AppVersion = Version.Parse ("1.0.0");

//
// 当前项目名称: native.cqp.NETCore
// 当前项目名称: native.csharp.NETCore
// Api版本: 9

return string.Format ("{0},{1}", 9, "native.cqp.NETCore");
Environment.Exit(7);
return string.Format ("{0},{1}", 9, "native.csharp.NETCore");
}

/// <summary>
Expand All @@ -77,7 +80,7 @@ private static int Initialize (int authCode)
Common.CqApi = new CqApi (authCode);

// AuthCode 传递完毕后将对象加入容器托管, 以便在其它项目中调用
Common.UnityContainer.RegisterInstance<CqApi> ("native.cqp.NETCore", Common.CqApi);
Common.ContainerBuilder.RegisterInstance<CqApi> (Common.CqApi).Keyed<CqApi>("native.csharp.NETCore");

// 注册插件全局异常捕获回调, 用于捕获未处理的异常, 回弹给 酷Q 做处理
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Expand Down Expand Up @@ -115,167 +118,167 @@ private static void ResolveAppbackcall ()
* Id: 1
* Name: 私聊消息处理
*/
if (Common.UnityContainer.IsRegistered<IReceiveFriendMessage> ("私聊消息处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveFriendMessage> ("私聊消息处理") == true)
{
ReceiveFriendMessage_1 = Common.UnityContainer.Resolve<IReceiveFriendMessage> ("私聊消息处理").ReceiveFriendMessage;
ReceiveFriendMessage_1 = Common.Container.ResolveKeyed<IReceiveFriendMessage> ("私聊消息处理").ReceiveFriendMessage;
}
if (Common.UnityContainer.IsRegistered<IReceiveOnlineStatusMessage> ("私聊消息处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveOnlineStatusMessage> ("私聊消息处理") == true)
{
ReceiveOnlineStatusMessage_1 = Common.UnityContainer.Resolve<IReceiveOnlineStatusMessage> ("私聊消息处理").ReceiveOnlineStatusMessage;
ReceiveOnlineStatusMessage_1 = Common.Container.ResolveKeyed<IReceiveOnlineStatusMessage> ("私聊消息处理").ReceiveOnlineStatusMessage;
}
if (Common.UnityContainer.IsRegistered<IReceiveGroupPrivateMessage> ("私聊消息处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveGroupPrivateMessage> ("私聊消息处理") == true)
{
ReceiveGroupPrivateMessage_1 = Common.UnityContainer.Resolve<IReceiveGroupPrivateMessage> ("私聊消息处理").ReceiveGroupPrivateMessage;
ReceiveGroupPrivateMessage_1 = Common.Container.ResolveKeyed<IReceiveGroupPrivateMessage> ("私聊消息处理").ReceiveGroupPrivateMessage;
}
if (Common.UnityContainer.IsRegistered<IReceiveDiscussPrivateMessage> ("私聊消息处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveDiscussPrivateMessage> ("私聊消息处理") == true)
{
ReceiveDiscussPrivateMessage_1 = Common.UnityContainer.Resolve<IReceiveDiscussPrivateMessage> ("私聊消息处理").ReceiveDiscussPrivateMessage;
ReceiveDiscussPrivateMessage_1 = Common.Container.ResolveKeyed<IReceiveDiscussPrivateMessage> ("私聊消息处理").ReceiveDiscussPrivateMessage;
}

/*
* Id: 2
* Name: 群消息处理
*/
if (Common.UnityContainer.IsRegistered<IReceiveGroupMessage> ("群消息处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveGroupMessage> ("群消息处理") == true)
{
ReceiveGroupMessage_2 = Common.UnityContainer.Resolve<IReceiveGroupMessage> ("群消息处理").ReceiveGroupMessage;
ReceiveGroupMessage_2 = Common.Container.ResolveKeyed<IReceiveGroupMessage> ("群消息处理").ReceiveGroupMessage;
}

/*
* Id: 3
* Name: 讨论组消息处理
*/
if (Common.UnityContainer.IsRegistered<IReceiveDiscussMessage> ("讨论组消息处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveDiscussMessage> ("讨论组消息处理") == true)
{
ReceiveDiscussMessage_3 = Common.UnityContainer.Resolve<IReceiveDiscussMessage> ("讨论组消息处理").ReceiveDiscussMessage;
ReceiveDiscussMessage_3 = Common.Container.ResolveKeyed<IReceiveDiscussMessage> ("讨论组消息处理").ReceiveDiscussMessage;
}

/*
* Id: 4
* Name: 群文件上传事件处理
*/
if (Common.UnityContainer.IsRegistered<IReceiveGroupFileUpload> ("群文件上传事件处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveGroupFileUpload> ("群文件上传事件处理") == true)
{
ReceiveFileUploadMessage_4 = Common.UnityContainer.Resolve<IReceiveGroupFileUpload> ("群文件上传事件处理").ReceiveGroupFileUpload;
ReceiveFileUploadMessage_4 = Common.Container.ResolveKeyed<IReceiveGroupFileUpload> ("群文件上传事件处理").ReceiveGroupFileUpload;
}

/*
* Id: 5
* Name: 群管理变动事件处理
*/
if (Common.UnityContainer.IsRegistered<IReceiveGroupManageIncrease> ("群管理变动事件处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveGroupManageIncrease> ("群管理变动事件处理") == true)
{
ReceiveManageIncrease_5 = Common.UnityContainer.Resolve<IReceiveGroupManageIncrease> ("群管理变动事件处理").ReceiveGroupManageIncrease;
ReceiveManageIncrease_5 = Common.Container.ResolveKeyed<IReceiveGroupManageIncrease> ("群管理变动事件处理").ReceiveGroupManageIncrease;
}
if (Common.UnityContainer.IsRegistered<IReceiveGroupManageDecrease> ("群管理变动事件处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveGroupManageDecrease> ("群管理变动事件处理") == true)
{
ReceiveManageDecrease_5 = Common.UnityContainer.Resolve<IReceiveGroupManageDecrease> ("群管理变动事件处理").ReceiveGroupManageDecrease;
ReceiveManageDecrease_5 = Common.Container.ResolveKeyed<IReceiveGroupManageDecrease> ("群管理变动事件处理").ReceiveGroupManageDecrease;
}

/*
* Id: 6
* Name: 群成员减少事件处理
*/
if (Common.UnityContainer.IsRegistered<IReceiveGroupMemberLeave> ("群成员减少事件处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveGroupMemberLeave> ("群成员减少事件处理") == true)
{
ReceiveMemberLeave_6 = Common.UnityContainer.Resolve<IReceiveGroupMemberLeave> ("群成员减少事件处理").ReceiveGroupMemberLeave;
ReceiveMemberLeave_6 = Common.Container.ResolveKeyed<IReceiveGroupMemberLeave> ("群成员减少事件处理").ReceiveGroupMemberLeave;
}
if (Common.UnityContainer.IsRegistered<IReceiveGroupMemberRemove> ("群成员减少事件处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveGroupMemberRemove> ("群成员减少事件处理") == true)
{
ReceiveMemberRemove_6 = Common.UnityContainer.Resolve<IReceiveGroupMemberRemove> ("群成员减少事件处理").ReceiveGroupMemberRemove;
ReceiveMemberRemove_6 = Common.Container.ResolveKeyed<IReceiveGroupMemberRemove> ("群成员减少事件处理").ReceiveGroupMemberRemove;
}

/*
* Id: 7
* Name: 群成员增加事件处理
*/
if (Common.UnityContainer.IsRegistered<IReceiveGroupMemberPass> ("群成员增加事件处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveGroupMemberPass> ("群成员增加事件处理") == true)
{
ReceiveMemberPass_7 = Common.UnityContainer.Resolve<IReceiveGroupMemberPass> ("群成员增加事件处理").ReceiveGroupMemberPass;
ReceiveMemberPass_7 = Common.Container.ResolveKeyed<IReceiveGroupMemberPass> ("群成员增加事件处理").ReceiveGroupMemberPass;
}
if (Common.UnityContainer.IsRegistered<IReceiveGroupMemberBeInvitee> ("群成员增加事件处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveGroupMemberBeInvitee> ("群成员增加事件处理") == true)
{
ReceiveMemberBeInvitee_7 = Common.UnityContainer.Resolve<IReceiveGroupMemberBeInvitee> ("群成员增加事件处理").ReceiveGroupMemberBeInvitee;
ReceiveMemberBeInvitee_7 = Common.Container.ResolveKeyed<IReceiveGroupMemberBeInvitee> ("群成员增加事件处理").ReceiveGroupMemberBeInvitee;
}

/*
* Id: 8
* Name: 群禁言事件处理
*/
if (Common.UnityContainer.IsRegistered<IReceiveSetGroupBan> ("群禁言事件处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveSetGroupBan> ("群禁言事件处理") == true)
{
ReceiveSetGroupBan_8 = Common.UnityContainer.Resolve<IReceiveSetGroupBan> ("群禁言事件处理").ReceiveSetGroupBan;
ReceiveSetGroupBan_8 = Common.Container.ResolveKeyed<IReceiveSetGroupBan> ("群禁言事件处理").ReceiveSetGroupBan;
}
if (Common.UnityContainer.IsRegistered<IReceiveRemoveGroupBan> ("群禁言事件处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveRemoveGroupBan> ("群禁言事件处理") == true)
{
ReceiveRemoveGroupBan_8 = Common.UnityContainer.Resolve<IReceiveRemoveGroupBan> ("群禁言事件处理").ReceiveRemoveGroupBan;
ReceiveRemoveGroupBan_8 = Common.Container.ResolveKeyed<IReceiveRemoveGroupBan> ("群禁言事件处理").ReceiveRemoveGroupBan;
}

/*
* Id: 10
* Name: 好友已添加事件处理
*/
if (Common.UnityContainer.IsRegistered<IReceiveFriendIncrease> ("好友已添加事件处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveFriendIncrease> ("好友已添加事件处理") == true)
{
ReceiveFriendIncrease_10 = Common.UnityContainer.Resolve<IReceiveFriendIncrease> ("好友已添加事件处理").ReceiveFriendIncrease;
ReceiveFriendIncrease_10 = Common.Container.ResolveKeyed<IReceiveFriendIncrease> ("好友已添加事件处理").ReceiveFriendIncrease;
}

/*
* Id: 11
* Name: 好友添加请求处理
*/
if (Common.UnityContainer.IsRegistered<IReceiveFriendAddRequest> ("好友添加请求处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveFriendAddRequest> ("好友添加请求处理") == true)
{
ReceiveFriendAdd_11 = Common.UnityContainer.Resolve<IReceiveFriendAddRequest> ("好友添加请求处理").ReceiveFriendAddRequest;
ReceiveFriendAdd_11 = Common.Container.ResolveKeyed<IReceiveFriendAddRequest> ("好友添加请求处理").ReceiveFriendAddRequest;
}

/*
* Id: 12
* Name: 群添加请求处理
*/
if (Common.UnityContainer.IsRegistered<IReceiveAddGroupRequest> ("群添加请求处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveAddGroupRequest> ("群添加请求处理") == true)
{
ReceiveAddGroupRequest_12 = Common.UnityContainer.Resolve<IReceiveAddGroupRequest> ("群添加请求处理").ReceiveAddGroupRequest;
ReceiveAddGroupRequest_12 = Common.Container.ResolveKeyed<IReceiveAddGroupRequest> ("群添加请求处理").ReceiveAddGroupRequest;
}
if (Common.UnityContainer.IsRegistered<IReceiveAddGroupBeInvitee> ("群添加请求处理") == true)
if (Common.Container.IsRegisteredWithKey<IReceiveAddGroupBeInvitee> ("群添加请求处理") == true)
{
ReceiveAddGroupBeInvitee_12 = Common.UnityContainer.Resolve<IReceiveAddGroupBeInvitee> ("群添加请求处理").ReceiveAddGroupBeInvitee;
ReceiveAddGroupBeInvitee_12 = Common.Container.ResolveKeyed<IReceiveAddGroupBeInvitee> ("群添加请求处理").ReceiveAddGroupBeInvitee;
}

/*
* Id: 1001
* Name: 酷Q启动事件
*/
if (Common.UnityContainer.IsRegistered<ICqStartup> ("酷Q启动事件") == true)
if (Common.Container.IsRegisteredWithKey<ICqStartup> ("酷Q启动事件") == true)
{
CqStartup_1001 = Common.UnityContainer.Resolve<ICqStartup> ("酷Q启动事件").CqStartup;
CqStartup_1001 = Common.Container.ResolveKeyed<ICqStartup> ("酷Q启动事件").CqStartup;
}

/*
* Id: 1002
* Name: 酷Q关闭事件
*/
if (Common.UnityContainer.IsRegistered<ICqExit> ("酷Q关闭事件") == true)
if (Common.Container.IsRegisteredWithKey<ICqExit> ("酷Q关闭事件") == true)
{
CqExit_1002 = Common.UnityContainer.Resolve<ICqExit> ("酷Q关闭事件").CqExit;
CqExit_1002 = Common.Container.ResolveKeyed<ICqExit> ("酷Q关闭事件").CqExit;
}

/*
* Id: 1003
* Name: 应用已被启用
*/
if (Common.UnityContainer.IsRegistered<ICqAppEnable> ("应用已被启用") == true)
if (Common.Container.IsRegisteredWithKey<ICqAppEnable> ("应用已被启用") == true)
{
AppEnable_1003 = Common.UnityContainer.Resolve<ICqAppEnable> ("应用已被启用").CqAppEnable;
AppEnable_1003 = Common.Container.ResolveKeyed<ICqAppEnable> ("应用已被启用").CqAppEnable;
}

/*
* Id: 1004
* Name: 应用将被停用
*/
if (Common.UnityContainer.IsRegistered<ICqAppDisable> ("应用将被停用") == true)
if (Common.Container.IsRegisteredWithKey<ICqAppDisable> ("应用将被停用") == true)
{
AppDisable_1004 = Common.UnityContainer.Resolve<ICqAppDisable> ("应用将被停用").CqAppDisable;
AppDisable_1004 = Common.Container.ResolveKeyed<ICqAppDisable> ("应用将被停用").CqAppDisable;
}


Expand Down
Loading

0 comments on commit d2b1a4d

Please sign in to comment.