在 ac# 控制台应用程序中设置结构图

发布于 2024-11-25 17:44:06 字数 1029 浏览 1 评论 0原文

我收到此错误:

StructureMap Exception Code:  202
No Default Instance defined for PluginFamily 

我的设置如下所示:

Console.WriteLine("Structure Map");
SetupSM sm = new SetupSM();
sm.Setup();

ISomeThing someThing = ObjectFactory.GetInstance<ISomeThing>();
Console.WriteLine("something.HowManyTHings: " + someThing.HowManyThings("asdf"));



 public class SetupSM
    {
        public void Setup()
        {
            var c1 = new Container(config =>
            {
                config.Scan(scan =>
                {
                    scan.TheCallingAssembly();
                    scan.WithDefaultConventions();
                });
            });

            var c2 = new Container(x =>
                                              {
                                                  x.For<ISomeThing>().Use<SomeThingOne>();
                                              });


        }
    }

这是我第一次尝试使用结构图,我缺少什么? 他们主网站上的指南似乎很旧,使用旧语法等。

I'm getting this error:

StructureMap Exception Code:  202
No Default Instance defined for PluginFamily 

My setup looks like:

Console.WriteLine("Structure Map");
SetupSM sm = new SetupSM();
sm.Setup();

ISomeThing someThing = ObjectFactory.GetInstance<ISomeThing>();
Console.WriteLine("something.HowManyTHings: " + someThing.HowManyThings("asdf"));



 public class SetupSM
    {
        public void Setup()
        {
            var c1 = new Container(config =>
            {
                config.Scan(scan =>
                {
                    scan.TheCallingAssembly();
                    scan.WithDefaultConventions();
                });
            });

            var c2 = new Container(x =>
                                              {
                                                  x.For<ISomeThing>().Use<SomeThingOne>();
                                              });


        }
    }

This is my first try at using structure map, what am I missing?
It seems the guide on their main website is very old using the old syntax etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼藏柔 2024-12-02 17:44:06

尝试将您的配置应用于静态 ObjectFactory,而不是您似乎立即扔掉的单独容器。

public class SetupSM
    {
        public void Setup()
        {
            ObjectFactory.Configure(config =>
            { 
                config.Scan(scan =>
                {
                    scan.TheCallingAssembly();
                    scan.WithDefaultConventions();
                });

                config.For<ISomething>().Use<SomeThingOne>();
            });
    }

Try applying your configuration to the static ObjectFactory instead of seperate containers which you appear to throw away immediately..

public class SetupSM
    {
        public void Setup()
        {
            ObjectFactory.Configure(config =>
            { 
                config.Scan(scan =>
                {
                    scan.TheCallingAssembly();
                    scan.WithDefaultConventions();
                });

                config.For<ISomething>().Use<SomeThingOne>();
            });
    }
紧拥背影 2024-12-02 17:44:06

前面的答案适用于 StructureMap 2 或 3,但在 StructureMap 4 中语法已更改。您可以在此处查看完整的解决方案:

http://ardalis.com/using -structmap-4-in-a-console-app

本质上,ObjectFactory 已被 Container 实例取代。您还可以选择在单独的注册表实例中执行配置(建议这样做)。因此,Main() 中的初始化代码变为:

static void Main(string[] args)
{
    var container = Container.For<ConsoleRegistry>();

    var app = container.GetInstance<Application>();
    app.Run();
    Console.ReadLine();
}

这使 Main 非常干净,并允许应用程序通过 请求所需的任何依赖项依赖注入并遵循显式依赖关系原则。结合起来,这些使您的应用程序变得非常可组合和可测试。

The previous answers are for StructureMap 2 or 3, but in StructureMap 4 the syntax has changed. You can see a complete solution here:

http://ardalis.com/using-structuremap-4-in-a-console-app

Essentially, ObjectFactory has been replaced with a Container instance. You can also optionally perform the configuration in a separate Registry instance, which is recommended. Thus your initialization code in Main() becomes:

static void Main(string[] args)
{
    var container = Container.For<ConsoleRegistry>();

    var app = container.GetInstance<Application>();
    app.Run();
    Console.ReadLine();
}

This keeps Main very clean, and allows Application to request any dependencies it needs through dependency injection and to follow the Explicit Dependencies Principle. Combined, these allow your application to be very composable and testable.

夏天碎花小短裙 2024-12-02 17:44:06
public static class StructureMapBootStrapper
{
    public static void BootStrap()
    {
        StructureMap.ObjectFactory.Initialize(
            bootStrapper =>
            {
                bootStrapper.For<ISomeThing>().Use<SomeThingOne>();
            });
    }
}

您的控制台应用程序:

Console.WriteLine("Structure Map");
StructureMapBootStrapper.BootStrap();

ISomeThing someThing = ObjectFactory.GetInstance<ISomeThing>();
Console.WriteLine("something.HowManyTHings: " + someThing.HowManyThings("asdf"));
public static class StructureMapBootStrapper
{
    public static void BootStrap()
    {
        StructureMap.ObjectFactory.Initialize(
            bootStrapper =>
            {
                bootStrapper.For<ISomeThing>().Use<SomeThingOne>();
            });
    }
}

Your console app:

Console.WriteLine("Structure Map");
StructureMapBootStrapper.BootStrap();

ISomeThing someThing = ObjectFactory.GetInstance<ISomeThing>();
Console.WriteLine("something.HowManyTHings: " + someThing.HowManyThings("asdf"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文