I am wondering if I have a design flaw in my solution. Here's what I have:
Entities
=> pure poco. refs: none.Data
=> data access. refs:Entities
.Service
=> business logic. refs:Entities
andData
- Manager Classes.
- ViewModels.
WebApp
=> UI. Refs:Entities
andService
WebApp
ASP.NET MVC project as UI, Entities
project with no reference for holding pure POCO. Data
to access database and Service
for Business Logic (where my Manager classes are).
Basically, I have defined a Manager
class for every entity. For exmaple, I have a Message
entity with relation to a list of Recipient
entity. I have a MessageManager
and RecipientManager
class, responsible for both CRUD operations using Data layer and logical results (e.g. public List<Message> GetAllMessagesWithPermissionForUser(User user, Permission permission)
)
For my MVC project I have defined some ViewModel classes in Service layer to generate specific videmodels for my views. Since the viewmodels are using Manager classes, I have defined them in my Service Class. For example I have a MessageOperationVM
viewmodel which has a PermittedBoxesToSend
property. This property uses my BoxManager
class to get all boxes permitted for the specified message:
// Initialized by Catsle Windsor.
public BoxManager BoxManager {get; set;}
public List<Box> PermittedBoxesToSend
{
if(this._premittedBoxesToSend != null)
{
this._permittedBoxesToSend = BoxManager.GetPermittedBoxesToSend(this.Message);
}
}
I am not sure if using manager classes in Viewmodels are a good design. Although I've defined them as constructor/property setters to get populated with DI. Should I populate my viewmodels' properties in my controllers instead of defining properties and get rid of manager classes in my ViewModels?
public ActionResult ShowNewMessageDialog() { var messageVM = new MessageOperationVM() { new Message() }; messageVM = this.BoxManager.GetPermittedBoxesToSend(); }
using a manager class for each entity seems to make maintenance hard. (Although they all derive from a
BaseManager
class which shares the operations they have in common)- Is there any reconsideration worth mentioning in the above design?
Thank you.
Update:
based on eulerfx's answer:
The problem I have with your answer is: To construct a ViewModel, I have to call some service layer's methods. So I cannot construct my ViewModel just based on my poco entity. Do you suggest that I should construct those parts in controller as well? :
public ActionResult ShowNewMessageDialog()
{
var message = this.messageRepository.GetMessage();
var messageVM = new MessageViewModel(message);
messageVM.CustomProperty = this.messageManager.CallSomeMethod(message);
return View(messageVM);
}