小编ebb*_*ebb的帖子

C#+模拟服务层?

我刚刚开始使用Moq进行单元测试/模拟,并遇到了问题..

我有一个名为"CustomerService"的服务层,它有以下代码:

public interface ICustomerService
{
    Customer GetCustomerById(int id);
}

public class CustomerService : ICustomerService
{
    private IRepository<Customer> customerRepository;

    public CustomerService(IRepository<Customer> rep)
    {
        customerRepository = rep;
    }
    public Customer GetCustomerById(int id)
    {
        var customer = customerRepository.Get(x => x.CustomerId == id);

        if (customer == null)
            return null;

        return customer;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的存储库类是通用的,并且遵循:

public interface IRepository<T> : IDisposable where T : class
    {
        T Get(Expression<Func<T, bool>> predicate);
    }

    public class Repository<T> : IRepository<T> where T : class
    {
        private ObjectContext context;
        private …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing moq mocking

4
推荐指数
1
解决办法
1万
查看次数

Moq-模拟通用存储库

我有一个通用存储库,正在尝试将.Returns转换为表达式,但是它拒绝...我的代码如下:

public RepositoryTest()
{
    IList<MockObjectSet> mocks = new List<MockObjectSet>()
    {
        new MockObjectSet { FirstName = "Beta", LastName = "Alpha", Mobile = 12345678 },
        new MockObjectSet { FirstName = "Alpha", LastName = "Beta", Mobile = 87654321 }
    };

    var mockRepository = new Mock<IRepository<MockObjectSet>>();

    mockRepository.Setup(x => x.GetBy(It.IsAny<Expression<Func<MockObjectSet, bool>>>()))
        .Returns((Expression<Func<MockObjectSet, bool>> predicate) => mocks.Where(predicate).ToList());

}
Run Code Online (Sandbox Code Playgroud)

只是说

Delegate System.Func<System.Collections.Generic.IEnumerable<expWEBCRM.Tests.Repositories.MockObjectSet>> does not take 1 arguments

提前致谢!

c# unit-testing mocking

4
推荐指数
1
解决办法
4104
查看次数

F# - POCO类

嘿!我正在尝试用适当的F#写一个POCO课......但是出了点问题......

我想"翻译"到正确的F#的C#代码是:

public class MyTest
{
    [Key]
    public int ID { get; set; }

    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我最接近F#中的上述代码是这样的:

type Mytest() =

    let mutable _id : int = 0;
    let mutable _name : string = null;

    [<KeyAttribute>]
    member x.ID
        with public get() : int = _id
        and  public set(value) = _id <- value

    member x.Name 
        with public get() : string = _name
        and  public set value = _name <- value
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试访问F#版本的属性时,它只返回一个编译错误说

"根据此程序点之前的信息查找不确定类型的对象.在此程序点之前可能需要类型注释来约束对象的类型.这可能允许解析查找."

尝试获取属性的代码是我的存储库的一部分(我使用的是EF Code First).

module …
Run Code Online (Sandbox Code Playgroud)

f# poco ef-code-first

4
推荐指数
1
解决办法
644
查看次数

Ninject +"加载Ninject组件ICache时出错"

我刚刚从NuGet安装了新的Ninject.MVC3并试图让它在我的asp.net mvc 3应用程序中运行,但是我现在得到这个奇怪的错误,然后在浏览我的网站时:

[InvalidOperationException: Error loading Ninject component ICache
No such component has been registered in the kernel's component container.

Suggestions:
  1) If you have created a custom subclass for KernelBase, ensure that you have properly
     implemented the AddComponents() method.
  2) Ensure that you have not removed the component from the container via a call to RemoveAll().
  3) Ensure you have not accidentally created more than one kernel.
]
   Ninject.Components.ComponentContainer.Get(Type component) in d:\BuildAgent-01\work\b68efe9aafe8875e\src\Ninject\Components\ComponentContainer.cs:146
   Ninject.Components.ComponentContainer.Get() in d:\BuildAgent-01\work\b68efe9aafe8875e\src\Ninject\Components\ComponentContainer.cs:102
   Ninject.KernelBase.CreateContext(IRequest request, IBinding binding) …
Run Code Online (Sandbox Code Playgroud)

dependency-injection ninject

4
推荐指数
1
解决办法
4121
查看次数

我该如何申报管道?

我如何申报管道是否重要?我知道三种方式:

let hello name = "Hello " + name + "!"    
let solution1 = hello <| "Homer"
let solution2 = "Homer" |> hello
Run Code Online (Sandbox Code Playgroud)

你会选哪个?solution1或solution2 - 为什么?

f#

4
推荐指数
1
解决办法
340
查看次数

用无参数构造函数记录?

我正在尝试构建一个使用Entity Framework的Web应用程序(ASP.NET MVC3),而且我再一次碰壁.当尝试在视图中的集合上运行foreach循环时,它会抛出以下异常:

System.InvalidOperationException:类'GvG.Entities.News'没有无参数构造函数.

现在是我的问题,是否有可能以某种方式在我的记录类型上定义无参数构造函数?

我目前的记录类型如下:

type News = { 
    mutable ID:int; 
    mutable Author:string; 
    mutable Title:string; 
    mutable Content:string }
Run Code Online (Sandbox Code Playgroud)

我知道我可以创建一个带烘焙场等的课程,但这就是我想要避免的.

f# entity-framework asp.net-mvc-3

4
推荐指数
2
解决办法
1317
查看次数

F# - > Seq to Map

我正在尝试Categories从数据库中加载所有我,然后将它们映射到Map(字典?),但是当我使用以下代码时:

[<StructuralComparison>]
type Category = { 
    mutable Id: string; 
    Name: string; 
    SavePath: string;
    Tags: ResizeArray<Tag> }

let categories = session.Query<Category>() 
                    |> Seq.map(fun z -> (z,0)) 
                    |> Map.ofSeq
Run Code Online (Sandbox Code Playgroud)

它只是抛出一个错误,说:

struct,record或union类型'Category'具有'StructuralComparison'属性,但组件类型'ResizeArray'不满足'comparison'约束

我完全不知道该怎么做,所以任何帮助表示赞赏!

f#

4
推荐指数
2
解决办法
3269
查看次数

Desugared形式的"让步"

let (x = 0) in x * x翻译到底是什么?一个功能(fun x -> x * x) 0)?- 这是有道理的,因为let绑定是表达式 - 而表达式必须返回值(就像函数一样).

