简体   繁体   English

使用Unity或MEF在C#中解析条件类型

[英]Conditional type resolving in C# with Unity or MEF

I'm working on an application architecture and i'm facing a BIG question. 我正在开发应用程序体系结构,并且遇到了一个大问题。 The app is quite simple, it receives a file, the app apply a syntaxe validation,a parsing,... and other stuff. 该应用程序非常简单,它接收一个文件,该应用程序应用语法验证,解析等...。 We can have diffrent type of files (eg : File1.xx, File2.yy), each File type has a specefic implementation of Syntax validation and parsing. 我们可以使用不同类型的文件(例如:File1.xx,File2.yy),每种文件类型都有特定的语法验证和解析实现。 The main constraint is to have a generic and easily extensible application ( can extend the type of managed files). 主要限制是拥有通用且易于扩展的应用程序(可以扩展托管文件的类型)。

That's why i have imagined something like : 这就是为什么我曾想过:

An Interface "IFileProcessor" in which i have 2 methods (or more) -bool IsSytaxValid(File f) -String Parse(...) 接口“ IFileProcessor” ,其中我有2种方法(或更多方法) -bool IsSytaxValid(File f)-String Parse(...)

In the app i shoud have something like : 在应用程序中,我应该有类似以下内容:

**// For a current File F1
IFileProcessor fileProcessor;
if (fileProcessor.IsSyntaxtValid(){
fileProcessor.Parse();
}else
 {
  bla bla bla
 }**

I want to have a plugin for File1.xx an another from File2.yy(2 dll in which i implement IFileProcessor interface). 我想要一个File1.xx插件,一个来自File2.yy的插件(我实现IFileProcessor接口的2个dll)。 The processing implemention, according the current file type, is dynamically loaded. 根据当前文件类型,动态加载处理实现。

I have a basic knowledges about Unity and MEF, i know it's my issue is managed by these kind of framworks. 我对Unity和MEF有基本的了解,我知道我的问题是由此类framworks管理的。

The Question is, which solution is the best to resolve my issue, and how i can do it ? 问题是,哪种解决方案是解决我的问题的最佳方法,我该怎么做?

Thanks in advance for your Help. 在此先感谢您的帮助。

If you use the file extension to discern between the file types, you can create named registrations for IFileProcessor based on the file extension. 如果使用文件扩展名区分文件类型,则可以基于文件扩展名为IFileProcessor创建命名注册。 When resolving the IFileProcessor , you use the extension of the file that you want to parse and by that acquire the correct instance. 解析IFileProcessor ,将使用要解析的文件扩展名,并以此获取正确的实例。

You can create a named registration like this programmatically: 您可以通过编程方式创建命名注册,如下所示:

IUnityContainer container = new UnityContainer();
container.RegisterType<IFileProcessor, TxtFileProcessor>(".txt");
container.RegisterType<IFileProcessor, CsvFileProcessor>(".csv");

As you want to use a Plugin model, you can also create the registrations by configuring the Unity container accordingly, eg: 当您要使用插件模型时,还可以通过相应地配置Unity容器来创建注册,例如:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
    </configSections>
    <unity>
        <containers>
            <container>
                <types>
                    <type name=".txt"
                          type="MyNamespace.IFileProcessor, MyAssembly"
                          mapTo="MyPluginNamespace.TxtFileProcessor, MyPluginAssembly"/>
                </types>
            </container>
        </containers>
   </unity>

And resolve it like this: 并像这样解决它:

var fileExt = System.IO.Path.GetExtension(filePath);
var fileProc = container.Resolve<IFileProcessor>(fileExt.ToLower());

Please note that the GetExtension method returns the leading "." 请注意, GetExtension方法返回前导“”。 of the extension, so you either have to remove it or configure your mapping names with a leading ".". 扩展名,因此您要么将其删除,要么用前导“。”配置映射名称。

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

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