[英]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。
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:
A project that consists of a server, client, and admin client should likely have 3-4 libraries: 由服务器,客户端和管理客户端组成的项目可能应具有3-4个库:
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.