Last active
December 2, 2016 05:20
-
-
Save elderfo/1c9893c6d0453a6241d44e39c4675b19 to your computer and use it in GitHub Desktop.
Modules Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Project.Controllers | |
{ | |
public class ExampleController : ModuleController | |
{ | |
public JsonResult GetUser(Guid userId) | |
{ | |
UserModel user = Modules.UserLogic.Get(userId); | |
return Json(user, JsonRequestBehavior.AllowGet); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Project.Logic; | |
namespace Project.Controllers | |
{ | |
public abstract class ModuleController : Controller | |
{ | |
protected Modules Modules { get; } = Modules.New(); | |
public override void Dispose() { | |
base.Dispose(); | |
Modules.Dispose(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Project.Data; // enternal dependency | |
namespace Project.Logic | |
{ | |
public class Modules : IDisposable | |
{ | |
private readonly Entities _entities; | |
private readonly User _userLogic; | |
internal static Modules New() | |
{ | |
return new Modules(new Entities()); | |
} | |
private Modules(Entities entities) | |
{ | |
_entities = entities; | |
_userLogic = User.New(_entities); | |
} | |
public User UserLogic { get; } = _userLogic | |
public override void Dispose() { | |
_entities.Dispose(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Project.Data; // enternal dependency | |
namespace Project.Logic | |
{ | |
public class User | |
{ | |
private readonly Entities _entities; | |
internal static User New(Entities entities) | |
{ | |
return new User(entities); | |
} | |
private User(Entities entities) | |
{ | |
_entities = entities; | |
} | |
public UserModel Get(Guid userId) | |
{ | |
User user = entities.Users.Find(userId); | |
// ... transform to UserModel | |
return userModel | |
} | |
// public List<UserModel> Get(Func<Data.User, bool> predicate = null) ... + other methods | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment