构造函数链接和静态属性参数以及 StructureMap

发布于 2024-11-13 07:18:49 字数 1317 浏览 2 评论 0原文

我希望我的构造函数使用参数调用另一个构造函数,但是当我执行此操作(MyProperty)时,MyProperty 必须是静态的。问题出在这个静态属性的 getter 中,我必须从 Structuremap 容器获取 ISettingReader 的实例,并且由于它是静态的,我的容器只包含两个元素而不是超过 50 个元素,然后它找不到该实例。 (ISettingReader 上的pluginFamily 错误)

这是我的代码。

    private static Func<LinqDataContext> _contextFactory;
    public static Func<LinqDataContext> DefaultContextFactory
    {
        get
        {
            var settingReader = ObjectFactory.GetInstance<ISettingReader>(); // I get an error saying it can't find ISettingReader()
            var connectionString = settingReader.GetSetting("MyProject.ConnectionString");
            _contextFactory = () => new LinqDataContext(connectionString);
            return _contextFactory;
        }
    }

    public MyProjectViewModelService() : this(DefaultContextFactory)
    {

    }

    public MyProjectViewModelService(Func<LinqDataContext> contextFactory)
    {
        _contextFactory = contextFactory;
    }

我认为如果我可以去掉 static 关键字,它应该可以工作。我确认当我在 Program.exe 中启动应用程序时,我已经在 StructureMap 容器中初始化了 ISettingReader

那么我应该做什么? 谢谢 !

John

PS:我在stackoverflow上发现了类似的问题,但他没有使用structureMap: 带有中间变量的构造函数链接

I would like my constructor to call another constructor with parameter but when I do this(MyProperty), then MyProperty must be static. And the problem is in the getter of this static Property, I have to get an instance of ISettingReader from structuremap Container and as it is static, my container contains only two elements instead of more than 50 elements, then it can't find the instance. (Error of pluginFamily on ISettingReader)

Here's my code.

    private static Func<LinqDataContext> _contextFactory;
    public static Func<LinqDataContext> DefaultContextFactory
    {
        get
        {
            var settingReader = ObjectFactory.GetInstance<ISettingReader>(); // I get an error saying it can't find ISettingReader()
            var connectionString = settingReader.GetSetting("MyProject.ConnectionString");
            _contextFactory = () => new LinqDataContext(connectionString);
            return _contextFactory;
        }
    }

    public MyProjectViewModelService() : this(DefaultContextFactory)
    {

    }

    public MyProjectViewModelService(Func<LinqDataContext> contextFactory)
    {
        _contextFactory = contextFactory;
    }

I think if I can get rid of my static keyword, it should work. And I confirm I have initialized my ISettingReader in structureMap container when I started my application in Program.exe

So what should I do ?
Thanks !

John

PS: there's similar problem I found on stackoverflow, but he doesn't use structureMap:
Constructor chaining with intermediate variables

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

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

发布评论

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

评论(1

紫轩蝶泪 2024-11-20 07:18:49

为什么不将代码从 getter 移至默认构造函数,并将 ISettingReader 作为构造函数中的依赖项。当您请求新的 MyProjectViewModelService Structuremap 时,它将自动解析 ISettingReader 并为该依赖项提供实例化的默认类型。

所以如果你愿意的话
public MyProjectViewModelService(ISettingReader 设置Reader)
{
var connectionString =settingReader.GetSetting("MyProject.ConnectionString");
_contextFactory = () =>;新的 LinqDataContext(connectionString);
}

您可以删除其余的。

从结构图中得到的异常是因为您没有为 ISettingReader 配置任何默认实例

Why don't you move the code from your getter to the default constructor and put the ISettingReader as a dependency in the constructor. When you request a new MyProjectViewModelService Structuremap will automatically resolve the ISettingReader and supply the instantiated default type for that dependency.

So if you would have
public MyProjectViewModelService(ISettingReader settingReader)
{
var connectionString = settingReader.GetSetting("MyProject.ConnectionString");
_contextFactory = () => new LinqDataContext(connectionString);
}

You can remove the rest.

The exception you get from structuremap is because you didn't configure any default instance for ISettingReader

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文