如何在我自己的项目中使用ServiceStack Funq

时间:2021-01-21 07:16:07

At work we're doing several new web services projects in ServiceStack and taking advantage of Funq in some of them. I'm currently working on a separate project that will consume said web services and was wondering if there was a way for me to use ServiceStack's Funq in my project to resolve my dependencies as to use more or less the same patterns we're using when developing our web services.

在工作中,我们在ServiceStack中进行了几个新的Web服务项目,并在其中一些项目中利用了Funq。我目前正在开发一个单独的项目,它将使用所述Web服务,并且想知道我是否有办法在我的项目中使用ServiceStack的Funq来解析我的依赖关系,以便使用或多或少使用我们正在使用的相同模式开发我们的Web服务。

Is this possible?

这可能吗?

2 个解决方案

#1


3  

I'm in a similar position, wanting to use heaps of the ServiceStack tools in a non-webby project. I agree that there is a ... slight lack in documentation for Funq

我处于类似的位置,希望在非webby项目中使用大量的ServiceStack工具。我同意Funq的文档略有缺失

I have been using it in a legacy WinForms app, trying to avoid changing the original project (too much) and I add the new forms to a new project.
I added references to most of the ServiceStack libraries to most of my projects (manually because I'm doing this in .Net 3.5)

我一直在传统的WinForms应用程序中使用它,试图避免更改原始项目(太多)并将新表单添加到新项目中。我在大多数项目中添加了对大多数ServiceStack库的引用(手动,因为我在.Net 3.5中这样做)

Here is the code in the winforms Program.cs file; Note that the FunqContainer is a public static property - I'm still not sure about that, but it gives access across the whole project to the FunqContainer

这是winforms Program.cs文件中的代码;请注意,FunqContainer是一个公共静态属性 - 我仍然不确定,但它允许整个项目访问FunqContainer

using System;
using System.Threading;
using System.Windows.Forms;

using Funq;
using MyApp.Utilities;

static class Program
    {
        public static Funq.Container FunqContainer { get; set; }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            FunqContainer = new Container();
            FunqContainer.Init(); 

            etc...
        }
    }

FunqContainer.Init() is an extension method in my separate project for - you guessed it - initializing Funq

FunqContainer.Init()是我单独项目中的扩展方法 - 你猜对了 - 初始化Funq

using System.Configuration; // Don't forget to ref System.Configuration.dll
using Funq;     
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.SqlServer;

namespace MyApp.Utilities
{
    public static class FunqExtensions
    {
        public static void Init(this Container container)
        {
            //-------------------------------------------------------
            // NB - I don't particularly like AutoWiring the public properties. 
            // Usually I want private stuff in the constructor)
            //-------------------------------------------------------
            var sqlServerConnectionString = ConfigurationManager.ConnectionStrings["HowdyCS"];
            container.Register<IDbConnectionFactory>(
                c => new OrmLiteConnectionFactory(
                    sqlServerConnectionString, 
                    SqlServerOrmLiteDialectProvider.Instance));

            container.Register<SomeForm>(
                c => new SomeForm(
                    c.Resolve<IDbConnectionFactory>()
                )
            ).ReusedWithin(ReuseScope.None);

        }
    }
}

I like using the lamda in the registration - it defers the construction of objects until they get resolved, rather than at registration time.
By default, the container stores the resolved object as a singleton, but if you have something that needs to be initialized every time it gets used ( ie user controls or winforms ) then use the .ReusedWithin(ReuseScope.None) extension.

我喜欢在注册中使用lamda - 它会延迟对象的构造,直到它们得到解决,而不是在注册时。默认情况下,容器将已解析的对象存储为单例,但如果每次使用时都需要初始化某些内容(即用户控件或winforms),则使用.ReusedWithin(ReuseScope.None)扩展。

Where I need my SomeForm (ie in a button click or whatever)

我需要我的SomeForm(即点击按钮或其他)

...
private void btnOpenSomeForm_Click(object sender, EventArgs e)
{
    var myForm = Program.FunqContainer.Resolve<SomeForm>();
    myForm.Show();
}

Check http://blogs.clariusconsulting.net/kzu/mab-containermodel-funq-a-transparent-container/ for more info

查看http://blogs.clariusconsulting.net/kzu/mab-containermodel-funq-a-transparent-container/了解更多信息

As an aside, this also works for VB.net when you put it through http://converter.telerik.com/

顺便说一下,当你通过http://converter.telerik.com/发布时,这也适用于VB.net

#2


6  

ServiceStack includes an enhanced version of Funq (e.g. with AutoWiring support) that's self-contained in the core ServiceStack.dll.

ServiceStack包含一个增强版的Funq(例如,具有AutoWiring支持),它是自包含在核心ServiceStack.dll中的。

Unfortunately at this time the ServiceStack.dll is contained in the ServiceStack NuGet package which brings in other ServiceStack server dependencies. You can build it from src or cherry pick from the NuGet package just the dlls you need, i.e:

