c# - MVC, I get it, but separating out the model for repository functions and maybe even business logic... Best practice? -


c# asp .net mvc 4.0

i understand mvc pattern, when comes down model:

public class user {     int id { get; set }     int name { get; set; } } 

i see benefit dividing business logic repository ( data fetchers ). like:

public class userrepository {     ienumerablelist<user> getallusers()     {         ienumerablelist<product> users = //linq or entity;         return ienumerablelist<product> users;       }        int getscorebyuserid( id )               {         int score = //linq or entity;         return score;       }   } 

would business logic go user class like:

public class user {     public int id { get; set }     public int name { get; set; }      public bool hasdiscount( int id )     {         if( getscorebyuserid( id ) > 5 )             return true;         return false;     } } 

does have decent example. it's not easy 1 2 3 find such explicit example me.

does above code seem okay? should repository extend user or should separate class.. or should of stuff go in user class itself?

edit::---- this?

public class userbusinesslogic {     public bool hasdiscount( int id )     {         if( getscorebyuserid( id ) > 5 )             return true;     } } 

edit::---- clarification of how understand now

enter image description here

there few options in situation. described in, example, such book "patterns of enterprise application architecture" martin fowler. depends on architecture pattern going choose.

when trying find solution make smth domain model pattern (user class related business logic , separate userrepository class data access).

you can merge these responsibilities in 1 class user (both business logic , data access) , come active record pattern.

however each pattern has own pros , cons, think better use domain model leads oop , right separation of concerns.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -