Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to have some logic codes inside a property of a data model class?

Tags:

c#

asp.net-mvc

I am learning MVC 3 and I have not found people using some logic codes inside a property of a data model class.

They do the data model class as follows (for example):

public class Customer
{
    public int CustomerId {get;set;}
    //other properties without any logic code.
}

Is it ok to have logic codes inside a property as follows?

public class Customer
{
    private int customerId;
    public int CustomerId {
       get{return customerId;}
       set
       {
         customerId=value;
         // some logic codes go here.
       }
    }
    //other properties go here.
}

Edit 1:

This is my real scenario:

Child table data model:

namespace MvcApplication1.Models
{
    public class Choice
    {
        public int ChoiceId { get; set; }
        public string Description { get; set; }
        public bool IsCorrect { get; set; }
        public QuizItem QuizItem { get; set; }
    }
}

Parent table data model:

namespace MvcApplication1.Models
{
    public class QuizItem
    {
        public int QuizItemId { get; set; }
        public string Question { get; set; }

        private IEnumerable<Choice> choices;
        public IEnumerable<Choice> Choices
        {
            get { return choices; }

            set
            {
                choices = value;
                foreach (var x in choices)
                    x.QuizItem = this;
            }
        }
    }
}

Consumer:

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {


            var data = new List<QuizItem>{
                new QuizItem
                {
                    QuizItemId = 1,
                    Question = "What color is your hair?",
                    Choices = new Choice[]{
                        new Choice{ ChoiceId=1, Description="Black.", IsCorrect=true},
                        new Choice{ ChoiceId=2, Description="Red.", IsCorrect=false},
                        new Choice{ ChoiceId=3, Description="Yellow.", IsCorrect=false}
                    }
                },
                new QuizItem
                {
                    QuizItemId = 2,
                    Question = "What color is your noze?",
                    Choices = new Choice[]{
                        new Choice{ChoiceId=1, Description="Pink.", IsCorrect=false},
                        new Choice{ChoiceId=2, Description="Maroon.", IsCorrect=true},
                        new Choice{ChoiceId=3, Description="Navy Blue.", IsCorrect=false}
                    }
                }
            };


            return View(data);
        }

    }
}
like image 481
xport Avatar asked Feb 07 '11 10:02

xport


People also ask

Should model classes contain logic?

Models should contain logic that deals with manipulating data. For example, say we have a post model, the model should contain methods that get the comments of a particular post. In Sails models, each model has static methods like find , findOne , update , etc.

Should ViewModel contain business logic?

ViewModel: ViewModel is the middle layer between the view and model. ViewModel contains the business logic, which manipulates the row data to show in the view. Any kind of function and methods should be in the view model.

Where do you put business logic?

The business logic should be placed in the model, and we should be aiming for fat models and skinny controllers. As a start point, we should start from the controller logic. For example: on update, your controller should direct your code to the method/service that delivers your changes to the model.

What does the model do in MVC?

The Model is the part of MVC which implements the domain logic. In simple terms, this logic is used to handle the data passed between the database and the user interface (UI). The Model is known as domain object or domain entity. The domain objects are stored under the Models folder in ASP.NET.


1 Answers

This calls for a method. Two reasons why:

  • I don't recommend setters for Collections
    • Property Usage Guidelines - Setting a property for each item in collection every time property is set is expensive and should not be in a property. A method is preferred instead.
  • Code (that you have in your case) in setter causes enough side-effects to disqualify use of property
    • Setters for collection type properties - A discussion on StackOverflow regarding setters for collections.

I suggest following:

public class QuizItem
{
    public int QuizItemId { get; set; }
    public string Question { get; set; }

    private IEnumerable<Choice> choices;
    public IEnumerable<Choice> Choices
    {
        get { return choices; }
    }

    public void SetChoices(IEnumerable<Choice> choices)
    {
        foreach (var x in choices)
            x.QuizItem = this;

        this.choices = choices;                
    }
}
like image 102
decyclone Avatar answered Oct 05 '22 23:10

decyclone