例:

let result1 = 
    (fun n1 -> (fun n2 -> 
    (fun n3 -> n1 + n2 + n3 ) 3) 2) 1

let result2 = 
    let n1 = 1 in
    let n2 = 2 in
    let n3 = 3 in
    n1 + n2 + n3

let result3 = 
    let n1 = 1
    let n2 = 2
    let n3 …
Run Code Online (Sandbox Code Playgroud)

f# functional-programming

4
推荐指数
1
解决办法
405
查看次数

如何读取xml文件中的键/值

我正在尝试构建一个读取 ASP.NET 项目的 web.config 文件的控制台项目。我需要从配置中读取一个值。我正在放置我想从 web.config 文件中读取的内容。

<appSettings>
  <add key="LogoFrmNumber" value="001"/>
  <add key="LogoFrmPeriod" value="01"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)

我想像读取常规 xml 文件一样读取 LogoFrmNumber 的值。我怎样才能读取该值。

这是我读取 web.config 的代码,但我被卡住了。

XDocument doc = XDocument.Load( "c://web.config" );

var values = doc.Descendants( "AppSettings" );

foreach ( var value in values )
{
     Console.WriteLine( value.Value );
}
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net console-application xml-parsing

3
推荐指数
1
解决办法
2万
查看次数

无法解析DNS(有时?)

给定一个并行请求10000个URL的同时请求100个URL的应用程序,对于其中的50-5000个,我将收到以下错误消息:

远程名称无法解析为“ www.url.com”

我了解该错误意味着DNS服务器无法解析该网址。但是,对于每次运行,无法解析的URL数量都会更改(范围从50到5000)。

我发出太多请求的速度太快了吗?而且我还能做到吗?-在功能更强大的服务器上运行相同的测试,结果表明仅10个网址无法解析-听起来更加现实。

进行并行请求的代码:

var semp = new SemaphoreSlim(100);
var uris = File.ReadAllLines(@"C:\urls.txt").Select(x => new Uri(x));

foreach(var uri in uris)
{
   Task.Run(async () =>
   {
      await semp.WaitAsync();
      var result = await Web.TryGetPage(uri); // Using HttpWebRequest
      semp.Release();
   });   
}
Run Code Online (Sandbox Code Playgroud)

c# windows dns multithreading

3
推荐指数
1
解决办法
1342
查看次数