Skip to main content
edited body
Source Link
Travis
  • 1.3k
  • 6
  • 14

My favorite bit of uncomprendable magic!

public class Blah { IEnumerable<T> Wrap(Func<IEnumerable<T>Func<Entities, Entities>IEnumerable<T>> act) { using(var entities = new Entities()) { return act(entities); } } public List<Employee> GetAllEmployees() { return Wrap(e => e.Employees.ToList()); } public List<Job> GetAllJobs() { return Wrap(e => e.Jobs.ToList()); } public List<Task> GetAllTasksOfTheJob(Job job) { return Wrap(e => e.Tasks.Where(x ....).ToList()); } } 

Wrap exists only to abstract that out or whatever magic you need. I'm not sure I would recommend this all the time but it's possible to use. The "better" idea would be to use a DI container, like StructureMap, and just scope the Entities class to the request context, inject it into the controller, and then let it take care of the lifecycle without your controller needing to.

My favorite bit of uncomprendable magic!

public class Blah { IEnumerable<T> Wrap(Func<IEnumerable<T>, Entities> act) { using(var entities = new Entities()) { return act(entities); } } public List<Employee> GetAllEmployees() { return Wrap(e => e.Employees.ToList()); } public List<Job> GetAllJobs() { return Wrap(e => e.Jobs.ToList()); } public List<Task> GetAllTasksOfTheJob(Job job) { return Wrap(e => e.Tasks.Where(x ....).ToList()); } } 

Wrap exists only to abstract that out or whatever magic you need. I'm not sure I would recommend this all the time but it's possible to use. The "better" idea would be to use a DI container, like StructureMap, and just scope the Entities class to the request context, inject it into the controller, and then let it take care of the lifecycle without your controller needing to.

My favorite bit of uncomprendable magic!

public class Blah { IEnumerable<T> Wrap(Func<Entities, IEnumerable<T>> act) { using(var entities = new Entities()) { return act(entities); } } public List<Employee> GetAllEmployees() { return Wrap(e => e.Employees.ToList()); } public List<Job> GetAllJobs() { return Wrap(e => e.Jobs.ToList()); } public List<Task> GetAllTasksOfTheJob(Job job) { return Wrap(e => e.Tasks.Where(x ....).ToList()); } } 

Wrap exists only to abstract that out or whatever magic you need. I'm not sure I would recommend this all the time but it's possible to use. The "better" idea would be to use a DI container, like StructureMap, and just scope the Entities class to the request context, inject it into the controller, and then let it take care of the lifecycle without your controller needing to.

Source Link
Travis
  • 1.3k
  • 6
  • 14

My favorite bit of uncomprendable magic!

public class Blah { IEnumerable<T> Wrap(Func<IEnumerable<T>, Entities> act) { using(var entities = new Entities()) { return act(entities); } } public List<Employee> GetAllEmployees() { return Wrap(e => e.Employees.ToList()); } public List<Job> GetAllJobs() { return Wrap(e => e.Jobs.ToList()); } public List<Task> GetAllTasksOfTheJob(Job job) { return Wrap(e => e.Tasks.Where(x ....).ToList()); } } 

Wrap exists only to abstract that out or whatever magic you need. I'm not sure I would recommend this all the time but it's possible to use. The "better" idea would be to use a DI container, like StructureMap, and just scope the Entities class to the request context, inject it into the controller, and then let it take care of the lifecycle without your controller needing to.