[英]One DLL with two implementations for two applications
我有一些類和方法的DLL。 和兩個使用它的應用程序。 一個需要幾乎所有方法的管理應用程序和僅需要部分內容的客戶端應用程序。 但是它們中的很大一部分都被他們兩個使用。 現在,我想用管理員的東西制作一個DLL,用客戶端的東西制作一個DLL。
是否有比擁有兩個不同的DLL和對每個更改進行手動編輯更好的方法?
通常,您可能不希望在客戶端公開管理代碼。 由於它是一個DLL,因此該代碼正等着被利用,因為這些方法必然是公開的。 更不用說反編譯.NET DLL是微不足道的,並且可能會暴露您不希望非管理員查看的管理程序的內部工作。
如果要最大程度地減少代碼重復,最好的方法(雖然不一定是“最簡單的”事情)是具有3個DLL:
由服務器,客戶端和管理客戶端組成的項目可能應具有3-4個庫:
您是否考慮過在公共庫上使用依賴項注入(某種形式的構造函數注入)來確定在執行過程中需要應用的規則。
這是一個非常簡單的示例:
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();
}
}
運行時,您將產生此輸出。
此消息是*重要*,請對此做點什么?
此消息很重要,請對此做些事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.