What is better for Struct / Classes data warehouse?

We saw a lot of discussion in SO regarding the vs struct class in C #. Basically ended the conclusions that talked about heap / stack memory allocation. We recommend using structures in small data structures .

Now I have a situation to solve a simple data warehouse among these two options. Currenlty in our application has thousands of classes, just acts as simple data stores (only open public fields), and they are transferred between different modules and services.

According to my understanding, I felt that it was better to move forward in structure instead of classes for performance reasons. Because these simple data structures act only as data warehouses.

Before embarking on this, I need the advice of experts who have experienced this struggle.

  • Do I understand correctly?
  • I have seen that most ORMs have classes as data stores. Therefore, I doubt that there are reasons to go against classes instead of structures. what was that?
+22
c # struct class
Dec 23 '09 at 7:25
source share
9 answers

I would make a selection based on the following criteria

  • reference type and value type semantics. If 2 objects are equal only if they are the same object, this indicates the type of the reference type semantics => class. If the value of its members defines equality (for example, 2 DateTimes are equal if both represent the same time, even if they are two different objects), the value type is semantics => struct
  • Object memory. If an object is huge and often distributed, then its structure will consume the stack much faster, so I would prefer to use it as a class. On the contrary, I would prefer to avoid a fine GC for small value types; so make them a structure.
  • Can you make an object immutable? I find that structures are great for "value objects" - from the DDD book.
  • Would you encounter some sort of box unboxing based on using this object? If yes, go for the class.
+44
Dec 23 '09 at 7:43
source share
Structures

must be defined unchanged where in classes should not be. If you think your objects will be small and immutable , you can continue to create their structures or let them be classes.

+7
Dec 23 '09 at 7:33
source share

The rather cool, not so well-known advantage of Structs over Classes is that the structures have an automatic implementation of GetHashcode and Equals.
This is very useful when dictionaries require keys.

The structural implementation of GetHashcode and Equals is based on the binary contents of the structure instances + reflection for reference elements (e.g. String members and other class instances)

So the following code works for GethashCode / Equals:

public struct Person { public DateTime Birthday { get; set; } public int Age{ get; set; } public String Firstname { get; set; } } class Program { static void Main(string[] args) { Person p1 = new Person { Age = 44, Birthday = new DateTime(1971, 5, 24), Firstname = "Emmanuel" }; Person p2 = new Person { Age = 44, Birthday = new DateTime(1971, 5, 24), Firstname = "Emmanuel" }; Debug.Assert(p1.Equals(p2)); Debug.Assert(p1.GetHashCode() == p2.GetHashCode()); } } 

Both statements succeed when Person is a structure. Both statements fail if Person is a class instead of struct.

Link: https://msdn.microsoft.com/en-Us/library/2dts52z7%28v=vs.110%29.aspx

Regards, Best Coding

+6
Sep 26 '15 at 13:06
source share

I really can never remember how different are structured, but they are. Subtle ways. In fact, sometimes they come and bite you.

So. If you don’t know what you are doing, just stick to classes.

I know this sounds a little newbie. I know that now I have to go, look for differences and show them here, but others have already done this. All I'm saying is that adding a different type of object creates a semantic burden, a little complicated, which you wisely consider carefully.

If I remember correctly, one of the biggest problems is the semantics of structs values: passing them will lead to different objects (as they are passed by value). If you then change a field in one place, beware that the field has not changed in all other places! That's why everyone recommends immutability for structures!

EDIT: for the case you are describing, the structures will not work !

+5
Dec 23 '09 at 7:34
source share

A note on immutable strings is correct. And this is where he can bite you. You can define structures with field installers, but when the field value changes, a new instance is created. Therefore, if you keep a reference to an old object, it will still refer to the old value. For this reason, I do not like the use of mutable structures, as this can lead to subtle and complex errors (especially if you use complex compound statements).

On the other hand, there are many good reasons for using immutable state classes (think string).

+3
Dec 23 '09 at 7:42
source share

