假设我有类:
public class Order
{
int OrderId {get; set;}
string CustomerName {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
我也在下面声明了变量
Func<Order, bool> predicate1 = t=>t.OrderId == 5 ;
Func<Order, bool> predicate2 = t=>t.CustomerName == "Ali";
Run Code Online (Sandbox Code Playgroud)
是否有任何方法可以连接这些变量(使用AND/OR)并将结果放在第三个变量中?例如:
Func<Order, bool> predicate3 = predicate1 and predicate2;
Run Code Online (Sandbox Code Playgroud)
要么
Func<Order, bool> predicate3 = predicate1 or predicate2;
Run Code Online (Sandbox Code Playgroud) 我有一个Angular应用程序.它的工作正常,但随着我的应用程序越来越大,我担心每个控制器都需要注入大量的依赖项.
例如
app.controller('viewapps',[
'$scope','Appfactory','Menu','$timeout','filterFilter','Notice', '$routeParams',
function($scope,Appfactory,Menu,$timeout,filterFilter,Notice,$routeParams) {
//controller code..
}])
Run Code Online (Sandbox Code Playgroud)
我确信将来会依赖列表.我在这里做错了吗?这是正确的方法吗?有效处理这个问题的最佳方法是什么?
我试图将依赖注入到自定义中AuthorizeAttribute
,如下所示:
public class UserCanAccessArea : AuthorizeAttribute
{
readonly IPermissionService permissionService;
public UserCanAccessArea() :
this(DependencyResolver.Current.GetService<IPermissionService>()) { }
public UserCanAccessArea(IPermissionService permissionService)
{
this.permissionService = permissionService;
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
string AreaID =
httpContext.Request.RequestContext.RouteData.Values["AreaID"] as string;
bool isAuthorized = false;
if (base.AuthorizeCore(httpContext))
isAuthorized = permissionService.UserCanAccessArea(AreaID, httpContext.User);
return isAuthorized;
}
}
Run Code Online (Sandbox Code Playgroud)
这有效,但似乎是作为一个单身人士解决,这意味着我得到了我以前的问题中描述的问题
我想要做的是使用属性注入但由于我的属性本身没有被Unity解决,我无法找到一种方法来配置容器来拦截和解析属性.我尝试过以下方法:
public class UserCanAccessArea : AuthorizeAttribute
{
public IPermissionService permissionService { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
string AreaID =
httpContext.Request.RequestContext.RouteData.Values["AreaID"] …
Run Code Online (Sandbox Code Playgroud) c# authentication asp.net-mvc dependency-injection unity-container
对于这个长期的问题提前抱歉,很长一段时间因为我整天都在挖掘这个问题.
我有一个ASP.Net MVC2应用程序,其中包含以下项目:MyApp.Web,MyApp.Services,MyApp.Data.
我们编写接口代码并将Ninject 2用于DI/IoC.
但是,我对打字(并忘记打字)非常厌倦:
Bind<ISomeService>.To<SomeService>;
Run Code Online (Sandbox Code Playgroud)
所以,了解Ninject.Extensions.Convensions,我试图用它来自动扫描和注册模块和IXxxx => Xxxx类型的简单依赖项.
我可以使用以下代码来设置Ninject,并且所有内容似乎都按预期连接.
public static IKernel Initialize()
{
var kernel = new StandardKernel();
kernel.Scan(a => {
a.FromAssemblyContaining<MyApp.Data.SomeDataClass>();
a.FromAssemblyContaining<MyApp.Services.SomeServiceClass>();
a.AutoLoadModules();
a.BindWithDefaultConventions();
a.InTransientScope();
});
return kernel;
}
Run Code Online (Sandbox Code Playgroud)
但是......我想以一种我认为支持的方式进一步采取这一点,但我似乎无法让它发挥作用.
由于我们的MyApp.Web项目根本没有使用MyApp.Data(直接),我试图避免引用MyApp.Data.使用上面的代码,我必须从MyApp.Web引用MyApp.Data,因为对SomeDataClass的编译时引用.
我更愿意为Ninject 指定一个程序集的名称进行扫描和注册.看来,Conventions扩展通过带有字符串(或可枚举的字符串)的From重载来支持这一点.
所以,我在From重载上尝试了几种变体:
public static IKernel Initialize()
{
var kernel = new StandardKernel();
kernel.Scan(a => {
a.From("MyApp.Data");
a.From("MyApp.Services.dll");
a.From("AnotherDependency, Version=1.0.0.0, PublicKeyToken=null"); //etc., etc. with the From(...)'s
a.AutoLoadModules();
a.BindWithDefaultConventions();
a.InTransientScope();
});
return kernel;
} …
Run Code Online (Sandbox Code Playgroud) 在ASP.NET MVC 5应用程序上,我有以下StructureMap配置:
cfg.For(typeof(IRequestHandler<,>)).DecorateAllWith(typeof(MediatorPipeline<,>));
Run Code Online (Sandbox Code Playgroud)
有谁知道如何使用ASP.NET Core IOC进行此配置?
我使用Resharper 5.1代码分析很多次我从resharper得到一个评论
"通过匿名代表取消订阅"
#Part of Code
if (((bool)e.NewValue))
{
listView.PreviewTextInput += (o,args) =>
listView_PreviewTextInput(o,args,listView);
}
else
{
listView.PreviewTextInput -= (o, args) =>
listView_PreviewTextInput(o, args, listView);
}
Run Code Online (Sandbox Code Playgroud)
我怎么能纠正或优化这件事
我是Simple Injector IOC容器的新手.我将开始在一个项目中工作,该项目需要使用MVC 4 ASP.NET Web API的多租户ASP.NET MVC实现.
我的问题是:Simple injector支持MVC 4 ASP.NET Web API吗?喜欢读简单的注射器文档这使得引用MVC 3,我想知道,如果MVC 4还支持.
鉴于以下内容:
public interface ICommandHandler<in TCommand>
{
void Handle(TCommand command);
}
public class MoveCustomerCommand
{
}
public class MoveCustomerCommandHandler : ICommandHandler<MoveCustomerCommand>
{
public void Handle(MoveCustomerCommand command)
{
Console.WriteLine("MoveCustomerCommandHandler");
}
}
public class TransactionCommandHandlerDecorator<TCommand> : ICommandHandler<TCommand>
{
private readonly ICommandHandler<TCommand> _decorated;
public TransactionCommandHandlerDecorator(ICommandHandler<TCommand> decorated)
{
_decorated = decorated;
}
public void Handle(TCommand command)
{
Console.WriteLine("TransactionCommandHandlerDecorator - before");
_decorated.Handle(command);
Console.WriteLine("TransactionCommandHandlerDecorator - after");
}
}
public class DeadlockRetryCommandHandlerDecorator<TCommand> : ICommandHandler<TCommand>
{
private readonly ICommandHandler<TCommand> _decorated;
public DeadlockRetryCommandHandlerDecorator(ICommandHandler<TCommand> decorated)
{
_decorated = decorated;
}
public …
Run Code Online (Sandbox Code Playgroud) 新的依赖注入,所以这可能是一个简单的问题,但我已经尝试过,无法弄清楚,我正在使用Simple Injector.
我有一个完全使用SimpleInjector的WebApi,现在我想使用OAuth实现安全性.
为此,我开始学习本教程,这非常有用,但不使用Dependancy Injection
http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/
我的global.asax文件看起来像这样,设置依赖注入(完美工作)
protected void Application_Start()
{
SimpleInjectorConfig.Register();
GlobalConfiguration.Configure(WebApiConfig.Register);
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个Startup.Auth.cs文件来配置OAuth
public class Startup
{
public void Configuration(IAppBuilder app)
{
var OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new MyAuthorizationServerProvider() // here is the problem
};
// Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
}
Run Code Online (Sandbox Code Playgroud)
现在我在上面评论过,MyAuthorizationServerProvider就是问题所在.它需要我通常注入的IUserService参数.我不想清空构造函数,因为我的IUserService也注入了一个存储库.这是文件
public class ApiAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
private IUserService _service;
public ApiAuthorizationServerProvider (IUserService service)
{
_service = service;
}
public override async …
Run Code Online (Sandbox Code Playgroud) 我只是好奇.我想学习c#团结.我是宿舍学生,我的个人电脑是macbook air,我们有一个公共电脑室.我只是问我是否可以在mac上使用c#.