views:

53

answers:

1

I ask this because i'm using Partial classing to extend my db model, and in doing so some snags i've run into using inheritance and also snags i've run into declaring my db model in another class as a property and just using it like that ie: Public Property DBModelClass as DBModelClassType - however, doign direct partial classing has worked out the best, with no snags... except for an issue i seem to be running into now.

that is, i create new properties with half-page code in the 'Get' part of the property decleration, these are like virtual/new properties that the db or db model isn't supposed to know anything about, the sort of stuff you generate/use but without saving it in your db, as i'm sure all of you have these. every time i submit my DBModel through an action, somehow, one of these properties 'Get' section is running (being called) even though i'venever asked for it to. of course, because it aint ready to run (it shouldn't) errors like null errors get returned (however, they wouldn't error if it were to run only when i asked for it to).

edit: so this second part of my question, are all properties in a model generated from a db (linq to sql) ran or called by the model state automatically? without being asked to do so?

an aside: (i do do a modelstate.isvalid - however, this running occurs before isvalid is called as it errors out before i even see validation information). that being, is the auto calling of all properties on a db model object even when not used normal? i tried using bind(false) and scaffold off on the problemed properties.

and the first part of my question, just as a reminder, is if i should or should not be using partial classing as a substitute to inheritance when extending db model's? thanks.

+2  A: 

You shouldn't be modifying your domain, I would recommend you using View Models:

Controller -> Model -> Mapper -> View Model -> View

Define View Model classes which will reflect your views and not the Domain Model. Then you could use AutoMapper to convert from those domain objects that your controller manipulates into view models and pass them to the respective views.

This post on Jimmy Bogard's blog is also worth reading.

Darin Dimitrov
i'm not sure i understand what you are saying. i have a db object, such as a 'product' for sale. suppliers add the product which gets posted to an action, the action grabs the needed bits and saves it in db and returns an updated view. problem of later-added properties being called without me actually calling it still exists while sending new product to the action.
Erx_VB.NExT.Coder
ah, i got ya now, that is in relation to best practice - noted!but then you have to set all the elements values from the domain model to the view model, right? each element one by one? (talking about actual instance values...) or does auto mapper add the values in real time as well somehow?
Erx_VB.NExT.Coder
If the properties have the same names AutoMapper will match them automatically and set their values. If names are different you need to configure them.
Darin Dimitrov