I do not think that there is only one possible answer to this question.
I would recommend a simple but not very general approach. I would write what is called ViewModels, i.e. Model classes related to your specific representations. Then I get your available locations from the controller and populate the ViewModel instance in your controller using the selected locations.
Basically, I would suggest some services, for example:
IEnumerable<Location> GetAvailableLocationsForUser(string userName);
Notice that I used IEnumerable<T> and not IQueryable<T> . Since the implementation actually queries the database, because it is too error prone (at least IMO) if this is the role of the controller for this (remember the deferred execution of IQueryable<T> ). And it returns an instance of the domain, that is, an object, not a map. I personally would not do anything except domain classes at the service level. There may be domain classes not , but, for example, composition of objects. This can help make efficient queries and avoid using lazy loading and delayed execution in controllers. This is useful when the controller needs an entire object graph, not just an entity.
Then I would like to write Models and ViewModels, as shown below, in a web application assembly:
public LocationModel { ... } public CreateItemViewModel : ItemModel { public List<LocationModel> AssociatedLocations { get; set; } public List<LocationModel> AvailableLocations { get; set; } ... }
- Basically, there are models (
ItemModel and LocationModel ) that are objects associated with the web application. This means that in these models there may be some things related to web things, for example, calculated read-only properties or property attributes ( DisplayAttribute ... etc.). I would write these models several times, in fact, because I donβt think that this is something that could be generalized: for example, one view may need to use the navigation property, and another view will not. Thus, this changes the depth of the mapping process depending on the representations that use the model. And I would not use AutoMapper at all (only manual mappers). - There are also ViewModels (
CreateItemViewModel ) that are objects associated with a single view (for example, a view that allows you to create an element in this example). The difference between Model and ViewModel is that the ViewModel is associated with one view (and is specified according to that view). On the other hand, models are associated with several views (their namespace will help you find out which views. For example, xxx.Item.Models for models associated with all views in the xxx.Item directory). ViewModels are created from scratch in the controller (or in a separate mapping) based on the domain classes.
In the example above, you could build domain classes that would return AssociatedLocations and AvailableLocations , but for this you would need your service level to know about the web part (I mean, your service interface and domain classes will know which properties are necessary for a certain kind). I'm not sure that these properties are actually associated with one view in your application, but if it is not, you can also create a domain class as a set of objects that return AssociatedLocations and AvailableLocations :
public ItemExtended : Item { public List<Location> AssociatedLocations { get; set; } public List<Location> AvailableLocations { get; set; } } ItemExtended GetItemExtendedById(long idItem);