Huge object in web application
I am writing web application and I have huge class that have more then 40 members, 3 types开发者_StackOverflow中文版 of arrays and have many methods. This class represents a task with all task's ingrediants including pricing of task. The problem is that I need to pass the client list of 40 first tasks therefore I pass 40 complete task items which turns to be very big and weight a lot of KB. How can I reduce this object??..
Here is a pic of the class diagram: http://www.imagehousing.com/image/624350
The first thing I would tackle before even thinking of sending anything is that you refactor that class into more manageable subcomponents. Take for instance properties like DeliveryNote
, DeliveryNoteId
, DeliveryNoteSerial
could be housed in a Delivery
class. The same could be said for the Group
, Payback
and other similarly named properties. When designing properties of a class, you need to ask yourself whether the properties are specific to the parent model, or are specific to a smaller problem domain.
Once you've refactored that class, you then need to determine what data the client needs. Do they need every article of data in that class. If not, why not produce a view class based on what the client needs and send only that.
If you don't think a partial view is appropriate for your data, you can use DataContractAttribute
and DataMemberAttribute
attributes to control which parts of your model actually represent the data contract to be passed to the client. E.g.:
[DataContract]
public class Task
{
[DataMember]
public string PropertyA { get; set; }
[DataMember]
public string PropertyB { get; set; }
public string PropertyC { get; set; }
}
In the above example, using these attributes I can enforce that PropertyA
and PropertyB
form component parts of the data contract. PropertyC
will not be part of the contract, so will not be serialised. This is of course dependent using the DataContractSerializer
or a WCF-service (which uses that serialiser).
This also limits a single-data contract view of the model.
In first glance, I think you need to reduce/simplify your class, that 40 members ain't really necessary to be direct members, e.g.:
Discount, DiscountTypeCode, DiscountTypeId, DiscountTypeSign can all becomes a class ->
class Discount {
float rate;
int typeCode, typeId;
string sign;
}
Simpily group every member with same prefix to a class. This is needed not only to trim the size, but better maintenance. To reduce size, it's up to you whether you need to include those sub objects to send to the client, since it's a web application, I don't think everytime your client need everything in the object, so while you return your object, consider return just a partial of it, create that in another view model.
I think you have you to follow some basic rule. Having big class create problem to understand the code and maintain it.
- List down all the nouns and Verbs
- Group common nouns and related verbs and create a class
- Repeat same procedure untill there is no noun and verbs remain.
- Now you to give serious thought to created classes and their methods a. Findout Generic and Specialise Classes b. If there is need of design pattern then think about and create relationship like Generalisation and Specialisation, Association, Dependency and realization
The above step will automatic give better shape to classe.
精彩评论