I have come across a few ways to write a business logic in asp.net but I am wondering for 2 example below, what are the benefits of using a struct to store class variables:
namespace Shopping
{
public struct ShoppingCart
{
public string Color;
public int ProductId;
}
public partial class MyShoppingCart
{
public decimal GetTotal(string cartID)
{
}
// Some other methods ...
}
}
namespace Shopping
{
public partial class MyShoppingCart
{
public string Color{ get; set; }
public int ProductId{ get; set; }
public decimal GetTotal(string cartID)
{
}
// Some other methods ...
}
}
Business logic is essentially the part of a computer program that contains the information (in the form of business rules) that defines or constrains how a business operates. Such business rules are operational policies that are usually expressed in true or false binaries.
Business logic is any Java™ code that is invoked as an action when an event occurs, such as a host screen being recognized or your HATS application being started. Business logic is specific to the application and is not provided as part of HATS.
The main components of business logic are business rules and workflows. A business rule describes a specific procedure; a workflow consists of the tasks, procedural steps, required input and output information, and tools needed for each step of that procedure.
Many financial organizations rely on business logic to define how a business system or application performs calculations and executes a transaction. For example, when you make a website purchase, business logic determines things like how much you should pay for shipping or taxes before providing you with a final total.
As dsimcha states in their answer here:
Whenever you don't need polymorphism, want value semantics, and want to avoid heap allocation and the associated garbage collection overhead. The caveat, however, is that structs (arbitrarily large) are more expensive to pass around than class references (usually one machine word), so classes could end up being faster in practice.
As JoshBerke states in his answer here:
Use a struct when you want value semantics as opposed to reference semantics.
From the http://msdn.microsoft.com/en-us/library/ms228593.aspx
1.7 Structs
Like classes, structs are data structures that can contain data members and function members, but unlike classes, structs are value types and do not require heap allocation. A variable of a struct type directly stores the data of the struct, whereas a variable of a class type stores a reference to a dynamically allocated object. Struct types do not support user-specified inheritance, and all struct types implicitly inherit from type object.
Structs are particularly useful for small data structures that have value semantics. Complex numbers, points in a coordinate system, or key-value pairs in a dictionary are all good examples of structs. The use of structs rather than classes for small data structures can make a large difference in the number of memory allocations an application performs. For example, the following program creates and initializes an array of 100 points. With Point implemented as a class, 101 separate objects are instantiated—one for the array and one each for the 100 elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With