我正在尝试针对Entity框架(或其他Linq提供程序)创建动态查询.
让我用一些示例代码解释我的问题.
如果我硬编码表达式:
var id = 12345;
Expression<Func<ItemSearch, bool>> myLambda = (s) => s.Id == id;
var finalQuery = context.ItemSearch.Where(myLambda);
this.Log((finalQuery as ObjectQuery).ToTraceString());
Run Code Online (Sandbox Code Playgroud)
生成的SQL如下所示:
SELECT ...
FROM ViewItemSearch "Extent1"
WHERE "Extent1".ID = :p__linq__0
Run Code Online (Sandbox Code Playgroud)
用一个很好的:p__linq__0dbParameter.
如果我创建一个表达式:
var id = 12345;
ParameterExpression param = Expression.Parameter(typeof(ItemSearch), "s");
Expression prop = Expression.Property(param, "Id");
Expression val = Expression.Constant(id);
Expression searchExpr = Expression.Equal(prop, val);
Expression<Func<ItemSearch, bool>> myLambda =
Expression.Lambda<Func<ItemSearch, bool>>(searchExpr , param);
var finalQuery = context.ItemSearch.Where(myLambda);
this.Log((finalQuery as ObjectQuery).ToTraceString());
Run Code Online (Sandbox Code Playgroud)
生成的SQL如下所示:
SELECT ...
FROM ViewItemSearch …Run Code Online (Sandbox Code Playgroud) 在传统的Asp.NET webforms应用程序中,我尝试注入一些IOC.
我不会详细介绍,但对于这一点,我认为ServiceLocator是一个很好的工具.我知道服务定位器是反模式的; o)
根据文档https://github.com/autofac/Autofac/wiki/Common-Service-Locator,ServiceLocator配置如下所示:
var container = builder.Build();
var csl = new AutofacServiceLocator(container);
ServiceLocator.SetLocatorProvider(() => csl);
Run Code Online (Sandbox Code Playgroud)
问题是LoacatorProvider是使用根容器设置的.因此,终身管理不活跃.
我正在考虑解决这个问题的方法
因为ServiceLocator.SetLocatorProvider将一个委托作为参数,并且因为在每次调用时调用此委托,ServiceLocator.Current为什么不为每个请求提供一个AutofacServiceLocator实例.
public class Global : HttpApplication, IContainerProviderAccessor
{
// Provider that holds the application container.
static IContainerProvider _containerProvider;
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
_containerProvider = new ContainerProvider(AutoFacBootstrapper.CreateContainer());
ServiceLocator.SetLocatorProvider(() =>
{
AutofacServiceLocator asl = HttpContext.Current.Items["AutofacServiceLocator"] as AutofacServiceLocator;
if (asl == null)
{
var cpa = (IContainerProviderAccessor)HttpContext.Current.ApplicationInstance; …Run Code Online (Sandbox Code Playgroud) 我有ServiceStack.CacheAccess.Memcached的问题.MemcachedClientCache.该Increment方法无法按预期工作.
对于测试,我使用的是常规控制台应用程序和ICacheClient界面.
根据方法文档:
必须先将项目插入缓存,然后才能更改该项目.
该项必须作为a插入System.String.
该操作仅适用于System.UInt32values,因此-1始终表示未找到该项.
所以我们走了:
ICacheClient client = new MemcachedClientCache();
string key = "test";
bool result = client.Remove(key);
Console.WriteLine("{0} removed: {1}", key, result);
//The item must be inserted as a System.String.
result = client.Add(key, "1");
Console.WriteLine("added {0}", result);
long v = client.Increment(key, 1);
Console.WriteLine("first increment : {0}", v);
string o = client.Get<string>(key);
Console.WriteLine("first read: {0}", o);
v = client.Increment(key, 1);
Console.WriteLine("second increment: {0}", v);
o = client.Get<string>(key);
Console.WriteLine("second …Run Code Online (Sandbox Code Playgroud)