English 中文(简体)
结构:如何在建筑商中找到一个名胜地
原标题:StructureMap: How to get a named instance within a constructor

Main:

class Program
{
    static void Main()
    {
        new CompositionRoot();

        Console.WriteLine("Test");
        var consumer = ObjectFactory.GetInstance<Consumer>();
        consumer.Parse();
        Console.ReadLine();
    }
}

<>Parsers:

public interface IFilePaser
{
    void Parse(string line);
}

public class ITMFileParser : IFilePaser
{
    public void Parse(string line)
    {
        Console.WriteLine("ITM");
    }
}

public class SEQFilePaser : IFilePaser
{
    public void Parse(string line)
    {
        Console.WriteLine("SEQ");
    }
}

<>CompositionRoot>:

public class CompositionRoot
{
    public CompositionRoot()
    {
        ObjectFactory.Initialize(x=>x.Scan(scan =>
                                               {
                                                   scan.AssembliesFromApplicationBaseDirectory();
                                                   scan.AddAllTypesOf<IFilePaser>().NameBy(NameBy);
                                               }));

        Consumer.Factory = ObjectFactory.GetNamedInstance<IFilePaser>;
    }



    private static string NameBy(Type type)
    {
        return type.Name.Replace("FileParser", "");
    }
}

<>载体>消费:

public class Consumer
{
    public static Func<string, IFilePaser> Factory = name => { throw new NotImplementedException(""); };

    public void Parse()
    {
        Factory("ITM").Parse("");
    }
}

我必须做些什么来利用以下重新出版的Consumer?

public class Consumer
{
    private readonly Func<string, IFilePaser> _filePaser;

    public Consumer(Func<string, IFilePaser> filePaser)
    {
        _filePaser = filePaser;
    }

    public void Parse()
    {
        _filePaser("ITM").Parse("");
    }
}
最佳回答

StructureMaps Func<T> trick isn t implemented for named instances. You need to register this explicitly:

ObjectFactory.Initialize(x => {
   x.Scan(scan => {
      scan.AssembliesFromApplicationBaseDirectory();
      scan.AddAllTypesOf<IFileParser>().NameBy(NameBy);
   });

   x.For<Func<string, IFileParser>>().Use(ObjectFactory.GetNamedInstance<IFileParser>); 
});
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签