Use interface between model and view in ASP.NET MVC

Posted by Icerman on Stack Overflow See other posts from Stack Overflow or by Icerman
Published on 2010-05-20T05:24:10Z Indexed on 2010/05/20 5:30 UTC
Read the original article Hit count: 253

Filed under:
|

Hi,

I am using asp.net MVC 2 to develop a site. IUser is used to be the interface between model and view for better separation of concern. However, things turn to a little messy here. In the controller that handles user sign on: I have the following:

        IUserBll userBll = new UserBll();
        IUser newUser = new User();

        newUser.Username = answers[0].ToString();
        newUser.Email = answers[1].ToString();

        userBll.AddUser(newUser);

The User class is defined in web project as a concrete class implementing IUser. There is a similar class in DAL implementing the same interface and used to persist data. However, when the userBll.AddUser is called, the newUser of type User can't be casted to the DAL User class even though both Users class implementing the interface (InvalidCastException).

Using conversion operators maybe an option, but it will make the dependency between DAL and web which is against the initial goal of using interface.

Any suggestions?

© Stack Overflow or respective owner

Related posts about interface

Related posts about mvc