I am working with a CRM product that uses ASP.net MVC 3.0, Entity Framework and Windsor for IOC container.
I have injected the services which are work with repository layer to controller through Windsor.
However, I have implemented the strategy pattern for my ContactController
to facilitates MyProfile
, CustomerProfile
, CompanyProfile
.
Now my ContactContext
class is depended on my ContactController
class so that i can't inject ContactContext
using Windsor as a result of that i have created instance of ContactContext
in ContactController
.
ContactController
class implementation
public class ContactController:BaseController
{
private ContactContext _contactContext;
public ContactController(PersonService personService, CompanyService customerService)
{
_contactContext = new ContactContext(personService, customerService, this);
}
public ActionResult ContactProfile(string profileId, string profileType)
{
base.ValidateContactProfileIdAndProfileTypeInfo(profileType, profileId);
return _contactContext.RenderContactProfile(ProfileType, ProfileId);
}
}
ContactContext
implementation
public class ContactContext
{
private Dictionary<ProfileType, IContactStrategy> _strategies =
new Dictionary<ProfileType, IContactStrategy>();
private BaseController _controller;
public ContactContext(PersonService personService, CompanyService companyService, BaseController controller)
{
_strategies.Add(ProfileType.MyProfile, new MyProfileStrategy(personService));
_strategies.Add(ProfileType.CustomerProfile, new PersonStrategy(personService));
_strategies.Add(ProfileType.CompanyProfile, new CompanyStrategy(companyService));
_controller = controller;
}
public ActionResult RenderProfileInfo(ProfileType profileType, long profileId)
{
return _strategies[profileType].GenerateProfileInfoView(profileId, _controller);
}
public ActionResult RenderPeopleInfo(ProfileType profileType, long profileId)
{
return _strategies[profileType].GeneratePeopleInfoView(profileId, _controller);
}
}
Strategies go like this
public class PersonStrategy:IContactStrategy
{
private PersonService _personService;
public PersonStrategy(PersonService personService)
{
_personService = personService;
}
#region Implementation of IContactStrategy
public ActionResult GenerateProfileInfoView(long profileId, BaseController controller)
{
//TODO: Load Profile info from service
PersonDetailsViewModel personDetailsViewModel = new PersonDetailsViewModel();
personDetailsViewModel.Name = "Robert Martin";
return controller.RenderPartialView("ProfileInfo", personDetailsViewModel);
}
public ActionResult GeneratePeopleInfoView(long profileId, BaseController controller)
{
//TODO: Load people from service
return controller.RenderPartialView("PeopleView", new List<PersonLiteViewModel>());
}
}
public class CompanyStrategy : IContactStrategy
{
private CompanyService _companyService;
public CompanyStrategy(CompanyService companyService)
{
_companyService = companyService;
}
#region Implementation of IContactStrategy
public ActionResult GenerateView(long profileId, BaseController controller)
{
throw new NotImplementedException();
}
public ActionResult GenerateProfileInfoView(long profileId, BaseController controller)
{
throw new NotImplementedException();
}
}
Question: How can i get rid of ContactContext
dependency with ContactController
?