A class object has the advantage that it can pass a link to it, while the scope and lifetime of such a link is unlimited if it reaches external code. The advantage of the structure is that, although it can go around short-lived references to them, endless messy links can not be transmitted. This helps to avoid having to worry about whether such links exist.

Some people have suggested that owners of data that change should not be structures. I strongly disagree. Entities that exist for storing data should in many cases be structs especially if they are mutable. Eric Lippert has repeatedly published that he considers mutable value types to be evil (search for labels “mutable” and “struct”). Of course, it is true that .net allows you to do certain things with the help of mutable structures, which it should not, and does not conveniently allow some of the things that it should, but POD structures ("Plain Old Data") that do not have mutating methods, but instead, they expose their entire state through public fields, have very useful consistency in their behavior, which is not used with any other data type. Using the POD structure can confuse someone who is not familiar with how they work, but will make the program more readable for everyone who does it.

Consider, for example, the following code, assuming that EmployeeInfoStruct contains only value types and immutable types , such as String:

 [employeeInfoStruct is a struct containing the following field]
 public Decimal YearlyBonus;

 [someEmployeeContainer is an instance of a class which includes the following method]
 EmployeeInfoStruct GetEmployeeInfo (String id);  // Just the signature - code is immaterial

 [some other method uses the following code]
 EmployeeInfoStruct anEmployee = someEmployeeContainer.GetEmployeeInfo ("123-45-6789");
 anEmployee.YearlyBonus + = 100;

Eric Lippert complains that the above code will change the value in anEmployee, but this change will not affect the container. I would suggest that a good thing is that anyone who knows how structures work can look at the above code and know that writing to a structured variable will affect that variable, but will not affect anything else if the program later does not use any another method (maybe SetEmployeeInfo) to save this variable somewhere.

Now replace EmployeeInfoStruct with EmployeeInfoClass, which has a read / write property of type YearlyBonus. Using only the above information, what can be said about the relationship between writing for any production server and anEmployee? Depending on the implementation of the anEmployee class (which, if EmployeeInfoClass is not sealed, may or may not actually be EmployeeInfoClass) and someEmployeeContainer, the relationship between the objects can be anything. Writes:

  • Do not affect others
  • Update another in 'natural' mode
  • Corrupt another in any way

Using structures containing only fields of value types or immutable classes, semantics will always be # 1. You do not need to look at the code of the structure itself or the container code to know this. In contrast, if anEmployee.Salary or someEmployeeContainer.GetEmployee is virtual, it is impossible to really know what semantics are.

It is important to note that if the structures are large, passing them by value or returning them from functions can be costly. It is generally best to pass large structures as ref parameters whenever possible. Although inline collections really don't do a good job of facilitating this use, it can make using a structure with hundreds of bytes cheaper than using a class.

+3
Oct 06 '11 at 16:24
source share

I think you have the right idea. Structures are created to simulate data types. They are based on value, not based on links. If you look at the MSDN documentation for most base data classes (int, double, decimal, ect.), They are all structure-based. However, for the same reason, structures should not be abused. A room for storing everything in this structure is allocated immediately after its creation, when classes simply allocate space for links to everything inside. If the data is in small enough pieces, where this is not a problem, and structures are the way to go. If this is a problem, refer to the classes. If you don’t know how best to stick to what you are familiar with.

+2
Dec 23 '09 at 7:47
source share

I remember one tip given on MSDN, which should be no more than 16 or 21 bytes. Searches for a link, but I can’t find it yet.

The main reason was that after you have a string in your data type, make it a class without hesitation. Otherwise, the structure should not take up much space.

+1
Dec 23 '09 at 7:43
source share

If you have low latency requirements and many objects, slow garbage collections can be a problem. In this case, the structure can be very useful, because the garbage collector does not need to check the hierarchy of value types if the value types do not contain reference types.

Here you can find a landmark: http://00sharp.wordpress.com/2013/07/03/a-case-for-the-struct/

0
Jul 04 '13 at 6:24
source share



All Articles