Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere should my Business Logic go?
    primarykey
    data
    text
    <p>I have the following seperation of logic in my application, as you can see I have a class called SimpleController which is what I use to Import and Find SimpleEntities. </p> <p>I read all the SimpleEntities into memory List cause I search these Entities very often and its a lot faster then reading from the database everytime I want to search for an Entity.</p> <p>Would it be better to move the logic where I read and store the SimpleEntities into memory into the SimpleLogic class instead of the SimpleController class?</p> <pre><code>public class SimpleEntity { public int SimpleId { get; set; } public string SimpleName { get; set; } } public class SimpleDAL { public ICollection&lt;SimpleEntity&gt; GetAllSimpleEntities() { //Retrieve SimpleEntities from Database } public void InsertSimpleEntity(SimpleEntity simpleEntity) { //Insert simple Entity into Database } } public class SimpleLogic { private readonly SimpleDAL simpleDAL = new SimpleDAL(); public ICollection&lt;SimpleEntity&gt; GetAllSimpleEntities() { return simpleDAL.GetAllSimpleEntities(); } public void InsertSimpleEntity(SimpleEntity simpleEntity) { //Validate simpleEntity before adding to database if (simpleEntity.SimpleId &lt;= 0) throw new Exception("Invalid SimpleEntity Id: " + simpleEntity.SimpleId); if (String.IsNullOrEmpty(simpleEntity.SimpleName)) throw new Exception("SimpleEntity Name cannot be empty or null"); simpleDAL.InsertSimpleEntity(simpleEntity); } } public class SimpleController { private readonly SimpleLogic simpleLogic = new SimpleLogic(); private List&lt;SimpleEntity&gt; simpleEntities; public SimpleController() { simpleEntities = simpleLogic.GetAllSimpleEntities().ToList(); } public int FindSimpleIndex(int simpleId) { return simpleEntities.FindIndex(p=&gt; p.SimpleId == simpleId); } public void ImportOtherSimpleEntity(OtherSimpleEntity otherSimpleEntity) { if (otherSimpleEntity.Operation == "Update") { int index = FindSimpleIndex(otherSimpleEntity.OtherSimpleId); //If entity was found update SimpleEntity if (index &gt; -1) { //Call SimpleLogic.UpdateSimpleEntity(Pass in SimpleEntity); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload