ASP.NET MVC / DDD architecture support
I am creating a web application using ASP.NET MVC and I am trying to use domain driven design. I have an architecture question.
I have a WebControl table to store keys and values for lists so that they can be editable. I have incorporated this into my business model, but it results in a lot of redundant code and I'm not sure if it is there. For example, in my Request class, I have a property called NeedType. Since this comes from a list, I created a NeedType class to provide values for the radio buttons. I'm only showing one example here, but the form will probably have a dozen lists that should come from the database.
[edit to clarify the question] What's the best way to do this? Are these list objects really part of my domain, or are they just for the UI? If it's not part of a domain, then they don't belong to my main project, so where do they go?
public class Request : DomainObject
{
public virtual int RequestId { get; set; }
public virtual DateTime SubmissionDate { get; set; }
public virtual string NeedType { get; set; }
public virtual string NeedDescription { get; set; }
// etc.
}
public class NeedType : DomainObject
{
public virtual int NeedTypeId { get; set; }
public virtual string NeedTypeCode { get; set; }
public virtual string NeedTypeName { get; set; }
public virtual int DisplayOrder { get; set; }
public virtual bool Active { get; set; }
}
public class RequestController : Controller
{
private readonly IRequestRepository repository;
public RequestController()
{
repository = new RequestRepository(new HybridSessionBuilder());
}
public RequestController(IRequestRepository repository)
{
this.repository = repository;
}
public ViewResult Index(RequestForm form)
{
ViewData.Add("NeedTypes", GetNeedTypes());
if (form == null)
{
form = new RequestForm();
form.BindTo(repository.GetById(125));
}
}
private NeedType[] GetNeedTypes()
{
INeedTypeRepository repo = new NeedTypeRepository(new HybridSessionBuilder());
return repo.GetAll();
}
}
a source to share
Create a separate model with the required data in your view. Model in M MVC is not the same as domainmodel. MVC models are dumb DTOs with no behavior, just properties. The domain model has as much behavior as possible. Domain model with get; set; properties are considered only as an anti-pattern, called the "anemic domain model". There are 2 places where most people put viewmodels: in the web layer, next to views and controllers, or in the app service layer.
Edit:
When all you need to do is display a list of all database needs and one query in your view, I would actually create one view model with a query and a list of needtypes as properties. I don't think calling multiple repos in a controller is a smell unless you have a larger application and you might need a separate app service layer that returns the entire view model with a single method call.
I think it would be nice to follow Todd Smith's advice on object of value. When needs can be added or edited by users at runtime, needtype must be an entity. When needtypes are hardcoded and only changed with new versions of the project, needtype should be a value object and the needtypes list can be populated with something like NeedType.GetAll () and stored in the database by adding a column to the query table instead of a separate needs table ...
a source to share
If it comes from the list then I bet it is a foreign key. Don't think about your user interface when designing your domain model. This is just a case where NeedType is a foreign key. Replace the NeedType string with a reference to the actual NeedType object. In your database, this will be the id reference.
When you create your list of NeedType options, you just need to pull out each NeedType. Perhaps keeping it cached would be a good idea if it didn't change much.
a source to share
Your NeedType looks like a value object to me. If it is read-only data, then it should be considered a value object in the DDD architecture and is part of your domain.
Many people run into the "omg so much redundancy" issue when working with DDD, since you are no longer using the old Database -> DataTable -> UI approach.
a source to share