业务逻辑类 [英] Business logic class

查看:233
本文介绍了业务逻辑类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所遇到的一些方法在asp.net写商业逻辑,但我想知道下面2的例子,有什么用结构来存储类变量的好处:

 命名空间的购物
{
公共结构的购物
{
公共串色;
公众诠释产品编号;
}

公共部分类MyShoppingCart
{

公共小数GetTotal(字符串cartID)
{
}

//一些其他的方法...
}
}



< HR>

 命名空间的购物
{
公共部分类MyShoppingCart
{
公共串色{得到;组; }
公众诠释产品编号{搞定;组; }

公共小数GetTotal(字符串cartID)
{
}

//一些其他的方法...
}
}


解决方案

作为的their回答这里




当你不需要多态,希望值语义,并希望
,以避免堆分配和相关的垃圾收集
开销。需要说明的,但是,是结构(任意大小)是
更昂贵的绕过比类引用(通常是一个
字机),因此类可能最终在实践中速度更快。




他的回答这里




当你想,而不是用语义值语义使用结构




http://msdn.microsoft .COM / EN-US /库/ ms228593.aspx




1.7结构



像类一样,结构是可以包含数据
成员和函数成员的数据结构,但是与类不同,结构是值
类型,并且不需要堆分配。结构类型
的变量直接存储该结构的数据,而类
类型的变量存储到一个动态分配的对象的引用。结构
类型不支持用户指定的继承,并且所有结构类型
隐式地从类型object继承。



结构对于小的数据尤为有用有
值语义结构。复数,点的坐标系中,或字典中的
键 - 值对结构的典型示例。对于小的数据结构的
使用结构而不是类的可以使
中的内存分配的应用程序
执行的数量的大的差异。例如,下面的程序创建并初始化
100点数的阵列。随着作为一个类实现点,101
单独的对象实例化 - 一个用于数组,每个为
100的元素之一。



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 ...
   }
}

解决方案

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.

这篇关于业务逻辑类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