【问题标题】:What architecture to choose (WCF services, server side logic)选择什么架构(WCF 服务、服务器端逻辑)
【发布时间】:2016-02-22 19:56:50
【问题描述】:

我正在开发服务器端逻辑来处理请求并用数据响应前端服务器以及移动应用直接连接。

我已经实现了 SessionContext 类,它基本上可以确保数据库中每个被调用的服务都有匹配的会话记录(忘记密码情况等除外)。

我现在正在尝试实现事件日志记录。我想要有共同的逻辑,所以我可以记录所有请求、异常、数据等。

我想出了这段代码,但不知何故我对此感觉不太好——每种服务方法的代码都太多了。有没有什么聪明的技巧可以让我更短、更容易阅读/编码?

当前实现将使用 EventLogic 类将事件记录到事件表。在某些时候,某些事件可能与会话有关,因此我将 eventLog 作为参数传递给 SessionContext(以在事件和会话之间创建链接)。 SessionContext 在成功处理时保存实体数据...我有一种直觉,我的设计有问题。

public Session CreateUser(string email, string password, System.Net.IPAddress ipAddress)
{
    using (var eventLog = new EventLogic())
    {
        try
        {
            eventLog.LogCreateUser(email, password, ipAddress);

            using (var context = SessionContext.CreateUser(eventLog, email, password, ipAddress))
            {
                return new Session()
                {
                    Id = context.Session.UId,
                    HasExpired = context.Session.IsClosed,
                    IsEmailVerified = context.Session.User.IsEmailVerified,
                    TimeCreated = context.Session.TimeCreated,
                    PublicUserId = CryptoHelper.GuidFromId(context.Session, context.Session.UserId, CryptoHelper.TargetTypeEnum.PublicUser),
                    ServerTime = context.Time
                };
            }
        } 
        catch (Exception e)
        {
            eventLog.Exception(e);
        }
    }
}

【问题讨论】:

  • 为什么不创建一个更通用的方法,比如 eventLog.Log(exception) 和 eventLog.Log(classname, methodname, serializedMessage, isOutgoing);或者你可以使用像 log4net 这样的可用库。
  • 你需要这个日志数据做什么?

标签: c# wcf architecture


【解决方案1】:

您应该考虑使用类似SLF4J + LogBack(例如)的东西来进行日志记录。

如果您的课程遵循SRP,则您的每个应用程序不应有多个类似LogCreateUser 的调用类型。这意味着,无需将日志记录逻辑提取到新类中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-22
    • 2012-01-13
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    相关资源
    最近更新 更多