首页 > 解决方案 > 尝试编写一个 Node.js 多层应用程序,就像我在 c# 中使用的那样

问题描述

我已经用 C# 编写了多层应用程序,如下所示。

它使进行全栈测试变得容易。

我已经研究 Node.js 几个星期了,似乎找不到这种分离,尤其是业务逻辑层。

我错过了一些明显的东西吗?

TypeScript 会是更好的选择吗?

层:
DTO - 数据传输对象
DAL - 数据访问层
BUS - 业务逻辑层
UI - 用户界面

这是所有层的部分任务应用程序:

using App.BusinessLogicLayer;
using App.DataAccessLayer;
using App.DataTransferObjects;
using System;

namespace App.DataTransferObjects
{
    public class Task
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

namespace App.DataAccessLayer
{
    public class Tasks
    {
        public static Task Get()
        {
            return new Task() { Id = 1, Name = "Task 1" };
        }
    }
}

namespace App.BusinessLogicLayer
{
    public interface ITaskContext
    {
        Task Get();
    }

    public class TaskContext : ITaskContext
    {
        public Task Get()
        {
            return Tasks.Get();
        }
    }

    public class TaskManager
    {
        ITaskContext _taskContext;
        public TaskManager() : this (new TaskContext())
        {

        }

        public TaskManager(ITaskContext taskContext)
        {
            _taskContext = taskContext;
        }

        public Task Get()
        {
            return _taskContext.Get();
        }
    }
}

namespace App.UserInterface
{
    class Program
    {
        static void Main(string[] args)
        {
            TaskManager tm = new TaskManager();
            Console.WriteLine(tm.Get().Name);
        }
    }
}

标签: node.jsexpress

解决方案


推荐阅读