我们正在使用ASP.NET MVC,Entity Framework Code First和即将发布的SQL Azure Federations构建一个多租户数据库应用程序.我们的存储库接受DbContext作为使用Ninject注入的构造函数参数.问题是DbContext有一个int构造函数参数,用于指定将用于使用该上下文发出的请求的帐户ID.像这样的东西:
public class DatabaseContext : DbContext
{
public DatabaseContext(int accountId)
{
// Code here to ensure we only work with data associated with
// the account identified by accountId
}
}
Run Code Online (Sandbox Code Playgroud)
帐户ID的值将存储在Session变量中.问题是当我们创建一个新的DatabaseContext实例时,我们如何让Ninject将一个值从Session注入到构造函数中?
或者我还有另一种方法可以将帐户ID注入DbContext吗?
简单案例:我有一个用于记录消息的接口,如下所示:
public interface ILogger
{
void Log(string message);
}
Run Code Online (Sandbox Code Playgroud)
也许有三个不同的类实现了这个接口.
现在,我可以在一个地方写一行,换行,比如:
kernel.Bind<ILogger>().To<ConsoleLogger>();
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何在许多类中使用该接口,但不通过构造函数注入每个人.因为我们可以使用这么多不同的接口,并且该类构造函数的声明可能很混乱.
我今天开始了一个新项目并且去了nuget以获得ninject.
我下载了Ninject MVC3插件,该插件应该下载我需要的所有内容.
然后,我从一个不同的项目中获取了一些旧代码,并注意到InRequestScope似乎已经消失了.
它被拿走了还是我错过了什么?
编辑
我想我在"Ninject.Web.Common"下找到了它
我对单元测试很新,并且正在使用xUnit和AutoFixture进行一些实验.
这是我要测试的类的构造函数:
public PokerClientIniGenerateCommand(
Func<TextWriter> writerCreator,
FranchiseInfo franchise)
{
// some code here
}
Run Code Online (Sandbox Code Playgroud)
我这样做:
public abstract class behaves_like_poker_client_ini_generate_command : Specification
{
protected PokerClientIniGenerateCommand commandFixture;
protected behaves_like_poker_client_ini_generate_command()
{
var fixture = new Fixture();
commandFixture = fixture.Create<PokerClientIniGenerateCommand>();
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何设置构造函数参数(主要是第一个参数--func部分).
在我的业务逻辑中,我像这样实例化这个类:
new PokerClientIniGenerateCommand(
() => new StreamWriter(PokerClientIniWriter),
franchise));
Run Code Online (Sandbox Code Playgroud)
所以在我的测试中我应该像这样调用func:
() => new StringWriter(PokerClientIniWriter)
Run Code Online (Sandbox Code Playgroud)
但是如何通过AutoFixture设置它.任何帮助将举例如将非常感谢.
考虑以下测试,
[Theory, MyConventions]
public void GetClientExtensionReturnsCorrectValue(BuilderStrategy sut)
{
var expected = ""; // <--??? the value injected into BuilderStrategy
var actual = sut.GetClientExtension();
Assert.Equal(expected, actual);
}
Run Code Online (Sandbox Code Playgroud)
和我正在使用的自定义属性:
public class MyConventionsAttribute : AutoDataAttribute {
public MyConventionsAttribute()
: base(new Fixture().Customize(new AutoMoqCustomization())) {}
}
Run Code Online (Sandbox Code Playgroud)
和SUT:
class BuilderStrategy {
private readonly string _clientID;
private readonly IDependency _dependency;
public void BuilderStrategy(string clientID, IDependency dependency) {
_clientID = clientID;
_dependency = dependency;
}
public string GetClientExtension() {
return _clientID.Substring(_clientID.LastIndexOf("-") + 1);
}
}
Run Code Online (Sandbox Code Playgroud)
我需要知道在构造函数参数中注入了什么值,clientID以便我可以使用它来与输出进行比较 …
我是F#语言的新手,我的专业背景主要是C#/ SharePoint,最近我去了Haskell课程,这是一门可爱的功能语言.
我的问题是关于类型别名(同义词)和函数签名的使用,在Haskell中是这样做的好的和直接的方式:
type Person = (String,Int)
type Address = (Person, String,Int)
getPerson :: Address -> Person
getPerson n = first n ...
Run Code Online (Sandbox Code Playgroud)
当我在F#中尝试过同样的方法时,我有点失败:
type Person = (int,int)
type Address = (Person, String, Int)
let getPerson (n:Address) =
n.first ...
Run Code Online (Sandbox Code Playgroud)
我做错了什么?或者当我使用signature(int,int) - > String - > int - > String - >(int,int)函数时,提高可读性的最佳做法是什么?
下面的解决方案等同于上面提到的Haskell类型同义词:
type Person = int*int
type Address = Person * string * int
let first (n,_,_) = n
let getPerson (n:Address) : Person =
first n
Run Code Online (Sandbox Code Playgroud) 我正在使用OpenTK及其数学库,但不幸的是,矢量类没有通用接口.例如,Vector2,3和4都具有相同的静态方法SizeInBytes http://www.opentk.com/files/doc/struct_open_t_k_1_1_vector3.html#ae7cbee02af524095ee72226d842c6892
现在我可以重载大量不同的构造函数,但我认为应该可以通过类型约束来解决这个问题.
我正在阅读http://msdn.microsoft.com/en-us/library/dd233203.aspx,我发现了这个
type Class4<'T when 'T : (static member staticMethod1 : unit -> 'T) > =
class end
Run Code Online (Sandbox Code Playgroud)
现在我自己尝试了但是我无法正确使用语法.
type Foo<'T when 'T: (static member SizeInBytes: unit -> int)>(data: 'T []) =
member this.GetBytes() = 'T.SizeInBytes()
let f = Foo([|new Vector3(1.0f,1.0f,1.0f)|])
f.GetBytes()
Run Code Online (Sandbox Code Playgroud)
你能发现问题吗?
编辑:VS2012抱怨这条线'T.SizeInBytes() //Unexpected symbol or expression,T.SizeInBytes()也不起作用.
EDIT2:
我做了一个不涉及外部库的例子
type Bar() =
static member Print() = printf "Hello Foo"
type Foo<'T when 'T: (static member Print: unit -> unit)>(data: 'T …Run Code Online (Sandbox Code Playgroud) 我正在尝试在F#中创建一个函数,它将某些类型转换为字符串,而不是其他类型.目标是可以传递原语,但不能偶然传递复杂的对象.这是我到目前为止所拥有的:
type Conversions =
static member Convert (value:int) =
value.ToString()
static member Convert (value:bool) =
value.ToString()
let inline convHelper< ^t, ^v when ^t : (static member Convert : ^v -> string) > (value:^v) =
( ^t : (static member Convert : ^v -> string) (value))
let inline conv (value:^v) = convHelper<Conversions, ^v>(value)
Run Code Online (Sandbox Code Playgroud)
不幸的是,我的conv函数得到以下编译时错误:
A unique overload for method 'Convert' could not be determined based on type information
prior to this program point. A type annotation may be needed. …Run Code Online (Sandbox Code Playgroud) 我正在为F#遇到一些麻烦,因为我正在学习.我有类似下一个代码的东西:
let A = [| [| [|"1";"Albert"|];[|"2";"Ben"|] |];[| [|"1";"Albert"|];[|"3";"Carl"|] |] |]
Run Code Online (Sandbox Code Playgroud)
(类型A: string[][][])
我正在尝试将A转换为:
let B = [| [|"1"; "Albert" |] ; [| "2"; "Ben"|] ; [| "3"; "Carl"|] |]
Run Code Online (Sandbox Code Playgroud)
(类型B: string[][])
我不知道该怎么做.我一直在尝试一些for和递归功能,但我没有得到它.
是否有某种形式的内置/术语我不知道它有点但不同的"组成"两个'a -> unit功能产生一个单一的功能; 例如:
let project event =
event |> logDirections
event |> stashDirections
let dispatch (batch:EncodedEventBatch) =
batch.chooseOfUnion () |> Seq.iter project
Run Code Online (Sandbox Code Playgroud)
可能成为:
let project = logDirections FOLLOWEDBY stashDirections
let dispatch (batch:EncodedEventBatch) =
batch.chooseOfUnion () |> Seq.iter project
Run Code Online (Sandbox Code Playgroud)
然后:
let dispatch (batch:EncodedEventBatch) =
batch.chooseOfUnion () |> Seq.iter (logDirections FOLLOWEDBY stashDirections)
Run Code Online (Sandbox Code Playgroud)
我想有人可能会比较它tee(如FSFFAP的铁路导向编程系列中提到的那样).
(它需要将相同的arg传递给两者并且我正在寻求按顺序运行它们而没有任何异常处理技巧问题等)
(我知道我可以做,let project fs arg = fs |> Seq.iter (fun f -> f arg)但我想知道是否有内置的和/或某种形式的组合库我不知道)
f# ×5
ninject ×3
autofixture ×2
c# ×2
.net ×1
action ×1
asp.net-mvc ×1
composition ×1
nuget ×1
oop ×1
session ×1
side-effects ×1
sorting ×1
unit-testing ×1
xunit ×1