I am using services in MVC. I have a service that perform work for the controller such as the following:
private void Initialize(string dataSourceID)
{
this._accountRepository = StorageHelper.GetTable<Account>(dataSourceID);
}
public bool Delete(Account account)
{
if (!ValidateAccountDeletion(account))
return false;
try
{
_accountRepository.Delete(account);
}
catch
{
return false;
}
return true;
}
I have multiple identical databases and the way I point my service to one or the other is as follows:
[HttpPost]
public ActionResult Delete(BaseViewModel vm)
{
if (vm.SubmitAction == "Delete")
{
_accountService = new AccountService(new ModelStateWrapper(this.ModelState), vm.DataSourceID);
Here I get the database to use information directly from the model.
What I would like to know is this an acceptable way of creating and using a service. Somehow I need to tell my
service what datasource to go against and this if very dynamic.
There’s nothing technically wrong with it.
So if you had 2010 data in DB2010 and you passed 2010 and the data to the view and
it passed it back and then somewhere build a connection based on “DB” and Year, nae problem
If you were passing DB2010, or hard coding it in then web page, then a peer review would be a painful experience for you. Grin.