enr*_*ein 12 .net servicestack funq
在工作中,我们在ServiceStack中进行了几个新的Web服务项目,并在其中一些项目中利用了Funq.我目前正在开发一个单独的项目,它将使用所述Web服务,并且想知道我是否有办法在我的项目中使用ServiceStack的Funq来解析我的依赖关系,以便使用我们正在使用的或多或少相同的模式开发我们的Web服务.
这可能吗?
ServiceStack包含一个增强版的Funq(例如,具有AutoWiring支持),它在核心中是独立的ServiceStack.dll.
不幸的是,此时ServiceStack.dll它包含在ServiceStack NuGet包中,该包引入了其他ServiceStack服务器依赖项.您可以从NuCet包中的src或cherry pick中构建它,只需要你需要的dll,即:
小智 3
我处于类似的位置,希望在非 webby 项目中使用大量 ServiceStack 工具。我同意 Funq 的文档略有缺乏
我一直在旧版 WinForms 应用程序中使用它,试图避免更改原始项目(太多),并将新表单添加到新项目中。
我向我的大多数项目添加了对大多数 ServiceStack 库的引用(手动,因为我在 .Net 3.5 中执行此操作)
这是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...
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我喜欢在注册中使用 lamda - 它推迟对象的构造,直到它们得到解决,而不是在注册时。
默认情况下,容器将解析的对象存储为单例,但如果您有每次使用时都需要初始化的对象(即用户控件或 winforms ),则使用扩展.ReusedWithin(ReuseScope.None)。
我需要的地方SomeForm(即单击按钮或其他方式)
...
private void btnOpenSomeForm_Click(object sender, EventArgs e)
{
var myForm = Program.FunqContainer.Resolve<SomeForm>();
myForm.Show();
}
Run Code Online (Sandbox Code Playgroud)
检查http://blogs.clariusconsulting.net/kzu/mab-containermodel-funq-a-transparent-container/了解更多信息
顺便说一句,当您通过http://converter.telerik.com/访问 VB.net 时,这也适用于 VB.net