不幸的是,此时ServiceStack.dll包含在ServiceStack NuGet包中,该包引入了其他ServiceStack服务器依赖项。您可以从NuGet包中的src或cherry pick中构建它,只需要你需要的dll,即:

  • ServiceStack.dll
  • ServiceStack.Common.dll
  • ServiceStack.Interfaces.dll
  • ServiceStack.Text.dll

#1


3  

I'm in a similar position, wanting to use heaps of the ServiceStack tools in a non-webby project. I agree that there is a ... slight lack in documentation for Funq

我处于类似的位置,希望在非webby项目中使用大量的ServiceStack工具。我同意Funq的文档略有缺失

I have been using it in a legacy WinForms app, trying to avoid changing the original project (too much) and I add the new forms to a new project.
I added references to most of the ServiceStack libraries to most of my projects (manually because I'm doing this in .Net 3.5)

我一直在传统的WinForms应用程序中使用它,试图避免更改原始项目(太多)并将新表单添加到新项目中。我在大多数项目中添加了对大多数ServiceStack库的引用(手动,因为我在.Net 3.5中这样做)

Here is the code in the winforms Program.cs file; Note that the FunqContainer is a public static property - I'm still not sure about that, but it gives access across the whole project to the FunqContainer

这是winforms Program.cs文件中的代码;请注意,FunqContainer是一个公共静态属性 - 我仍然不确定,但它允许整个项目访问FunqContainer

using System;
using System.Threading;
using System.Windows.Forms;

using Funq;
using MyApp.Utilities;

static class Program
    {
        public static Funq.Container FunqContainer { get; set; }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            FunqContainer = new Container();
            FunqContainer.Init(); 

            etc...
        }
    }

FunqContainer.Init() is an extension method in my separate project for - you guessed it - initializing Funq

FunqContainer.Init()是我单独项目中的扩展方法 - 你猜对了 - 初始化Funq

using System.Configuration; // Don't forget to ref System.Configuration.dll
using Funq;     
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.SqlServer;

namespace MyApp.Utilities
{
    public static class FunqExtensions
    {
        public static void Init(this Container container)
        {
            //-------------------------------------------------------
            // NB - I don't particularly like AutoWiring the public properties. 
            // Usually I want private stuff in the constructor)
            //-------------------------------------------------------
            var sqlServerConnectionString = ConfigurationManager.ConnectionStrings["HowdyCS"];
            container.Register<IDbConnectionFactory>(
                c => new OrmLiteConnectionFactory(
                    sqlServerConnectionString, 
                    SqlServerOrmLiteDialectProvider.Instance));

            container.Register<SomeForm>(
                c => new SomeForm(
                    c.Resolve<IDbConnectionFactory>()
                )
            ).ReusedWithin(ReuseScope.None);

        }
    }
}

I like using the lamda in the registration - it defers the construction of objects until they get resolved, rather than at registration time.
By default, the container stores the resolved object as a singleton, but if you have something that needs to be initialized every time it gets used ( ie user controls or winforms ) then use the .ReusedWithin(ReuseScope.None) extension.

我喜欢在注册中使用lamda - 它会延迟对象的构造,直到它们得到解决,而不是在注册时。默认情况下,容器将已解析的对象存储为单例,但如果每次使用时都需要初始化某些内容(即用户控件或winforms),则使用.ReusedWithin(ReuseScope.None)扩展。

Where I need my SomeForm (ie in a button click or whatever)

我需要我的SomeForm(即点击按钮或其他)

...
private void btnOpenSomeForm_Click(object sender, EventArgs e)
{
    var myForm = Program.FunqContainer.Resolve<SomeForm>();
    myForm.Show();
}

Check http://blogs.clariusconsulting.net/kzu/mab-containermodel-funq-a-transparent-container/ for more info

查看http://blogs.clariusconsulting.net/kzu/mab-containermodel-funq-a-transparent-container/了解更多信息

As an aside, this also works for VB.net when you put it through http://converter.telerik.com/

顺便说一下,当你通过http://converter.telerik.com/发布时,这也适用于VB.net

#2


6  

ServiceStack includes an enhanced version of Funq (e.g. with AutoWiring support) that's self-contained in the core ServiceStack.dll.

ServiceStack包含一个增强版的Funq(例如,具有AutoWiring支持),它是自包含在核心ServiceStack.dll中的。

Unfortunately at this time the ServiceStack.dll is contained in the ServiceStack NuGet package which brings in other ServiceStack server dependencies. You can build it from src or cherry pick from the NuGet package just the dlls you need, i.e:

不幸的是,此时ServiceStack.dll包含在ServiceStack NuGet包中,该包引入了其他ServiceStack服务器依赖项。您可以从NuGet包中的src或cherry pick中构建它,只需要你需要的dll,即:

  • ServiceStack.dll
  • ServiceStack.Common.dll
  • ServiceStack.Interfaces.dll
  • ServiceStack.Text.dll