3

I have a simple structure (struct) which I want to place in my asp.net MVC application.

public struct QuantityAndSize
{
    public decimal quantity;
    public string size;
}

A struct is not really a model and it isn't a view or a controller either. Maybe I shouldn't use structs in the first place, so where should I place it or should I avoid it.

EDIT

This question is not performance related. The main purpose of this question is to get the Structure of the project correct so that it will meet best practice

Bongo
  • 2,933
  • 5
  • 36
  • 67
  • If it semantically belongs alongside the models, why do you think it shouldn't go there? If you have some other semantic grouping of code then why not make a folder to hold that group? It's not really clear why this is a problem... – David Dec 21 '15 at 12:05
  • It stores the quantity and the size of an object. For example I have an object and Order for a Board. The User wants to buy 2 Boards of the size 50cm – Bongo Dec 21 '15 at 12:07
  • The user fills in a Form where he places the order. The order is then stored in the model and will be send via email – Bongo Dec 21 '15 at 12:09
  • So you **are** using it as a model? Again, why did you decide on using a struct? – CodeCaster Dec 21 '15 at 12:09
  • It is a piece used inside a model. So i guess it belongs to the model but it isn't a model for itself – Bongo Dec 21 '15 at 12:10
  • 1
    A part of a model is still a model. If you don't have compelling reasons to use a struct, use a class. – CodeCaster Dec 21 '15 at 12:11
  • 1
    @CodeCaster okay I thought it would be more fancy like placing it in some sort of extra directory or something but placing it alongside the models seems to be valid to I guess. Thanks – Bongo Dec 21 '15 at 12:15

1 Answers1

1

It all depends on use cases. You have to answer yourself why you have used struct in the first place.

Personally I find structs rather exotic beings in my web applications because of the fact than they are immutable. The good example of using struct is introducing a Range<T> structure, which can be used as generic range to prevent creating similar beings in my code. Each range should be immutable and I want each to be unique - it seems like a good use case.

I don't see any good use cases when interacting between layers in MVC and I don't see any real use case when introducing some business concepts. On the other hand, if you want to gather some values into one and use it as a value type, using struct is perfectly fine IMO.

kamil-mrzyglod
  • 4,948
  • 1
  • 20
  • 29