Where to put "ViewModel Classes" in DDD approach? - domain-driven-design

As you may have seen the question from the title, i'm little bit confused about where to put the ViewModel classes in our web project. What is the best practice to putting ViewModel classes? Seperate assembly or in UI project?
We're working on a project that is built upon Asp.net MVC2 with DDD approach.
Thank you.

As the name suggests ViewModel is the Model for the View and I consider them property of the Presentation Layer
I'd like to keep it in the Model folder in the MVC project.
For example a PersonDetail view can have a PersonModel and a PersonController.
I keep the PersonModel in the MVC project and I return it from the PersonController to the view.
In practice I have a lot of ViewModel in the UI project, at least one for each view and they act like DTOs.

you can put them in your presentation layer (WebUI for me in asp.net mvc) or in your infrastructure project, or you can create a Dto project and put them in there

Related

MvcSiteMapProvider that supports sub areas

I'm looking for a SiteMap provider for use in an MVC 5 application that can support sub areas.
The folder structure in my project notionally looks something like:
Project Root
Areas
Area A
Controllers
Models
Views
Areas
Area A1
Controllers
Models
Views
I need an MVC SiteMapProvider that allows me to use controllers located in the A1 directory.
I've played with the MvcSiteMapProvider (https://github.com/maartenba/MvcSiteMapProvider), and it does not seem to support sub areas in the sitemap file. Is anyone aware of a solution for creating breadcrumbs with this type of setup?
Since MVC doesn't support sub-areas without advanced customization, MvcSiteMapProvider does not support them either.
However, there is a project called MvcCodeRouting that supports multiple levels of controllers as an alternative to MVC areas, and MvcSiteMapProvider has support for it.
Found a good enough answer for the time being:
Using MvcSiteMapProvider, initially I was trying to use the Area, Controller, and Action properties in the mvcSiteMapNode to reach the A1 controller. This was unsuccessful. But if I used the URL property, that worked.
https://github.com/maartenba/MvcSiteMapProvider/wiki/Defining-Sitemap-Nodes-in-XML

Circular Referencing between projects in Visual Studio

I have built two projects in my VS solution. One is a MVC web applicaition and other one is a standard C# windows form project. I want to transfer data both ways. For exmaple the input from my MVC web application to the form and then output back to my web application from the C# form project. However, I can only do refrenceing from one side and access the static variblias of only one project from another. To achieve my goal, I need to do circular referencing which VS is not allowing me. Is there a workaround to my solution.
PS: My main goal is to access all the static members of every class into every other class in my whole solution.
Separate the static functions in a separate library and then reference that from windows or web application. That way you will also know what are common functions and what functions are specific to those feature or class.
It is not a good design to let "access all the static members of every class into every other class". It violate OOPS principles.
If its shared data save it to any repository and access it from there from both of your application.

MVC 4 unable to perform scaffolding

I'm new to MVC, and my intention actually is to learn 'Code First' technique, using MVC 4. I'd been through several 'MVC 4 Entity Framework' tutorials, and no doubt, tutorials are really simple, and understandable. but for every tutorial I followd, the
EF will look for the database in the default location and if it doesn’t exist, it will create it for you
part is not working for me. Do i have to add ADO.net Entity framework model in project, and design the database entities firt? or am I missing something? It's been more than 15 days, & I'm stuck.
(I tried making database manually first, and it worked perfect. But I want to learn, as described in tutorials, that databases are automatically created, if we mention the connection string, and EntityFramework finds out that path doesn't actually contains such database, so it creates one).
I'd been through several tutorials, let's say, I'm following THIS one.
(I'm using Visual Studio Express for web 2012, EF 5.0.0)
your question: Do i have to add ADO.net Entity framework model in project, and design the database entities firt?
yes, in code first approach you need to tell entity framework what tables you are going to create, you do it by creating poco classes(Plain old CLR Object). then in run time entity framework will create a model(witch is a xml file) and generate query according to that xml file then executes it. you can read more about that in [CSDL MSL CSDL][1]
so for summary you write your domain classes(poco classes) and ef will create db for you but btw there is another way where in entity framework code first witch is called [Code first to existing database][2]
[2]: http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx it revers engineer the databse and create model for you.
Do i have to add ADO.net Entity framework model in project, and design the database entities first?
This is really the more traditional approach, aptly called the database-first approach. The code-first approach is an alternative to the former. They mean exactly what they say. In the case of code-first the code is first written and in effect the database is generated automatically. In database approach, the database is first created then you can use EF to automatically generate the code required to handle the database, mainly CRUD and connection management.
The built-in scaffolder in ASP.NET MVC should work fine either in code-first or database-first as long as you have your models already established in your project. If you find the generated code too simplistic, an alternative scaffolder that you can use is CamoteQ - it is an online ASP.NET MVC scaffolding tool that is based in DDD principles and recognized patterns.

Security system design in MVC application

Which design approach can be used, already exists or new ones to implement security system in MVC web-application?
Possibly there are patterns, best practices, e.g. in some popular languages, e.g. Java, .Net or whatsoever?
On which level is it better to implement it: model or controller or something between them?
I faced that primitive approach results in spreading security checks among many controllers or models depending on implementation, mixing with code of levels.
But it is not obvious for me in which way to design security in better way.
I have an MVC application.
I need flexible system of access rights.
I have and hierarchy of categories and entities in categories.
Some user can edit/view/add/remove one on set of categories, another users - other categories.
It is also required that depending on user role some fields of model should not be retrieved from DB (null should be returned)
Admins should be able to assign different access rights.
You should be using .Net's built in member ship providers. By default a new MVC 3 Internet Application Visual Studio project template will give you basic login code. Clicking on the 'manage' your site button from within visual studio will give you the web interface to manage the users and roles. Create your users/roles, then define [Authorize(Roles="Admin,Users")] on your Controller class definition or on your method definitions. DO NOT define URL access as was done in Asp.Net Web Form applications as there are multiple URLs that could map to a single place. Use the Authorize attribute instead.
Also check my response here:
What features do I need to have before I open an ASP.Net app onto the internet?
In addition make sure you use Html.AntoForgeryToken in your views and [ValidateAntiForgeryToken] on your [HttpPost] controller methods (ie any methods you post back to)

Subsonic Question, is it possible execute templates during runtime?

Before ask my question, i have to say that subsonic is wonderfull tool, i realy like it.
I have an application which is createg domain layer during the runtime itself. I mean It has got customizable entites and when the entity added or any entity schema changed my model layer compiled again in the runtime according to changed database.
I'm wondering that, is it possible to execute subsonic templates during the runtime or when the user changed enetity schema or add to new entity to the application can it automatically generate the model layer again ? or how to trigger the template during the runtime of Subsonic ?
Thank you
If you make some modifications to the templates you can use the TextTransform.exe command line tool to generate SubSonic classes as described in the following answer:
SubSonic ASP.NET MVC sample in Visual Web Developer Express
You could then automate the execution of the commandline when your user modifies the schema.

Resources