ASP.NET MVC: Where should this business logic go?

I am working on my first real MVC application, and I try to follow the general recommendations of OOP. I am reorganizing the simple business logic that I used in the controller in my domain model. I've been reading lately, and it seems pretty clear that I have to put the logic somewhere in the domain model entity class to avoid the anti-anomalous domain model anti-pattern.

The application will allow people to buy leasing for parking spaces. Rates are determined by the length of the seat and regardless of whether the client is a member of the business park.

So, I have entity classes in my domain model that look like this (simplified):

public class Customer { int ID { get; set; } string Name { get; set; } bool IsMember { get; set; } } public class ParkingSpace { int ID { get; set; } int Length { get; set; } } public class ParkingSpaceLease { int ID { get; set; } DateTime OpenDate { get; set; } DateTime CloseDate { get; set; } Customer Customer { get; set; } ParkingSpace ParkingSpace { get; set; } } 

Edit: Just to clarify that LeaseQuote is not an essential class, because it is simply used to display cost breakdowns for prospective customers and is not saved anywhere.

 public class LeaseQuote { int SubTotal { get; set; } int Discount { get; set; } int Total { get; set; } } 

Now, as a function of the application, I need to be able to generate quotes for different combinations of customers and parking spaces. Quotation marks will usually be available outside the context of the actual rental creation, for example, when a customer calls to find out the price.

So what is the best way to do this? Does it make sense to create an instance of a new ParkingSpaceLease object inside the controller only to call the GetQuote method on it?

 var lease = new ParkingSpaceLease(); var quote = lease.GetQuote(length: 168, isMember: true); return Json(quote); 

Or should the LeaseQuote class use a method?

 var leaseQuote = new LeaseQuote(); var quote = leaseQuote.GetQuote(length: 168, isMember: true); return Json(quote); 

It feels weird when introducing logic into the ParkingSpaceLease class. I think that to create a new rental object I feel "difficult" when I know that I am not going to actually do anything with it, except for access to the GetQuote method, which seems like a separate service.

So where should the GetQuote method go and why should it go there?

+4
source share
3 answers

It looks like your LeaseQuote is not an entity and is not a business level class. I mean, you do not store it in the database anywhere, right? And this is not part of another data object.

When i see it

Now, as a function of the application, I need to be able to generate quotes for different combinations of customers and parking spaces. Quotation marks will usually be available outside the context of the actual rental creation, for example, when a customer calls to find out the price.

I think of a method like this

 public LeaseQuote GetQuote(Customer customer, ParkingSpace parkingSpace, int length) 

But with this in mind, I probably also want to store information about the cost of the parking space inside the ParkingSpace object and (if applicable) the customer discount in the Customer object.

Where will it go? In a model class (a business model, not a LINQ or Entity model) that accesses your objects and serves as the supplier of your controller.

Now I know that I am not using your models exactly as written. And it may just be a personal bias. But when I think about data models and data objects, they should not have any addon methods beyond what is returned from the database. They should simply present the data unchanged as they appear in the database. If you work on data that belongs to the level above the data objects.

Update:

What is interesting to me from your example, why would you need to transfer all Entity objects (Customer and Parking Space) instead of the properties needed to perform the calculation?

It depends on your code. Identifying the object itself can be dangerous if the consumption code controls the object. I prefer to convey an object mainly because of what I'm used to. But I also try not to manipulate the entity along the way. This, and I think the method signature reflects what the GetQuote method aims at; this is due to the customer and parking space.

I could also make it so that if later in the Entity there are more fields that may affect the GetQuote method, then the method signature will not need to be changed. In this case, only the implementation for GetQuote needs to be changed.

Short answer: Preference.

+4
source

Just make GetQuote a static method in ParkingSpaceLease.

0
source

I think you may have a slightly distorted model of the property, which will lead to your concern that renting is the wrong place to get a quote. It seems to me that the lease will consist entirely of a parking space, which will be leased, and will be associated only with the client buying the lease. IOW:

 public class ParkingSpace { int ID { get; set; } int Length { get; set; } IEnumerable<ParkingSpaceLease> Leases { get; set; } LeaseQuote GetQuote(Customer customer/*, other relevant parameters */) { ... } } public class ParkingSpaceLease { int ID { get; set; } DateTime OpenDate { get; set; } DateTime CloseDate { get; set; } Customer Customer { get; set; } } public class LeaseQuote { //Properties ParkingSpaceLease GetLease(); } 

EDIT I skipped the part regarding the fact that LeaseQuote is a separate class.

0
source

All Articles