F#Unity设计时配置

时间:2014-05-20 15:29:45

标签: dependency-injection f# unity-container design-time

可以使它在C#中工作:

  • 创建名为UnityCSharp的C#控制台应用
  • 添加Unity NuGet
  • 参考System.Configuration
  • 如下所示添加App.config和C#代码,工作正常

C#的App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <alias alias="myAlias" type="UnityCSharp.ILogger, UnityCSharp" />
    <container>
      <register type="myAlias" mapTo="UnityCSharp.Logger, UnityCSharp" />
      <register type="myAlias" mapTo="UnityCSharp.SpecialLogger, UnityCSharp" name="special" />
    </container>
  </unity>
</configuration>

C#:

using System;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace UnityCSharp
{
    public interface ILogger { string SayHello(); }
    public class Logger : ILogger { public string SayHello() { return "hello"; } }
    public class SpecialLogger : ILogger { public string SayHello() { return "special hello"; } }

    class Program
    {
        static void Main(string[] args)
        {
            var container = new UnityContainer().LoadConfiguration();
            Console.WriteLine(container.Resolve<ILogger>().SayHello()); // --> "hello"
            Console.WriteLine(container.Resolve<ILogger>("special").SayHello()); // --> "special hello"
            Console.ReadLine();
        }
    }
}

但是不能在F#中重复它(再次使用控制台应用程序,名为UnityFSharp,相同的NuGet和System.Configuration引用)。

F#的App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <alias alias="myAlias" type="UnityFSharp.ILogger, UnityFSharp" />
    <container>
      <register type="myAlias" mapTo="UnityFSharp.Logger, UnityFSharp" />
      <register type="myAlias" mapTo="UnityFSharp.SpecialLogger, UnityFSharp" name="special" />
    </container>
  </unity>
</configuration>

F#:

open System;
open Microsoft.Practices.Unity
open Microsoft.Practices.Unity.Configuration

type ILogger = abstract SayHello : unit -> string
type Logger() = interface ILogger with member x.SayHello () = "hello"
type SpecialLogger() = interface ILogger with member x.SayHello () = "special hello"

[<EntryPoint>]
let main argv = 
    let container = (new UnityContainer()).LoadConfiguration() // CRASHES HERE
    Console.WriteLine (container.Resolve<ILogger>().SayHello()) // --> "hello"
    Console.WriteLine (container.Resolve<ILogger>("special").SayHello()) // --> "special hello"
    Console.ReadLine() |> ignore
    0

将失败:

  

无法解析类型名称或别名myAlias。请检查   您的配置文件并验证此类型名称。

我理解这些命名在F#中有所不同(http://ilspy.net/截图如下)。 但是我怎么能让它发挥作用呢? 我尝试了各种F#模块,命名空间和App.config版本。 只有一个工作的例子会有帮助! (我可以重组我的F#)

enter image description here

1 个答案:

答案 0 :(得分:2)

这里定义类型的方式,它们编译成名为

的类型
  • Program+ILogger
  • Program+Logger
  • Program+SpecialLogger

因为它们是在Program模块中定义的。

一个选项是编辑配置文件以匹配名称:

<alias alias="myAlias" type="Program+ILogger, UnityFSharp" />
<container>
  <register type="myAlias" mapTo="Program+Logger, UnityFSharp" />
  <register type="myAlias" mapTo="Program+SpecialLogger, UnityFSharp" name="special" />
</container>

我认为这样做的方法是在程序崩溃之前为程序添加一行:

Console.WriteLine typeof<ILogger>

如果您不喜欢这些模块名称,可以将这些类型放在适当的名称空间中:

namespace Ploeh.Samples

type ILogger = abstract SayHello : unit -> string
type Logger() = interface ILogger with member x.SayHello () = "hello"
type SpecialLogger() = interface ILogger with member x.SayHello () = "special hello"

但是,这将涉及将它们放在单独的文件中,因为main模块中定义了Program函数。使用上面的命名空间,名称变为:

  • Ploeh.Samples.ILogger
  • Ploeh.Samples.Logger
  • Ploeh.Samples.SpecialLogger