我有一些Web方法将我的对象作为序列化XML返回.它只是序列化对象的NHibernate映射属性......任何人都有一些洞察力?似乎Web方法实际上是序列化NHibernate代理而不是我的类.我尝试过使用[XMLInclude]和[XMLElement],但属性仍然没有序列化.我有一种非常可怕的hackish方式绕过这个,但我想知道是否有更好的方法!
像这样的东西:
<?xml version="1.0" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="StoryManager" assembly="StoryManager">
<class name="Graphic" table="graphics" lazy="false">
<id name="Id" column="id" type="int" unsaved-value="0" >
<generator class="identity"/>
</id>
<property name="Assigned" />
<property name="Due" />
<property name="Completed" />
<property name="UglyHack" insert="false" update="false" />
<many-to-one name="Parent" class="Story" column="story_id"/>
</class>
</hibernate-mapping>
public class Graphic
{
private int m_id;
public virtual int Id
{
get { return m_id; }
set { m_id = value; }
}
private DateTime? m_assigned;
public virtual DateTime? Assigned
{
get { return m_assigned; }
set …Run Code Online (Sandbox Code Playgroud) 在实体框架4中是否可以选择在没有使用代理类的情况下将某些查询加载到POCO中?(出于缓存该对象的目的,以备将来只读使用).我正在使用存储库 - 服务模式.
我的意思是:
var order = _orderService.GetById(1);
// after order is loaded then we can see in the debugger that:
// order.Customer is of type System.Data.Entity.DynamicProxy.Customer_17631AJG_etc
Run Code Online (Sandbox Code Playgroud)
我想要的是order.Customer实际使用POCO类型MyApp.Models.Entities.Customer而不是该类型的代理.
编辑:根据Ladislav建议在存储库中添加"GetUnproxied"方法,我做了这个改动:
// this is the current method that must return a DynamicProxy
public IQueryable<T> GetQuery()
{
return ObjectSet.AsQueryable();
}
// this is the new additional method that must return the plain POCO
public IQueryable<T> GetReadOnly()
{
ObjectContext.ContextOptions.ProxyCreationEnabled = false;
var readOnly = ObjectSet.AsQueryable();
ObjectContext.ContextOptions.ProxyCreationEnabled = true;
return readOnly; …Run Code Online (Sandbox Code Playgroud) 当试图在init:acl生成的acl_classes表中的现有条目的实体上运行findAcl()时,我得到一个AclNotFoundException.
在调用findAcl()之前使用createAcl()对相关对象进行测试会在acl_classes中显示相同类型的代理类的新记录,并且操作成功完成.
这似乎是错误的,代理应该是透明的,或者我错过了什么?
我的Discount模型描述了系统中所有类型折扣的常用字段.我有一些代理模型描述了用于计算总数的具体算法.基Discount类有一个名为的成员字段type,它是一个标识其类型及其相关类的字符串.
class Discount(models.Model):
TYPE_CHOICES = (
('V', 'Value'),
('P', 'Percentage'),
)
name = models.CharField(max_length=32)
code = models.CharField(max_length=32)
quantity = models.PositiveIntegerField()
value = models.DecimalField(max_digits=4, decimal_places=2)
type = models.CharField(max_length=1, choices=TYPE_CHOICES)
def __unicode__(self):
return self.name
def __init__(self, *args, **kwargs):
if self.type:
self.__class__ = getattr(sys.modules[__name__], self.type + 'Discount')
super(Discount, self).__init__(*args, **kwargs)
class ValueDiscount(Discount):
class Meta:
proxy = True
def total(self, total):
return total - self.value
Run Code Online (Sandbox Code Playgroud)
但我继续得到AttributeError的例外,说自己没有类型.如何解决这个问题还是有另一种方法来实现这一目标?
我正在使用CXF生成SOAP客户端类.在CXF文档中,他们写道
JAX-WS客户端代理是否安全?
官方JAX-WS回答:不可以.根据JAX-WS规范,客户端代理不是线程安全的.要编写可移植代码,您应将它们视为非线程安全并同步访问或使用实例池或类似代码.
CXF回答:对于许多用例,CXF代理是线程安全的.例外情况是:
(我省略了对这些用例的描述)
对于大多数"简单"用例,您可以在多个线程上使用CXF代理.以上概述了其他人的解决方法.
有没有人有任何相反的经历?遇到了faq中没有描述的多线程问题?或者他们的描述是否准确,他们基本上可以安全使用?
我有一个来自另一个闭源的库的类,但我希望能够使用它的接口.原因是我不想在任何地方进行instanceof检查或检查null,但我也不想扩展现有的类.
例如,假设我有这样的代码:
public class Example {
// QuietFoo is from another library that I can't change
private static QuietFoo quietFoo;
// LoudFoo is my own code and is meant to replace QuietFoo
private static LoudFoo loudFoo;
public static void main(String[] args) {
handle(foo);
}
private static void handle(Object foo) {
if (foo instanceof QuietFoo)
((QuietFoo) foo).bar();
else if (foo instanceof LoudFoo)
((LoudFoo) foo).bar();
}
}
Run Code Online (Sandbox Code Playgroud)
我无法改变QuietFoo:
public class QuietFoo {
public void bar() { …Run Code Online (Sandbox Code Playgroud) 我有一个Iface接口,它有两个用java编写的方法.该接口是Zzz类的内部接口.我在scala中编写了调用处理程序.然后我尝试在scala中创建一个新的代理实例,如下所示.
val handler = new ProxyInvocationHandler // this handler implements
//InvocationHandler interface
val impl = Proxy.newProxyInstance(
Class.forName(classOf[Iface].getName).getClassLoader(),
Class.forName(classOf[Iface].getName).getClasses,
handler
).asInstanceOf[Iface]
Run Code Online (Sandbox Code Playgroud)
但编译器在这里说
$Proxy0 cannot be cast to xxx.yyy.Zzz$Iface
Run Code Online (Sandbox Code Playgroud)
我怎样才能以简短的方式使用代理.
当我设计一个通用类时,我经常处于以下设计选择之间的两难境地:
template<class T>
class ClassWithSetter {
public:
T x() const; // getter/accessor for x
void set_x(const T& x);
...
};
// vs
template<class T>
class ClassWithProxy {
struct Proxy {
Proxy(ClassWithProxy& c /*, (more args) */);
Proxy& operator=(const T& x); // allow conversion from T
operator T() const; // allow conversion to T
// we disallow taking the address of the reference/proxy (see reasons below)
T* operator&() = delete;
T* operator&() const = delete;
// more operators to delegate to …Run Code Online (Sandbox Code Playgroud) 它想从我这里得到什么?如何让它发挥作用?
var proxy_handler =
{
ownKeys: function(target)
{
return Object.keys(target.data)
},
}
var proxxxy = function(initial_data)
{
var return_value = "Goodbye world"
var target = function() { return return_value }
if(typeof initial_data == "undefined")
{
target.data = {}
}
else
{
target.data = initial_data
}
return new Proxy(target, proxy_handler)
}
var p = proxxxy({q:"aaa",w:"bbb",f:"ccc"})
console.log(p())
console.log(Object.getOwnPropertyNames(p))
Run Code Online (Sandbox Code Playgroud)
它打印一个错误,但不应该:
me@me:~/tst$ node --version
v6.2.2
me@me:~/tst$ node test3.js
Goodbye world
/home/me/tst/test3.js:26
console.log(Object.getOwnPropertyNames(p))
^
TypeError: 'ownKeys' on proxy: trap result did not include 'arguments'
at …Run Code Online (Sandbox Code Playgroud) 我正在开发一个我没有启动的 Django 项目,并且面临继承问题。
我有一个大模型(在示例中简化)称为MyModel它应该代表不同类型的项目。
的所有实例对象MyModel都应具有相同的字段,但方法行为根据项目类型而有很大差异。
到目前为止,它是使用一个MyModel名为 的单个字段来设计的item_type。
然后 MyModel 中定义的方法检查该字段并使用多个 if 执行不同的逻辑:
def example_method(self):
if self.item_type == TYPE_A:
do_this()
if self.item_type == TYPE_B1:
do_that()
Run Code Online (Sandbox Code Playgroud)
更重要的是,某些子类型有许多共同点,因此可以说子类型B和C代表第一级继承。然后这些类型有子类型,例如B1, B2, C1, C2(在下面的示例代码中更好地解释)。
我想说这不是执行多态性的最佳方法。
现在我想更改这些模型以使用真正的继承。
由于所有子模型都有相同的字段,我认为多表继承是没有必要的。我正在考虑使用代理模型,因为只有它们的行为应该根据它们的类型而改变。
这是我提出的伪解决方案:
ITEM_TYPE_CHOICES = (
(TYPE_A, _('Type A')),
(TYPE_B1, _('Type B1')),
(TYPE_B2, _('Type B2')),
(TYPE_C1, _('Type C1')),
(TYPE_C2, _('Type C2')))
class MyModel(models.Model):
item_type = models.CharField(max_length=12, choices=ITEM_TYPE_CHOICES)
def common_thing(self):
pass …Run Code Online (Sandbox Code Playgroud) python django proxy-classes django-models django-inheritance
proxy-classes ×10
c# ×2
django ×2
java ×2
python ×2
acl ×1
adapter ×1
c++ ×1
client ×1
cxf ×1
doctrine-orm ×1
instanceof ×1
interface ×1
javascript ×1
nhibernate ×1
node.js ×1
poco ×1
proxy ×1
scala ×1
soap ×1
symfony ×1
v8 ×1
web-services ×1