简体   繁体   English

一个DLL具有两个应用程序的两个实现

[英]One DLL with two implementations for two applications

I have a DLL with some classes and methods. 我有一些类和方法的DLL。 And two applications using it. 和两个使用它的应用程序。 One admin-application that needs almost every method and a client-application that only needs parts of the stuff. 一个需要几乎所有方法的管理应用程序和仅需要部分内容的客户端应用程序。 But big parts of it are used by both of them. 但是它们中的很大一部分都被他们两个使用。 Now I want make a DLL with the admin stuff and one with the client stuff. 现在,我想用管理员的东西制作一个DLL,用客户端的东西制作一个DLL。

  1. Duplicating the DLL and edit things manually everytime is horrible. 每次都复制DLL并手动编辑内容是可怕的。
  2. Maybe conditional compiling helps me but I dont know how to compile the DLL twice with different coditions in one solution with the three projects. 也许条件编译对我有帮助,但是我不知道如何在三个项目的一个解决方案中用不同的条件对DLL进行两次编译。

Is there a better approach for this issue than having two different DLLs and manually editing on every change? 是否有比拥有两个不同的DLL和对每个更改进行手动编辑更好的方法?

In general, you probably don't want admin code exposed on the client side. 通常,您可能不希望在客户端公开管理代码。 Since it's a DLL, that code is just waiting to be exploited, because those methods are, by necessity, public. 由于它是一个DLL,因此该代码正等着被利用,因为这些方法必然是公开的。 Not to mention decompiling a .NET DLL is trivial and may expose inner-workings of your admin program you really don't want a non-administrator to see. 更不用说反编译.NET DLL是微不足道的,并且可能会暴露您不希望非管理员查看的管理程序的内部工作。

The best, though not necessarily the "easiest" thing to do, if you want to minimize code duplication, is to have 3 DLLs: 如果要最大程度地减少代码重复,最好的方法(虽然不一定是“最简单的”事情)是具有3个DLL:

  1. A common library that contains ONLY functions that BOTH applications use 一个公共库,仅包含两个应用程序都使用的函数
  2. A library that ONLY the admin application will use (or else compile it straight into the application if nothing else uses those functions at all) 一个仅由管理应用程序使用的库(或者,如果根本没有其他函数使用这些功能,则将其直接编译到应用程序中)
  3. A library that ONLY the client application will use (with same caveat as above) 仅客户端应用程序将使用的库(与上述相同)

A project that consists of a server, client, and admin client should likely have 3-4 libraries: 由服务器,客户端和管理客户端组成的项目可能应具有3-4个库:

  1. Common library, used by all 3 公用库,所有3个都使用
  2. Client library, used by client and server 客户端库,由客户端和服务器使用
  3. Admin library, used by server and admin client 管理库,服务器和管理客户端使用
  4. Server library, used only by server (or else compile the methods directly into the application) 服务器库,仅由服务器使用(或直接将方法编译到应用程序中)

Have you considered using dependency injection on the common library, some form of constructor injection to determine the rules that need to be applied during execution. 您是否考虑过在公共库上使用依赖项注入(某种形式的构造函数注入)来确定在执行过程中需要应用的规则。

Here's a very simple example: 这是一个非常简单的示例:

public interface IWorkerRule
    {
        string FormatText(string input);
    }

    internal class AdminRules : IWorkerRule
    {
        public string FormatText(string input)
        {
            return input.Replace("!", "?");
        }
    }

    internal class UserRules : IWorkerRule
    {
        public string FormatText(string input)
        {
            return input.Replace("!", ".");
        }
    }

    public class Worker
    {
        private IWorkerRule Rule { get; set; }

        public Worker(IWorkerRule rule)
        {
            Rule = rule;
        }

        public string FormatText(string text)
        {
            //generic shared formatting applied to any consumer
            text = text.Replace("@", "*");

            //here we apply the injected logic 
            text = Rule.FormatText(text);
            return text;
        }
    }

    class Program
    {       
        //injecting admin functions
        static void Main()
        {
            const string sampleText = "This message is @Important@ please do something about it!";

            //inject the admin rules.
            var worker = new Worker(new AdminRules());
            Console.WriteLine(worker.FormatText(sampleText));

            //inject the user rules
            worker = new Worker(new UserRules());
            Console.WriteLine(worker.FormatText(sampleText));

            Console.ReadLine();

        }

    }

When run you'll produce this output. 运行时,您将产生此输出。

This message is *Important* please do something about it? 此消息是*重要*,请对此做点什么?

This message is *Important* please do something about it. 此消息很重要,请对此做些事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM