c# - Controller and repository bound to one type- how to use second entity? -
i think worded title poorly.. here's problem:
i'm creating mvc app using ef , have implemented repository layer allow me create unit tests (with addition of ninject).
i have simplified below sake of question , please note i'm extending existing system , cannot combine two. have campaign model
public partial class campaign { public int campaignid { get; set; } public string name { get; set; } public virtual campaigndetails campaigndetail { get; set; } } public partial class campaigndetails { public int campaigndetailsid { get; set; } public int campaignid { get; set; } public system.datetime? sentout { get; set; } public string sentoutby { get; set; } public system.datetime? lastmodified { get; set; } public string lastmodifiedby { get; set; } public virtual campaign campaign { get; set; } } every time campaign created want create campaigndetails` record.
my controller looks this:
private readonly icampaignrepository campaignrepository; public campaigncontroller(icampaignrepository campaignrepository) { this.campaignrepository = campaignrepository; } [httppost] public actionresult create(campaign campaign) { if (modelstate.isvalid) { try { campaignrepository.insert(campaign); campaignrepository.save(); return redirecttoaction("index"); } catch (exception ex) { modelstate.addmodelerror(string.empty, "something went wrong. message: " + ex.message); } } return view(campaign); } as can see strongly-typed use campaign, so should create campaigndetails?
would have separate campaigndetail controller (that has no ui) , repository, , call campaigndetailcontroller.add(campaign.id) ?
or services come in handy?
[httppost] public actionresult create(campaign campaign,campaigndetails campaigndetails ) { if (modelstate.isvalid) { try { //get campaigndetails http parameters or seed in other way campaign.campaigndetails = campaigndetails; campaignrepository.insert(campaign); campaignrepository.save(); return redirecttoaction("index"); } catch (exception ex) { modelstate.addmodelerror(string.empty, "something went wrong. message: " + ex.message); } } return view(campaign); }
Comments
Post a Comment