Last active
May 21, 2018 07:32
-
-
Save danielplawgo/f9ba0b21d00e4a2a48942e90d1fa1be7 to your computer and use it in GitHub Desktop.
Wstrzykiwanie zależności z wykorzystaniem Lazy
This file contains hidden or 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
Activating: System.Lazy`1[DependencyInjectionWithLazy.Logics.IUserLogic] | |
Activating: DependencyInjectionWithLazy.Controllers.UsersController | |
Before call controller: Users.create (GET) | |
After call controller: Users.create (GET) |
This file contains hidden or 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
Activating: DependencyInjectionWithLazy.Models.DataContext | |
UserRepository.ctor | |
Activating: DependencyInjectionWithLazy.Models.UserRepository | |
Activating: DependencyInjectionWithLazy.Validators.UserValidator | |
UserLogic.ctor | |
Activating: DependencyInjectionWithLazy.Logics.UserLogic | |
Activating: DependencyInjectionWithLazy.Controllers.UsersController | |
Before call controller: Users.create (GET) | |
After call controller: Users.create (GET) |
This file contains hidden or 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
Activating: System.Lazy`1[DependencyInjectionWithLazy.Logics.IUserLogic] | |
Activating: DependencyInjectionWithLazy.Controllers.UsersController | |
Before call controller: Users.create (POST) | |
Activating: System.Lazy`1[DependencyInjectionWithLazy.Models.IUserRepository] | |
Activating: System.Lazy`1[DependencyInjectionWithLazy.Validators.UserValidator] | |
UserLogic.ctor | |
Activating: DependencyInjectionWithLazy.Logics.UserLogic | |
Before call: UserLogic.Add | |
Activating: DependencyInjectionWithLazy.Validators.UserValidator | |
After call: UserLogic.Add | |
After call controller: Users.create (POST) |
This file contains hidden or 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
public class UserLogic : IUserLogic | |
{ | |
private Lazy<IUserRepository> _userRepository; | |
protected IUserRepository UserRepository | |
{ | |
get { return _userRepository.Value; } | |
} | |
private Lazy<UserValidator> _validator; | |
protected UserValidator Validator | |
{ | |
get { return _validator.Value; } | |
} | |
private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger(); | |
public UserLogic(Lazy<IUserRepository> userRepository, | |
Lazy<UserValidator> validator) | |
{ | |
_userRepository = userRepository; | |
_validator = validator; | |
_logger.Info("UserLogic.ctor"); | |
} | |
public Result<User> Add(User user) | |
{ | |
var validationResult = Validator.Validate(user); | |
if (validationResult.IsValid == false) | |
{ | |
return Result.Failure<User>(validationResult.Errors); | |
} | |
UserRepository.Add(user); | |
return Result.Ok(user); | |
} | |
} |
This file contains hidden or 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
public class UserLogic : IUserLogic | |
{ | |
protected IUserRepository UserRepository { get; set; } | |
protected UserValidator Validator { get; set; } | |
private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger(); | |
public UserLogic(IUserRepository userRepository, | |
UserValidator validator) | |
{ | |
UserRepository = userRepository; | |
Validator = validator; | |
_logger.Info("UserLogic.ctor"); | |
} | |
public Result<User> Add(User user) | |
{ | |
var validationResult = Validator.Validate(user); | |
if (validationResult.IsValid == false) | |
{ | |
return Result.Failure<User>(validationResult.Errors); | |
} | |
UserRepository.Add(user); | |
return Result.Ok(user); | |
} | |
} |
This file contains hidden or 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
public class UserRepository : IUserRepository | |
{ | |
private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger(); | |
private Lazy<DataContext> _db; | |
protected DataContext Db | |
{ | |
get { return _db.Value; } | |
} | |
public UserRepository(Lazy<DataContext> db) | |
{ | |
_db = db; | |
_logger.Info("UserRepository.ctor"); | |
} | |
public void Add(User user) | |
{ | |
Db.Users.Add(user); | |
Db.SaveChanges(); | |
} | |
} |
This file contains hidden or 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
public class UserRepository : IUserRepository | |
{ | |
private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger(); | |
protected DataContext Db { get; set; } | |
public UserRepository(DataContext db) | |
{ | |
Db = db; | |
_logger.Info("UserRepository.ctor"); | |
} | |
public void Add(User user) | |
{ | |
Db.Users.Add(user); | |
Db.SaveChanges(); | |
} | |
} |
This file contains hidden or 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
public class UsersController : Controller | |
{ | |
private Lazy<IUserLogic> _userLogic; | |
protected IUserLogic UserLogic | |
{ | |
get { return _userLogic.Value; } | |
} | |
public UsersController(Lazy<IUserLogic> userLogic) | |
{ | |
_userLogic = userLogic; | |
} | |
// GET: Users | |
public ActionResult Index() | |
{ | |
return View(); | |
} | |
public ActionResult Create() | |
{ | |
return View(new UserViewModel()); | |
} | |
[HttpPost] | |
public ActionResult Create(UserViewModel viewModel) | |
{ | |
if (ModelState.IsValid == false) | |
{ | |
return View(viewModel); | |
} | |
var user = new User() | |
{ | |
FirstName = viewModel.FirstName, | |
LastName = viewModel.LastName | |
}; | |
var result = UserLogic.Add(user); | |
if (result.Success) | |
{ | |
return Content("Added"); | |
} | |
result.AddErrorToModelState(ModelState); | |
return View(viewModel); | |
} | |
} |
This file contains hidden or 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
public class UsersController : Controller | |
{ | |
protected IUserLogic UserLogic { get; set; } | |
public UsersController(IUserLogic userLogic) | |
{ | |
UserLogic = userLogic; | |
} | |
public ActionResult Create() | |
{ | |
return View(new UserViewModel()); | |
} | |
[HttpPost] | |
public ActionResult Create(UserViewModel viewModel) | |
{ | |
if (ModelState.IsValid == false) | |
{ | |
return View(viewModel); | |
} | |
var user = new User() | |
{ | |
FirstName = viewModel.FirstName, | |
LastName = viewModel.LastName | |
}; | |
var result = UserLogic.Add(user); | |
if (result.Success) | |
{ | |
return Content("Added"); | |
} | |
result.AddErrorToModelState(ModelState); | |
return View(viewModel); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment