小编Joh*_*ohn的帖子

MongoDB删除每个数据库

我想知道是否有命令从MongoDB中删除每个数据库?

我知道如果我只想删除一个数据表,我只需要输入数据库的名称,如下面的代码,但我不想指定它.

mongo DB_NAME --eval 'db.dropDatabase();'
Run Code Online (Sandbox Code Playgroud)

mongodb

88
推荐指数
6
解决办法
5万
查看次数

无法掌握Freeze/Inject/Register之间的区别

在开始之前,我是AutoFixture的忠实粉丝,我仍然在学习如何使用该工具.所以感谢开发了Autofixture先生Ploeh和所有贡献者.

那么让我们从我的问题开始吧.

根据 AutoFixture/AutoMoq忽略注入实例/冻结模拟

上面链接的有趣部分是给出了这段代码

Mock<ISettings> settingsMock = new Mock<ISettings>();
settingsMock.Setup(s => s.Get(settingKey)).Returns(xmlString);

ISettings settings = settingsMock.Object;
fixture.Inject(settings);
Run Code Online (Sandbox Code Playgroud)

马克回答哪个可以重写

fixture.Freeze<Mock<ISettings>>()
       .Setup(s => s.Get(settingKey)).Returns(xmlString);
Run Code Online (Sandbox Code Playgroud)

它看起来像一个语法糖,使用Freeze方法是一种在流畅的界面中编写模拟,配置和自动混合容器中的注入的方法.

在对网络进行一些研究之后,Freeze和Inject之间实际上存在功能差异.我发现了这个问题:https: //github.com/AutoFixture/AutoFixture/issues/59 ,它指出了 如何在AutoFixture中冻结空实例的答案

上面链接的作者描述了Freeze方法如下:

在内部,Freeze创建一个所请求类型的实例(例如IPayPalConfiguration)然后注入它,这样当你再次请求它时它将始终返回该实例

我明白我们这样做的时候

var customer = fixture.Freeze<Order>();
Run Code Online (Sandbox Code Playgroud)

每当我们的代码请求Order类型时,它将始终使用相同的Order实例.但是如果我在Freeze构造函数中指定我希望它使用特定的实例呢?

这是一个小代码示例:

[Fact]
public void MethodeName()
{
    var fixture = new Fixture().Customize(new AutoMoqCustomization());
    fixture.Freeze<OrderLine>(new OrderLine("Foo"));
    var order = fixture.Create<Order>();
}

public class Order
{
    private readonly OrderLine _line;

