Conventions in the destination code?

I have two classes: A and B. B knows about A, and A does not know about B. B has properties that can be perfectly set from A, although inheritance of A and B. does not exist. be many times when I need to assign properties B from A, but I'm looking for pointers on where I should put this code.

public class A { } public class B { //constructor? public B(A a) { //set b properties from a } //factory method? public static B FromA(A a) { B b = new B(); //set b properties from a return b; } //setter method? public static void SetBFromA(B b, A a) { //set b properties from a } //assignment method? public void AssignFrom(A a) { //set b properties from a } } //static helper class? public static class BHelper { public static B GetBFromA(A a) { B b = new B(); //set b properties from a return b; } public static void SetBFromA(B b, A a) { //set b properties from a } } 

What, if any, are common practice? Can any of them affect their signature? For example, using a constructor, is it usually passed that B holds a reference to the passed A? These are the considerations that I think of.

Thanks!

+6
c #
source share
4 answers

All options you specify are valid. Which one you choose will depend on the specific circumstances.

  //constructor? public B(A a) { //set b properties from a } 

A constructor pattern will be the choice when B cannot exist without data from A.

 //factory method? public static B FromA(A a) { B b = new B(); //set b properties from a return b; } 

The factory method would be a model of choice when the values โ€‹โ€‹of B fall depending on the values โ€‹โ€‹of A or B. Especially if B has children, that a value of B will provide a design choice.

 //setter method? public static void SetBFromA(B b, A a) { //set b properties from a } 

The setter method is not applicable in your scenario, because it will be used when A and B do not know each other, and a third party must intercede.

 //assignment method? public void AssignFrom(A a) { //set b properties from a } 

This is essentially a visitor pattern. Visits B and leaves something by itself (thinks of bachelor days). This will be a sample if data from A is optional for B.

+2
source share

I would use the constructor method or explicit conversion.

 public static explicit operator B(A typ) { } 

All in all, I think this is very subjective. We could post the answer using another method and see which one voted the most, but in fact each method has different advantages and disadvantages. This will depend more on the context and how this template is executed in the rest of the code.

+3
source share

I will throw away an alternative:

 public static B CreateFromA(A a) { } 

In this case, the factory syntax is used to suggest that you can disable B given A , but avoiding everything that looks like a conversion.

+3
source share

I do not think that there is a common practice as such. When I did this, I used the factory method or constructor. I would prefer a factory.

0
source share

All Articles