12

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

这可能吗?

4

2 回答 2

6

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

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

  • 服务栈.dll
  • ServiceStack.Common.dll
  • ServiceStack.Interfaces.dll
  • ServiceStack.Text.dll
于 2013-02-05T17:38:21.097 回答
3

我处于类似的位置,想在非 webby 项目中使用大量的 ServiceStack 工具。我同意…… Funq 的文档略有不足

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

这是winformsProgram.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()是我单独项目中的一个扩展方法——你猜对了——初始化 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);

        }
    }
}

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

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

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

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

顺便说一句,当您通过http://converter.telerik.com/放置它时,这也适用于 VB.net

于 2013-04-19T03:14:34.870 回答