    public Order(OrderLine line)
    {
        _line = line;
    }
}
public class OrderLine
{
    private readonly …
Run Code Online (Sandbox Code Playgroud)

c# autofixture

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

如何在执行Task.WhenAny时产生返回项

我的解决方案中有两个项目:WPF项目和类库.

在我的类库中:

我有一个符号列表:

class Symbol
{
     Identifier Identifier {get;set;}
     List<Quote> HistoricalQuotes {get;set;}
     List<Financial> HistoricalFinancials {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

对于每个符号,我查询金融服务以使用webrequest检索每个符号的历史财务数据.(webClient.DownloadStringTaskAsync(URI))

所以这是我的方法:

    public async Task<IEnumerable<Symbol>> GetSymbolsAsync()
    {
        var historicalFinancialTask = new List<Task<HistoricalFinancialResult>>();

        foreach (var symbol in await _listSymbols)
        {
            historicalFinancialTask.Add(GetFinancialsQueryAsync(symbol));
        }

        while (historicalFinancialTask.Count > 0)
        {
            var historicalFinancial = await Task.WhenAny(historicalFinancialTask);
            historicalFinancialTask.Remove(historicalFinancial);

            // the line below doesn't compile, which is understandable because method's return type is a Task of something
            yield return new Symbol(historicalFinancial.Result.Symbol.Identifier, historicalFinancial.Result.Symbol.HistoricalQuotes, historicalFinancial.Result.Data); 
        }
    }

    private async Task<HistoricalFinancialResult> GetFinancialsQueryAsync(Symbol symbol)
    { …
Run Code Online (Sandbox Code Playgroud)

c# async-await c#-5.0

27
推荐指数
2
解决办法
7771
查看次数

MongoDB如何检查是否存在

我想知道如何使用mongoDB和C#检查对象的存在.

我找到了一种方法,但是由于Any()方法,我不得不使用Linq,但是我想知道如果没有Linq可以做到这一点吗?

database.GetCollection<ApplicationViewModel>("Applications").Find(Query.EQ("Name", applicationName)).Any()
Run Code Online (Sandbox Code Playgroud)

多谢你们!

.net c# mongodb mongodb-.net-driver

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

带有异步初始化的 Lazy&lt;Task&lt;T&gt;&gt;

class Laziness
{
    static string cmdText = null;
    static SqlConnection conn = null;

 
    Lazy<Task<Person>> person =
        new Lazy<Task<Person>>(async () =>      
        {
            using (var cmd = new SqlCommand(cmdText, conn))
            using (var reader = await cmd.ExecuteReaderAsync())
            {
                if (await reader.ReadAsync())
                {
                    string firstName = reader["first_name"].ToString();
                    string lastName = reader["last_name"].ToString();
                    return new Person(firstName, lastName);
                }
            }
            throw new Exception("Failed to fetch Person");
        });

    public async Task<Person> FetchPerson()
    {
        return await person.Value;              
    }
}
Run Code Online (Sandbox Code Playgroud)

Riccardo Terrell 于 2018 年 6 月出版的《.NET 中的并发》一书说道: …

c# asynchronous lazy-evaluation async-await

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

如何使用Autofixture生成字典

对于列表,我们可以做到

fixture.CreateMany<List<string>>(1000); // with 1000 elements
Run Code Online (Sandbox Code Playgroud)

但如何用字典做呢?并且能够指定要生成的元素数量

c# dictionary autofixture

12
推荐指数
1
解决办法
1605
查看次数

如何获得数字范围

我有一个数字间隔[1,20].

如果我决定禁止范围[15,18],我想要一种能够返回数量范围的方法.我的方法应该给我一个包含[1,15]和[18,20]的列表

Range对象可能看起来像那样

public class Range
{
     int Start {get;set;}
     int End {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

c#

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

CQRS和服务总线

CQRS是关于分离命令和查询.我们可以使用Event Sourcing,DDD,NoSQL等几种模式和技术轻松添加它......但ServiceBus是强制性的吗?

cqrs

9
推荐指数
1
解决办法
4762
查看次数

如何在我的属性中设置动态值

我想将动态变量作为参数传递给我的属性.在这里,我想使用Environment.MachineName,请参阅下面的代码:

public interface IMonitoringViewModelConfiguration : IConfigurationContainer
{
    [ConfigurationKey("MonitoringService", Environment.MachineName)]
    string ConnectionString { get; }
}
Run Code Online (Sandbox Code Playgroud)

但我收到此错误:错误1属性参数必须是属性参数类型Abc.ServiceBus.Monitoring.ViewModel的常量表达式,typeof表达式或数组创建表达式

为了传递我的Environment.MachineName,是否有尽可能干净的解决方法?

谢谢.

约翰

PS:我发现了一些谈论这个案子的文章,但它已经写成2 - 3年前了.但今天,来自.NET 4.0的clr能提供一些不错的解决方案吗?

c# attributes

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

CQRS验证和唯一性

在我的CQRS架构中,我想验证当我发送一个InsertSettingCommand(设置是一个键/值对象)时,我想检查我的数据库中是否存在该键.如果我很好地理解CQRS和验证,它说只有当验证某些格式化内容(例如检查电子邮件是否遵循某些语法或客户的名称是否为空)时,才应在客户端执行验证.但在我的情况下,我需要查询我的数据库以查看它是否存在,但我不知道在客户端查询我的读取存储是否正确?或者我应该在我的域名中调用我的阅读商店?然后抛出一个InsertSettingDuplicated事件?

那么在CQRS环境中我最好的方法是什么呢?有人谈论补偿行动吗?它能帮助我吗?

谢谢.

c# validation cqrs

7
推荐指数
2
解决办法
1400
查看次数