小编Jon*_*ear的帖子

AutoFac Injection into属性

所以我需要在我正在使用的授权属性中注入许多不同的服务.为简单起见,我将留下这个以显示配置管理器.

public class FeatureAuthorizeAttribute : AuthorizeAttribute
{
    public IConfigurationManager ConfigurationManager;

    private readonly string _feature;
    public FeatureAuthorizeAttribute(string feature)
    {
        _feature = feature;

        var test  = ConfigurationManager.GetCdnPath();
    }
}
Run Code Online (Sandbox Code Playgroud)

将使用如下

[FeatureAuthorize("Admin")]
Run Code Online (Sandbox Code Playgroud)

我试过使用构造函数注入

public FeatureAuthorizeAttribute(string feature, IConfigurationManager configurationManager)
{
   ConfigurationManager = configurationManager;
   _feature = feature
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试时,这只会导致错误

[FeatureAuthorize("Admin", IConfigurationManager)]
Run Code Online (Sandbox Code Playgroud)

首先,这似乎是错误的方式.我假设我需要在容器中注册我的自定义授权属性,以使其开始接收

dependency-injection autofac

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

实体框架LINQ多列计数(不同)

我不完全确定如何将以下SQL转换为LINQ Fluent API.任何帮助都会很棒

select count(distinct thread.Id) as ThreadCount, count(distinct segment.Id) as SegmentCount
from Segment 
inner join SegmentCommunicator as SegmentCommunicator
    on Segment.Id = SegmentCommunicator.SegmentId
inner join Thread
    on Thread.Id = Segment.ThreadId
where SegmentCommunicator.CommunicatorId in (94, 3540, 6226, 10767, 20945)
Run Code Online (Sandbox Code Playgroud)

目前,我知道如何在2个查询中执行此操作,但对于我的生活,我无法弄清楚如何缩减成一个.任何帮助将非常感激

        var aggregate1 = _threadProvider
                .AsQueryable()
                .SelectMany(t => t.Segments)
                .Where(s => s.SegmentCommunicators.Any(sc => communicatorIds.Contains(sc.CommunicatorId)))
                .Select(s => s.ThreadId)
                .Distinct()
                .Count();

        var aggregate2 = _threadProvider
            .AsQueryable()
            .SelectMany(t => t.Segments)
            .Where(s => s.SegmentCommunicators.Any(sc => communicatorIds.Contains(sc.CommunicatorId)))
            .Select(s => s.Id)
            .Distinct()
            .Count();
Run Code Online (Sandbox Code Playgroud)

linq linq-to-sql

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

自动运行方法的 C# 属性

我不确定这是否完全可能。但我想做的是创建一个属性,当我调用 run 方法时,然后运行具有特定 run 属性的所有方法。我意识到这可以通过委托来完成,但我觉得如果可以通过属性来实现,看起来可能会更干净一些。我应该指出,运行顺序并不重要。

基本设计:

//This is the method called that should start off the attribute chain
public void Run(){
  //calling logic in here
}

[AutomatedRun]
private void Method1(){

}

[AutomatedRun]
private void Method2(){

}
Run Code Online (Sandbox Code Playgroud)

custom-attributes c#-4.0

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