I have minimized the problem which is: I have a class that returns customer remainig limit. I initilaize this class just in Mvc Controllers(driver project) not in a service or other project.
public class AccountLimit
{
private readonly IMoneyService _moneyService;
private readonly ILimitService _limitService;
private readonly long _customerId;
public AccountLimit(long customerId,IMoneyService moneyService,ILimitService limitService)
{
_moneyService = moneyService;
_limitService = limitService;
_customerId = customerId;
}
private decimal withdrawalSum { get { return _moneyService.GetTotalWithdrawal(_customerId); } }
private decimal depositSum { get { return _moneyService.GetTotalDeposit(_customerId); } }
private decimal depositLimit { get { return _limitService.GetDeposlitLimit(_customerId); } }
private decimal withDrawalLimit { get { return _limitService.GetwithdrawalLimit(_customerId); } }
public decimal RemainingDeposit{ get { return depositLimit - depositSum; } }
public decimal RemainingWithdrawal { get { return withdrawalLimit - withdrawalSum; } }
}
Some controllers just needs RemaningDeposit, some of them just needs RemaningWithdrawal some of them needs both. One of them:
[Authorize]
public class DepositController : Controller
{
private readonly IMoneyService _moneyService;
private readonly ILimitService _limitService;
private readonly ICustomerService _customerService;
public DepositController(ICustomerService customerService, IMoneyService moneyService, ILimitService limitService)
{
_moneyService = moneyService;
_limitService = limitService;
_customerService = customerService;
}
public ActionResult GetDepositLimit()
{
var customer = _customerService.Find(User.Identity.Name);
var accountLimit = new AccountLimit(customer.Id,_moneyService,_limitService);
return View(accountLimit.RemainingDeposit);
}
}
Then I thought I can use Parameterized Instantiation which is auto-generated factory by Autofac to pass strongly-typed parameters to the resolution function.
Then my class will have interface public class AccountLimit : IAccountLimit And my deposit controller will be like this:
[Authorize]
public class DepositController : Controller
{
private readonly Func<long, IAccountLimit> _accountLimit;
private readonly ICustomerService _customerService;
public DepositController(ICustomerService customerService, Func<long, IAccountLimit> accountLimit)
{
_accountLimit = accountLimit;
_customerService = customerService;
}
public ActionResult GetDepositLimit()
{
var customer = _customerService.Find(User.Identity.Name);
var myAccountLimit = _accountLimit(customer.Id);
return View(myAccountLimit.RemainingDeposit);
}
}
Without Parameterized Instantiation I have new AccountLimit in my code. My controllers have reference of IMoney and ILimitService.
With Parameterized Instantiation I don't have new in my code I don't have references. But If I don't use Autofac how do other developer resolve or initilaize this Func<long, IAccountLimit> _accountLimit ?
Question: I have mixed. Is Parameterized Instantiation neccessary here ? I can test my code without it because I can mock customer, money and limit services. I'm just using this class on controllers, so is Parameterized Instantiation overstatement on here.
Simply: Is my first code without Parameterized Instantiation is anti pattern ? Is it ok or should I use Parameterized Instantiation or custom Factory ?
I couldn't find a satisfying answer for these questions by myself. That's wyh I need your help.
Thanks for your help!
Aucun commentaire:
Enregistrer un commentaire