Bast Practise: ASP.NET MVC Controller/Action with multiple object types -


I'm looking for the best method for the following problem.

I currently have a large number of objects that are inheritors for all the same base objects and all are very similar. Is there a solution available, allowing anyone to take action and edit the action without the need to repeat the same code.

For example, I may have an individual object:

  public class PersonBase {public string first name {get; Set; } Public String LastName {get; Set; }}  

And then I will have several objects that come from person :

  public class salesperson: PersonBase {Public Double Total Tanning {Received; Set; }} Public class customer: Personbase {public string address {get; Set; }}  

Now I have a task that will create a customer base:

  [http post] Create public virtual actionless (formulation collection) { Var person = new personality (); UpdateModel (person); If (Model.IsValid & amp; amp; person. ISI Walid) {// Save to the db} Return View (); }  

Now I can easily copy this code and modify it so that I can be a salesperson and customer, but I have a large number of items based on personbabs And it will be very much the repetition of the same code which I would like to save.

Is there a way to make action more normal for all types of people?

Thank you

The solution that works for me is from C # 4 to Dynamic was to use. So my code looks like this:

  [httppost] Create a public virtual action officer (int type, form collection collection) {dynamic person = new business message (); If (type == 1) person = new SalesPerson (); And if (type == 2) person = new customer (); UpdateModel (person); If (Model.IsValid & amp; amp; person. ISI Walid) {// Save to the db} Return View (); }  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -