I have an ObjectA whose property values will always be assigned to another object's (ObjectB) properites. What I have been doing is to have a service class instantiate the receiveing ObjectB and then assign the values to the new object from ObjectA. Should I instead be sending the entire objectA directly to Object B in a setter parameter or other method? What are best practices here if any?
我有一个ObjectA,其属性值将始终分配给另一个对象的(ObjectB)属性。我一直在做的是让服务类实例化接收ObjectB,然后将值分配给ObjectA中的新对象。我应该在setter参数或其他方法中将整个objectA直接发送到Object B吗?这里有什么最佳做法?
EDIT: In a Service class:
编辑:在服务类:
public AssignValues (objectA)
{
ObjectB objectB= new ObjectB();
objectB.prop1= ObjectA.prop1;
objectB.prop2= ObjectA.prop2;
}
OR
public AssignValues(objectA)
{
ObjectB objectB= new ObjectB();
objectB.SetValuesFromObjectA= objectA;
}
OR
public AssignValues(objectA)
{
ObjectB objectB= new ObjectB();
objectB.SetValuesFromObjectA(objectA);
}
OR
public AssignValue(objectA)
{
ObjectB objectB= new ObjectB(objectA);
}
4 个解决方案
#1
It's hard to answer because we don't know what these classes are. You could say, "oh, I will always pass objectA to objectB's constructor" and wind up with ...
这很难回答,因为我们不知道这些类是什么。你可以说,“哦,我将始终将objectA传递给objectB的构造函数”并结束......
public Chicken( Gorilla g )
{
this.IsAlive = g.IsAlive;
}
... or something just as nonsensical. There are so many ways to do what you want to do, the fundamental question of the "right way" depends completely what you're doing.
......或者说是荒谬的东西。有很多方法可以做你想做的事情,“正确的方式”的根本问题完全取决于你正在做的事情。
#2
Sounds like there's an Object b constructor that takes a single parameter, an Object A:
听起来像是一个Object b构造函数,它接受一个参数,一个对象A:
// ctor:
ObjectB( const ObjectA& a ) {
prop1= a.prop1;
prop2= a.prop2;
}
// or ctor:
ObjectB( const ObjectA& a ) : prop1(a.prop1), prop2(a.prop2) {
}
. . .
// use
ObjectB b(a);
#3
If the objects are of the same type, I would probably prefer to use a Clone()
(shallow-copy) method.
如果对象属于同一类型,我可能更喜欢使用Clone()(浅拷贝)方法。
If the objects are of different types, I would probably write a method that looks like your first case to keep the two types separated. Optionally, I might do that as an extension method that takes objectA and objectB (if either one is a struct, you might have to do that by ref or return value). As I said, The main reason I would not want to use a constructor or class/instance method in this case is that you are now tying the two types together when they may not necessarily have to really know about each other.
如果对象的类型不同,我可能会编写一个看起来像第一种情况的方法来保持两种类型的分离。或者,我可以这样做作为一个扩展方法,它接受objectA和objectB(如果其中一个是结构,你可能必须通过ref或返回值来实现)。正如我所说的那样,在这种情况下,我不想使用构造函数或类/实例方法的主要原因是,当你可能不一定要真正了解彼此时,你现在将两种类型绑在一起。
#4
Have you considered dependency injection with AutoFac? It allows you to declare what the relationships are before actually making anything. Then make, use, and clean-up nicely.
您是否考虑过使用AutoFac进行依赖注入?它允许您在实际制作任何内容之前声明关系。然后很好地制作,使用和清理。
#1
It's hard to answer because we don't know what these classes are. You could say, "oh, I will always pass objectA to objectB's constructor" and wind up with ...
这很难回答,因为我们不知道这些类是什么。你可以说,“哦,我将始终将objectA传递给objectB的构造函数”并结束......
public Chicken( Gorilla g )
{
this.IsAlive = g.IsAlive;
}
... or something just as nonsensical. There are so many ways to do what you want to do, the fundamental question of the "right way" depends completely what you're doing.
......或者说是荒谬的东西。有很多方法可以做你想做的事情,“正确的方式”的根本问题完全取决于你正在做的事情。
#2
Sounds like there's an Object b constructor that takes a single parameter, an Object A:
听起来像是一个Object b构造函数,它接受一个参数,一个对象A:
// ctor:
ObjectB( const ObjectA& a ) {
prop1= a.prop1;
prop2= a.prop2;
}
// or ctor:
ObjectB( const ObjectA& a ) : prop1(a.prop1), prop2(a.prop2) {
}
. . .
// use
ObjectB b(a);
#3
If the objects are of the same type, I would probably prefer to use a Clone()
(shallow-copy) method.
如果对象属于同一类型,我可能更喜欢使用Clone()(浅拷贝)方法。
If the objects are of different types, I would probably write a method that looks like your first case to keep the two types separated. Optionally, I might do that as an extension method that takes objectA and objectB (if either one is a struct, you might have to do that by ref or return value). As I said, The main reason I would not want to use a constructor or class/instance method in this case is that you are now tying the two types together when they may not necessarily have to really know about each other.
如果对象的类型不同,我可能会编写一个看起来像第一种情况的方法来保持两种类型的分离。或者,我可以这样做作为一个扩展方法,它接受objectA和objectB(如果其中一个是结构,你可能必须通过ref或返回值来实现)。正如我所说的那样,在这种情况下,我不想使用构造函数或类/实例方法的主要原因是,当你可能不一定要真正了解彼此时,你现在将两种类型绑在一起。
#4
Have you considered dependency injection with AutoFac? It allows you to declare what the relationships are before actually making anything. Then make, use, and clean-up nicely.
您是否考虑过使用AutoFac进行依赖注入?它允许您在实际制作任何内容之前声明关系。然后很好地制作,使用和清理。