我有这个基本的linq查询,我想从数据库中获取一个城市.问题是我的搜索字符串被修剪而没有我要求它.我尽可能地简化了它.例:
var firstCity =
from city in db.Cities
where city.City_Code == "LAS "
select city;
Run Code Online (Sandbox Code Playgroud)
city.City_Code是"LAS"而不是"LAS",它仍然以City_Code"LAS"获得城市.
我该如何解决这个问题?我也试过Equals,但结果是一样的.
我目前使用Autofac进行简单的构造函数注入而没有任何问题.但是,我想知道的是如何在运行时解决依赖关系.下面的示例显示了我们可以导出文档的多种方式.通过简单的构造函数注入,IExport的具体实现在运行时得到解决.但是,需要做的是从下拉列表中解析用户选择的IExport,这将在构造容器后发生.有没有我可以做到这一点的例子?
Public interface IExport
{
void Run(string content);
}
public class PDFformat : IExport
{
public void Run(string content)
{
// export in pdf format
}
}
public class HTMLformat : IExport
{
public void Run(string content)
{
// export in html format
}
}
public class RTFformat : IExport
{
public void Run(string content)
{
// export in rtf format
}
}
public class HomeController : Controller
{
IExport Export;
public HomeController(IExport export)
{
Export = export;
}
public …Run Code Online (Sandbox Code Playgroud) 嗨我开始在我的所有项目中使用Simple Injector DI容器,并且想要一些关于如何适应的建议是我的要求的强大功能.
我有几个命令处理程序装饰器,它将包装命令:
public class TransactionCommandHandlerDecorator<TCommand>
: ICommandHandler<TCommand>
{
private readonly ICommandHandler<TCommand> handlerToCall;
private readonly IUnitOfWork unitOfWork;
public TransactionCommandHandlerDecorator(
IUnitOfWork unitOfWork,
ICommandHandler<TCommand> decorated)
{
this.handlerToCall = decorated;
this.unitOfWork = unitOfWork;
}
public void Handle(TCommand command)
{
this.handlerToCall.Handle(command);
unitOfWork.Save();
}
}
Run Code Online (Sandbox Code Playgroud)
互斥装饰者:
public class TransactionCommandHandlerWithMutexDecorator<TCommand>
: ICommandHandler<TCommand>
{
private readonly ICommandHandler<TCommand> handlerToCall;
private readonly IUnitOfWork unitOfWork;
private static object Lock = new object();
public TransactionCommandHandlerWithMutexDecorator(
IUnitOfWork unitOfWork,
ICommandHandler<TCommand> decorated)
{
this.handlerToCall = decorated;
this.unitOfWork = unitOfWork;
}
public …Run Code Online (Sandbox Code Playgroud) 我正在使用ASP.NET MVC 4应用程序.
家庭控制器的构造函数参数化为2参数(Iservice1 service1,Iservice2 service2)并非所有代码路径都使用任何Service(service1,service2),只在某些代码路径中我需要service1 instance/object或service2 instance/object.
我不想使用container.Resolve < <Lazy<IService1>>();
从这个链接(http://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx)我明白unity.mvc 4使用unity 3,它具有Lazy loading支持,但是如何在ASP.NET MVC 4中执行此操作.
asp.net-mvc lazy-loading inversion-of-control unity-container deferred-loading
最新版本的SimpleInjector引入了MVC和WebApi之间的区别.两个请求注册是否为同一个东西的别名?或者是否存在潜在的差异?
谢谢
我想通过 SimpleInjector 注入 Container 属性。我没有找到 SimpleInjector 的任何功能。
然后我想将self Container注册到自己,但是Container没有接口。
我想要这个功能,因为我不通过构造函数传输 Container 对象 - 因为如果我可以使用注册对象的自动注入。
我的使用思路:
var container = new Container();
container.Options.AutowirePropertiesWithAttribute<InjectableProperty>();
container.Register<ISomething, Something>(Lifestyle.Singleton);
Run Code Online (Sandbox Code Playgroud)
ISomething:
public interface ISomething
{
void SomeMethod();
}
Run Code Online (Sandbox Code Playgroud)
东西类:
public class Something : ISomething
{
public void SomeMethod()
{
var environment = _container.GetInstance<IEnvironment>();
environment.DoSomething();
}
[InjectableProperty] // - maybe it is not possible (I don't know it)
Container Container {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
你有什么想法来实现这一目标吗?
非常感谢。
我有一种情况,我想使用Identity框架中的自定义UserStore.所以我的控制器看起来像这样.
public AccountController()
: this(new UserManager<ApplicationUser>(new MyUserStore()))
{
}
public AccountController(UserManager<ApplicationUser> userManager)
{
UserManager = userManager;
}
Run Code Online (Sandbox Code Playgroud)
现在Simpleinjector不允许多个构造函数.它可以允许用一个构造函数注册这些类.当情况与上述情况类似时,我该如何解决这个问题.使用Simpleinjector注册.
asp.net-mvc dependency-injection simple-injector asp.net-mvc-4 asp.net-identity
我将.png(800 x 294)尺寸37.2 KB导入统一但导入后显示它的大小为0.9MB,这比原来的大.我在导入设置中将最大尺寸更改为1024到512,将分辨率从原始尺寸降低到512 x 188,尺寸为94KB.如果没有任何图像分辨率变化,.png大小如何增加(从37.2K到0.9MB)?
我希望能够将装饰器与Autofac混合搭配.
例如,假设我有一个由Repository类实现的IRepository接口.
我可以拥有以下装饰器:RepositoryLocalCache,RepositoryDistributedCache,RepositorySecurity,RepositoryLogging ...,你得到了ideea.
基于配置设置,我想用所需的装饰器来装饰基本实现.那可以是无,一个或多个装饰器.
我熟悉注册一个装饰器的语法,或者按固定顺序注册它们的链,但我怎样才能使这个动态化呢?
我正在使用带有ASP.NET Web API项目的Simple Injector,我想对一个接口使用不同的实现,具体取决于所使用的REST端点的版本.例如,如果使用端点的v1,IPaymentData则应使用调用的类进行实例化PaymentData,但如果使用v2 IPaymentData则应由调用的类实现PaymentDataNew.结果证明这是有问题的!
我不想使用类似Simple Injector文档的一部分的复合类,因为这意味着我的代码需要注意并考虑我正在使用的注入框架(坏).
我在最新版本(3.0)中注意到一种称为"基于上下文的注入"的功能.使用该container.RegisterConditional函数,每次解析类型时都应该可以运行委托.
container.RegisterConditional(typeof(IPaymentData),
c => ((HttpContext.Current.Items["version"] as string ?? "1") == "2")
? typeof(PaymentDataNew)
: typeof(PaymentData),
Lifestyle.Scoped,
c => true);
Run Code Online (Sandbox Code Playgroud)
但是,这似乎不起作用,因为即使生命周期是作用域,并且默认生活方式是WebApiRequestLifestyle根据版本返回实现的委托只调用第一个请求.后续请求跳过此(它们似乎使用缓存实现).
有什么我想念的吗?如何在每次请求进入时确保调用委托?
c# ×6
.net ×3
asp.net-mvc ×2
autofac ×2
asp.net ×1
containers ×1
lazy-loading ×1
linq ×1
sprite ×1