小编Bas*_*sin的帖子

格式化规则在类成员声明之间有空行

Micrsoft为EditorConfig的EditorConfig .NET编码约定设置提供了大量编码设置

但是找不到创建规则的方法,这会建议开发人员在类成员声明之间添加空行.

// "Bad" style
public class Order
{
    private readonly IRepository _repository;
    private readonly IPriceCalculator _priceCalculator;
    public Order(IRepository repository, IPriceCalculator priceCalculator)
    {
        _repostitory = repository;
        _priceCalculator = priceCalculator;
    }
    public CopyFrom(Order originalOrder)
    {
        // Create new order
    }
    public Cancel(Customer customer)
    {
        // Cancel order
    }
}

// Good style
public class Order
{
    private readonly IRepository _repository;

    private readonly IPriceCalculator _priceCalculator;

    public Order(IRepository repository, IPriceCalculator priceCalculator)
    {
        _repostitory = repository;
        _priceCalculator = priceCalculator;
    }

    public CopyFrom(Order …
Run Code Online (Sandbox Code Playgroud)

.net c# visual-studio editorconfig

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

如何在ASP.NET Core中根据请求配置服务

在ASP.NET Core中,我们可以在启动期间注册所有依赖项,这些依赖项在应用程序启动时执行.然后注册的依赖项将被注入控制器构造函数中.

public class ReportController
{
    private IReportFactory _reportFactory;

    public ReportController(IReportFactory reportFactory)
    {
        _reportFactory = reportFactory;
    }

    public IActionResult Get()
    {
        vart report = _reportFactory.Create();
        return Ok(report);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想IReportFactory在当前请求中注入基于数据的不同实现(用户授权级别或与请求一起传递的查询字符串中的某些值).

问题:ASP.NET Core中是否有内置的抽象(中间件),我们可以注册另一个接口实现?

如果没有内置功能,可能的方法是什么?

更新 IReportFactory接口用作一个简单示例.实际上我在不同的地方注入了一堆低级接口.现在我希望根据请求数据注入那些低级接口的不同实现.

public class OrderController
{
    private IOrderService _orderService;

    public OrderController(IOrderService orderService)
    {
        _orderService = orderService;
    }

    public IActionResult Create()
    {
        var order = _orderService.Create();
        return Ok(order);
    }
}    

 public class OrderService
 {
    private OrderBuilder _orderBuilder;
    private IShippingService _shippingService; // This now have many …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection asp.net-core

5
推荐指数
2
解决办法
2955
查看次数

EF Core删除同一张表上的一对一关系

模型与自身具有可选关系

public class Item
{
    public Guid Id { get; set; }
    public string Description { get; set; }
    public Guid StockId { get; set; }

    // optionally reference to another item from different stock
    public Guid? OptionalItemId { get; set; }

    public virtual Item OptionalItem { get; set; }      
}
Run Code Online (Sandbox Code Playgroud)

在 DbContext 模型中配置如下:

protected override void OnModelCreating(ModelBuilder builder)
{
     builder.Entity<Item>().HasOne(item => item.OptionalItem)
                           .WithOne()
                           .HasForeignKey<Item>(item => item.OptionalItemId)
                           .HasPrincipalKey<Item>(item => item.Id)
                           .IsRequired(false)
}
Run Code Online (Sandbox Code Playgroud)

我想通过在Stock使用新项目更新之前删除现有项目来用新项目替换现有项目。

// Given Stock contains only …
Run Code Online (Sandbox Code Playgroud)

c# sql-server entity-framework-core .net-core ef-core-2.1

5
推荐指数
1
解决办法
2017
查看次数

如何将 Blazor 应用配置为仅针对经过身份验证的用户返回 index.html

我们现有的 ASP.NET Core 应用程序(.NET 5)使用 Angular 作为 UI 框架。
我们创建了一个 Blazor WASM 客户端库,希望与现有的 Angular 框架一起在此应用程序中使用。

以下文档是我们如何配置它的Startup.Configure方法,以便从 wwwroot 中的专用目录“blazor-app”“提供”balzor 应用程序。

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // ...
    app.UseBlazorFrameworkFiles("/blazor-app");
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
        endpoints.MapFallbackToFile("blazor-app/index.html");
    });
}
Run Code Online (Sandbox Code Playgroud)

我们如何配置 Blazor 应用程序,以便仅针对经过身份验证的用户返回 index.html?

例如这样的事情?

[Authorize]
public class ClientController
{
    public IActionResult ClientApp()
    {
        // returns Blazor app index.html
    }
}
      
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core blazor-webassembly

5
推荐指数
1
解决办法
5045
查看次数

当未找到记录时,对于不可为空的列返回 null

实体类型

public class Person
{
    public Guid Id { get; set; }

    public string Name { get; set; }

    public Guid TeamId { get; set; } // mandatory foreign key

    public virtual Team Team { get; set; } // navigation property

    // dozen other properties
}

public class Team
{
    public Guid Id { get; set; }

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

我有一个方法可以返回给定人员 ID 的团队 ID。方法的返回类型定义为Guid?. 可以为 Null,因为数据库中可能不存在给定的人员 ID。

public Task<Guid?> GetTeamIdFor(Guid personId)
{
    using …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-core

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

相同字符串值的两个实例不相等

格式化为货币样式字符串的数字返回的值不等于预期值。

const expected = `12,09 €`;
const formatted = 
    new Intl.NumberFormat(`de-De`, { style: `currency`, currency: `EUR` }).format(12.09);

expect(formatted).toEqual(expected); // Fail

expected === formatted; // false

// Logged values
console.log(`FORMATTED: type = ${typeof formatted}, value = '${actual}';`);
console.log(`EXPECTED: type = ${typeof expected}, value = '${expected}';`);
// FORMATTED: type = string, value = '12,09 €'; 
// EXPECTED: type = string, value = '12,09 €';
Run Code Online (Sandbox Code Playgroud)

new Intl.NumberFormat(`de-De`, { style: `currency`, currency: `EUR` }).format(12.09); 
// returns "12,09 €"

`12,09 €` === `12,09 €`; …
Run Code Online (Sandbox Code Playgroud)

javascript jasmine typescript

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

当具有不可为空值的字典没有匹配项时返回 null

我有一个值为整数类型的字典。当我使用 type 的属性之一填充我的类时Nullable<int>,我想根据给定的产品 ID 使用字典中的值填充此属性。

当给定的产品 ID 没有相应的值时,如何获得可为空类型?

public class OrderLine
{
    public int? AvailableQuantity { get; set; }
}

var selectedProductId = Guid.NewGuid();
var products = new Dictionary<Guid, int>
{
    { Guid.NewGuid(), 1 },
    { Guid.NewGuid(), 2 },
    { Guid.NewGuid(), 3 },
};

var result = new OrderLine
{
    Id = Guid.NewGuid(),
    ProductId = selectedProductId,
    AvailableQuantity = products.GetValueOrDefault(selectedProductId, default)
};
Run Code Online (Sandbox Code Playgroud)

上面的方法返回0而不是null

当我尝试时,编译器无法编译

AvailableQuantity = products.GetValueOrDefault(selectedProductId, default(int?))
Run Code Online (Sandbox Code Playgroud)

无法从用法推断方法“TValue System.Collections.Generic.CollectionExtensions.GetValueOrDefault(this IReadOnlyDictionary, TKey, TValue)”的类型参数。尝试明确指定类型参数。

我无法更改字典的类型。字典是一种广泛使用的返回类型方法。这是我们需要处理产品 id 不能在该字典中的第一种情况。 …

c# collections dictionary

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