2

In my company we have recently started developing MVC application. Our task is to write the business logic layer and in future it should be with less maintenance.

We have couple of web services to add/update/delete user information.

Now we have to add the business logics like:

If Field1 on the page is 'xxxx' then field2 should be in the range of 1000 to 2000 If field3 is some department then field4 should be only in some sub departments.

So we have to design the layer so that in future our admin(who don't have programming knowledge) can go in and change the logics so that it will work. Please give me some suggestions.

So far what i have got is: Write all these conditions in Model and validate them when user click save button.

Thanks in advance.

user1882705
  • 1,081
  • 4
  • 15
  • 43
  • The best way to do it is some variation of [this](http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs) example.. but there are easier ways to do basic validation such as DataAnnotations. – Trevor Elliott Oct 11 '13 at 15:51
  • If you want a non developer to be able to configure business logic then I might consider a **[rules engine](http://stackoverflow.com/q/250403/2835914)**. – Michael Oct 11 '13 at 16:02

6 Answers6

4

Business logic should kept inside the model. You should aim to have a big Model and a small controller.

You may find this interesting to read this.

Also check Where does the “business logic layer” fit in to an MVC application?

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • You mean i have to hard code all these conditions by if else conditions and keep them in Model – user1882705 Oct 11 '13 at 15:46
  • 1
    You can create a whole class hierarchy inside the model to clearly express your business logic. – Marshall777 Oct 11 '13 at 15:47
  • How to make this editable for a non developer persons in my company?my admin who don't have technical knowledge also should be able to edit in future after we deploy this on production and leave the company. – user1882705 Oct 11 '13 at 15:51
  • So you want that any person can change your Business layer? Seems not very interesting! ;) – Rahul Tripathi Oct 11 '13 at 15:53
  • Not the layer, but the logics? I told my manager that is not possible.but he is insisting on that. – user1882705 Oct 11 '13 at 15:55
  • Not quite sure but are you looking for something like this:- http://stackoverflow.com/questions/10143307/using-editable-attribute-on-mvc-3-view-model ? – Rahul Tripathi Oct 11 '13 at 15:59
  • Business logic should be kept in a different assembly, models and controllers interact with that layer. – Ed DeGagne Oct 11 '13 at 18:31
1

Keep it in a separate assembly which doesn't know about your ui layer. Your models can go here and enforce business rules. I personally like building the business layer on top of the Csla framework, which lets you build rich models with powerful rules. Its geared toward ntier development but I believe its also compatible with ddd.

Andy
  • 8,432
  • 6
  • 38
  • 76
1

I like to use Entity Framework and Fluent Validation to create a domain layer that contains both models and validators. The set up looks like this:

public abstract class DomainEntity
{
    private IValidator validator;

    protected DomainEntity(IValidator validator)
    {
        this.validator = validator;
    }

    public bool IsValid
    {
        get { return validator.IsValid; }
    }

    public ValidationResult Validate()
    {
        return validator.Validate();
    }
}

public class Person : DomainEntity
{
    public int Id { get; set; }
    public string Name { get; set; }

    public Person() : base(new PersonValidator())
}

public class PersonValidator() : AbstractValidator<Person>
{
    public PersonValidator()
    {
         ... validation logic
    }
}

Using this set up, my models and validators live in the same layer but I don't muddy up my model classes with busines logic.

Neil Smith
  • 2,565
  • 1
  • 15
  • 18
1

When you are talking about layering, your Business layer should be separated from Presentation Layer. ASP.NET MVC is a presentation technology; so, your Business Layer would be in different assembly. Additionally, your Business Model wouldn't be used directly in your views; you can use ViewModel to validate user input and when everything was OK, transfer ViewModel data into Business Entity.

If you are interested to obtain more information about layering in enterprise level applications I recommend you Microsoft Spain - Domain Oriented N-Layered .NET 4.0 Sample App.

Abbas Amiri
  • 3,074
  • 1
  • 23
  • 23
  • Microsoft Spain - Domain Oriented N-Layered .NET 4.0 Sample App - http://microsoftnlayerapp.codeplex.com/ - the link is dead – mvark Sep 16 '15 at 06:03
  • 1
    @mvark, some new information has been provided by the author at http://blogs.msdn.com/b/cesardelatorre/archive/2010/03/26/our-brand-new-ddd-n-layer-net-4-0-architecture-guide-book-and-sample-app-in-codeplex.aspx – Abbas Amiri Sep 16 '15 at 06:53
0

You can use DataAnnotations to do this - in fact data annotations enable more than just server side enforcing of model validity. They can also give hints to Entity Framework and client side scripts in order for database/client side validation, and add metadata to methods and properties that MVC can inspect

e.g. for a model:

class PersonDetailsModel
{
    [Required("Please enter a name")] // Don't allow no value, show the message when the rule is broken (if client side validation is enabled it shows when you tab off the control)
    [DisplayName("Full Name")] // Hint for MVC - show this when using the helper methods e.g. in MVC4 Razor syntax @Html.LabelFor(model => model.Name)
    public string Name { get; set; }
}

And yes, keep as much business logic in the business layer (model). Cross-cutting concerns aside, your components should be as loosely coupled as possible. This way there is a central place to make changes, your code is more testable and maintainable and it also helps you to keep your programming consistent (which helps newbies to your project get up to speed quickly)

If you have more complex rules, you can write EF validators.

http://msdn.microsoft.com/en-gb/data/gg193959.aspx

If you aren't using Entity Framework then you might want to consider it - if you are using another ORM then obviously use the tools that support that. If you aren't using an ORM, then there are alternatives but you'll have to write some plumbing code

Charleh
  • 13,749
  • 3
  • 37
  • 57
0

Business Logic should be in Model layer, and I don't think that anyone without programming knowledge can change business logic, he must have basic programming knowledge at least

VahiD
  • 1,014
  • 1
  • 14
  • 30