Moving the models in mvc 3 from web project to class library
I am creating an application in MVC3. On creating the application, there were three folders created in the Web Project:
- Controllers
- Models
- Views
I want to implement a layered architechture, hence i created a class library project which will have the business and repository layers.
I moved the models from the web project to class library project so that i can use the models in the business layer. The model classes contain the required properties and their validations as specified below:
public class ChangePasswordModel
{
[Required]
[DataType(DataType.Password)]
[Display(Name = "Current password")]
public string OldPassword { get; set; }
[Required]
[ValidatePasswordLength]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm new password")]
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
public sealed class ValidatePasswordLengthAttribute : ValidationAttribute, IClientValidatable
I also have a class in the same file above to validate my password length which implements the interface IClientValidatable
I am facing two problems:
- I am unable to use the CompareAttribute as well as the interface
IClientValidatable
. Both of the mentioned classes area apart of System.Web.MVC. I have added the reference to theSystem.Web.MVC
dll to my class library.
In both the cases i am unable to resolve the issue. The resharper mentions the error relating to circular dependencie开发者_开发知识库s.
Can i not use the System.Web.Mvc
reference and use the classes and validations provided in a class library?
Additionally, the validations provided in System.ComponentModel.DataAnnotations
do not create an issue.
I am unable to build the solution and hence cannot proceed with the development. Any solutions and suggestions are welcome.
You view models are a concern of the UI layer. Hence they should not be moved to a class library.
Do not confuse view models with your models in the business layer. View models are used to represent the business model in the view. Use a mapping layer like AutoMapper to copy information from your business model to your view model.
More info about view models: http://blog.gauffin.org/2011/07/three-reasons-to-why-you-should-use-view-models/
Other than that, the circular dependency do not have anything to do with ASP.NET or MVC. You are most likely trying to add a reference from project A to project B and vice versa. Not possible.
I was having the same problem. I am not sure if this will work for you or will work developing forward but the fix is very easy, add a reference to EntityFramework on your Library and it will take the Errors Away.
Regards, Sean
精彩评论