简体   繁体   中英

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. 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(...)

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). 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.

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. When resolving the IFileProcessor , you use the extension of the file that you want to parse and by that acquire the correct instance.

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:

<?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 "." of the extension, so you either have to remove it or configure your mapping names with a leading ".".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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