将方法或函数作为数据传递的常用方法有哪些有用的定义,例如:
language-agnostic delegates closures function-pointers dynamic-proxy
我想在反序列化之后将单例范围的依赖项重新注入原型Spring bean.
假设我有一个Process bean,它依赖于Repository bean.Repository bean的作用域是单例,但Process bean是原型作用域.我会定期对序列进行序列化,然后对其进行反序列化.
class Process {
private Repository repository;
// getters, setters, etc.
}
Run Code Online (Sandbox Code Playgroud)
我不想序列化和反序列化存储库.我也不想将"瞬态"放在成员变量上,该成员变量在Process中包含对它的引用,也不是对某种代理的引用,或者除了声明为Repository的普通旧成员变量之外的任何东西.
我想我想要的是让Process依赖于一个可序列化的代理,该代理指向(通过瞬态引用)到Repository,并且在反序列化时,可以再次找到Repository.我怎么能自定义Spring呢?
我想我可以使用代理来保存依赖引用,就像.我希望我可以使用那种确切的技术.但是我看到Spring生成的代理不是可序列化的,并且文档说如果我将它与单例bean一起使用,我将得到一个例外.
我可以在单例bean上使用自定义作用域,当要求自定义作用域bean时,它总是提供代理.这是一个好主意吗?其他想法?
我想创建一个动态代理,用于将WinForms控件绑定到由不同(非GUI)线程更改的对象.这样的代理将拦截PropertyChanged事件并使用适当的SynchronizationContext调度它.
这样我就可以使用辅助类来完成这项工作,而不必每次都手动实现同步(if (control.InvokeRequired) etc.).
有没有办法使用LinFu,Castle或类似的库?
[编辑]
数据源不一定是列表.它可以是任何业务对象,例如:
interface IConnection : INotifyPropertyChanged
{
ConnectionStatus Status { get; }
}
Run Code Online (Sandbox Code Playgroud)
我可以创建一个可以完成工作的包装器,它看起来像这样:
public class ConnectionWrapper : IConnection
{
private readonly SynchronizationContext _ctx;
private readonly IConnection _actual;
public ConnectionWrapper(IConnection actual)
{
_ctx = SynchronizationContext.Current;
_actual= actual;
_actual.PropertyChanged +=
new PropertyChangedEventHandler(actual_PropertyChanged);
}
// we have to do 2 things:
// 1. wrap each property manually
// 2. handle the source event and fire it on the GUI thread
private void PropertyChanged(object sender, PropertyChangedEvArgs …Run Code Online (Sandbox Code Playgroud) c# multithreading inotifypropertychanged dynamic-proxy winforms
我试图找出如何克隆或将System.Data.Entity.DynamicProxies转换为它的实际类.例如:
System.Data.Entity.DynamicProxies.Currency_F4008E27DE_etc is the proxy class
MyApp.Entities.Currency is the real class
Run Code Online (Sandbox Code Playgroud)
MyApp.Entities中的所有类都继承自BaseEntity,因此我尝试在那里进行转换:
public abstract partial class BaseEntity
{
public T ShallowCopy<T>() where T : BaseEntity
{
return this.MemberwiseClone() as T;
}
// other BaseEntity properties not relevent here
}
Run Code Online (Sandbox Code Playgroud)
然后将DynamicProxies转换为真正的类:
// this returns a DynamicProxies class
Currency currency = LookupDefaultCurrency();
// this one needs to return a Entities.Currency class
// (but currently returns a DynamicProxies class too
Currency pocoCurrency = (Currency)currency.ShallowCopy<Currency>();
HttpRuntime.Cache[key] = pocoCurrency;
Run Code Online (Sandbox Code Playgroud)
这样做的原因是我想从该对象中删除所有Entity Framework跟踪等,并将其普通(POCO)属性存储在缓存中.我需要能够为所有100个左右的Entity类执行此操作,因此它必须具有合理的通用性 …
我正在尝试序列化域模型并遇到一个需要将动态代理转换为POCO的问题.我遇到的问题是通过模型中的虚拟属性存在循环引用.虽然我试图使用[ScriptIgnore]序列化程序不解析这些属性,但它仍然可以.我相信这是因为对象是动态代理,并且属性中仍然存在一些导致解析器进入的残余(这反过来导致递归错误"循环引用" - 我尝试将递归限制为3步但我得到了错误"超出递归步骤").
如何将对象从动态代理转换为POCO以便可以序列化?
编辑:简单的例子
public class One : BaseViewModel
{
public int OneId { get; set; }
public virtual ICollection<Two> Two { get; set; }
}
public class Two
{
public int TwoId { get; set; }
public int OneId { get; set; }
[ScriptIgnore]
public virtual One One { get; set; }
}
public abstract class BaseViewModel
{
public string AsJson()
{
var serializer = new JavaScriptSerializer();
return serializer.Serialize(this);
}
}
Run Code Online (Sandbox Code Playgroud) c# serialization dynamic-proxy entity-framework-4.1 asp.net-mvc-3
我试图了解代理和动态代理模式之间的区别.从我到目前为止所读到的,我发现的唯一事情是代理类字节代码是在编译期间创建的,而动态代理是在运行时创建的.我还缺少另一个差异吗?如果不是那么更喜欢代理而不是动态代理的原因是什么(性能问题除外)
我从数据库中获取一些数据并将其存储在全局变量中,如下所示:
//Global Variable
public static List<stuff> Stuff;
using (var context = new StuffContext())
{
stuff = new List<stuff>();
stuff = (from r in context.Stuff
select r).ToList();
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是上下文关闭,当我希望访问存储在全局变量中的一些数据时,我不能.
数据是System.Data.Entity.DynamicProxies.Stuff而不是Application.Model.Stuff,这意味着当我尝试对数据执行某些操作时,我会收到此错误:
"The ObjectContext instance has been disposed and can no longer be used for operations that require a connection."
Run Code Online (Sandbox Code Playgroud)
我的问题是,我如何使用上面的代码作为示例,转换/转换为我想要的类型,以便我可以在我的应用程序中使用其他数据?
我在某些操作中通过反射使用类属性,因此在使用DynamicProxy实例时,它会导致加载整个数据库.(700多个课程相互关联).
是否可以检查是否加载了延迟加载属性?ProxyCreationEnabled = false在我的情况下,禁用动态代理生成()是不可用的.
Customer oCustomer = context.get(1);
if(oCustomer.Location.HasLoaded)
do smt..
public class Customer
{
public decimal? Id {get; set;}
public virtual CustomerLocation Location{get; set;}
}
public class CustomerLocation
{
public decimal? Id {get; set;}
public string Detail {get; set;}
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有以下代码的类,在这里我希望使用任何代表数字的类/类型都是琐碎的。我发现自己定义了大量的方法,如下所示:
public class Range {
private BigDecimal inferior = new BigDecimal(0);
private BigDecimal superior = new BigDecimal(1);
public Range(BigDecimal inferior, BigDecimal superior) {
if (inferior.compareTo(superior) == -1) {
this.inferior = inferior;
this.superior = superior;
}
}
public Range(int inferior, int superior) {
this(new BigDecimal(inferior), new BigDecimal(superior));
}
public Range(Integer inferior, Integer superior) {
this(new BigDecimal(inferior), new BigDecimal(superior));
}
public Range(float inferior, float superior) {
this(new BigDecimal(inferior), new BigDecimal(superior));
}
public Range(double inferior, double superior) {
this(new BigDecimal(inferior), new BigDecimal(superior));
} …Run Code Online (Sandbox Code Playgroud) 我编写了一个薄包装器,它传递了创建的节点document.createElement并添加了一些方法。这个包装器是用代理实现的。我所做的就是抓住一些吸气剂。
return new Proxy(node, {
get (target, prop) {
if (prop === 'node') return target
if (wFuncs[prop]) {
return Reflect.get(target, wFuncs[prop]).bind(target)
}
return Reflect.get(target, prop)
}
})
Run Code Online (Sandbox Code Playgroud)
我本来希望能够传递这样的代理,appendChild因为它仍然是 html 元素的实例Node,并且具有 html 元素的所有属性和方法。然而,appendChild 抱怨传递的元素不是 Node:
TypeError:无法在“Node”上执行“appendChild”:参数 1 不是“Node”类型
关于如何解决这个问题有什么想法吗?
dynamic-proxy ×10
c# ×4
java ×3
adapter ×1
cloning ×1
closures ×1
database ×1
delegates ×1
dom ×1
javascript ×1
lazy-loading ×1
oop ×1
poco ×1
spring ×1
winforms ×1