开发者

How to remove dependency on System.Web.Mvc within domain model assembly containing POCOs

I have an assembly generated using POCO template using Entity Framework (e.g. "Company.Models.dll").Besides generated POCOs I also have "follow up" partial classes that define meta data using System.ComponentModel.DataAnnotations. So for example Company.Models.Customer is auto-generated (in separate folder) and then I have partial class with same namespace. Within this partial class I define inner class for metadata...For example:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Company.Models
{
    [MetadataType(typeof (CustomerMetaData))]
    public partial class Customer
    {
        public override string ToString()
        {
            return String.Format("Id: {0}, Name: {1}", Id, Name);
        }

        //[Bind(Exclude = "Id")]
        public class CustomerMetaData
        {
            [ScaffoldColumn(false)]
            public int Id { get; set; }

            [DisplayName("Name")]
            [Required(ErrorMessage = "Customer Name is required.")]
            [StringLength(100, ErrorMessage = "Customer name cannot exceed 100 characters.", MinimumLength = 2)]
            public string Name { get; set; }
        }
    }
}

The issue I am having is that I now want to use this a开发者_Python百科ssembly in my MVC project and want to use MVC specific attributes (like the commented out Bind attribute above).However, doing so requires making my Company.Models.dll dependent on System.Web.Mvc.dll.

I would like to avoid this at all costs if possible but how?

So far I am aware of 2 possible solutions and am asking community for their opinions or better approaches...

Solution 1

This solution has been discussed here: Using Data Annotations on POCO's with MVC for Remote Validation The "trick" was to use ViewModels and map (either mannually or using AutoMapper) POCOs to MVC project specific ViewModels. Then apply all necessary attributes to ViewModels instead of domain model POCOs. While this makes sense for large projects it is a bit overkill for small simple solutions...

Solution 2

Use Bind attribute in Controller action parameters (as seen on some http://www.asp.net/mvc tutorials). For example:

//
// POST: /Customer/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude="Id")]Customer customerToCreate)
{
        if (!ModelState.IsValid)
        return View();

        // TODO: Add insert logic here
    return RedirectToAction("Index");
    }

Using this solution would allow to skip dependency on System.Web.Mvc from my POCO dll at the cost of having to remember to insert Bind attribute on all relevant Controller actions. Also, if I have multiple ASP.NET MVC projects the issue gets worse...

So, is there another way? ;)


I'd suggest that your POCO entities do not contain any UI-specific code. Ideally they wouldn't contain any code specific to data persistence either but EF makes this tricky.

The first solution is definitely best solution to use in order to make your application more scalable and to better separate out your concerns.

Another solution could be to create your own domain attributes (MyRequiredAttribute, MyStringLengthAttribute etc.) and place these in your core assembly. You could then create a custom metadata provider to have MVC recognise these attributes. Here's an example of how to create this: http://bradwilson.typepad.com/blog/2009/10/enterprise-library-validation-example-for-aspnet-mvc-2.html


A third option, which is similar to solution 2, is binding to the customer type.

You would create a Model Binder.

Watch Jimmy Bogards MVC Conf presentation on the subject: http://www.viddler.com/explore/mvcconf/videos/1/

Sample Code:

Global ASAX

    protected void Application_Start()
    {

        ModelBinders.Binders.Add(typeof(Media), new MediaModelBinder());
        ModelBinders.Binders.Add(typeof(Album), new AlbumModelBinder());

    }

ModelBinder Class

public class MediaModelBinder : BaseModelBinder, IModelBinder 
{
    /// <summary>
    /// Initializes a new instance of the <see cref="MediaModelBinder"/> class.
    /// </summary>
    public MediaModelBinder() : base(DependencyInjection.Resolve<IUserAuthorization>()) { }

    /// <summary>
    /// Binds the model to a value by using the specified controller context and binding context.
    /// </summary>
    /// <param name="controllerContext">The controller context.</param>
    /// <param name="bindingContext">The binding context.</param>
    /// <returns>The bound value.</returns>
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var mediaRepository = DependencyInjection.Resolve<IMediaRepository>();
        User user = GetUser(controllerContext);

        int mediaId = Convert.ToInt32(controllerContext.RouteData.Values["id"]);
        Media media = mediaRepository.RetrieveByPrimaryKeyAndUserId(mediaId, user.Id);

        return media;
    }
}


Easiest solution: put your Partial Class Customer in your MVC project. No other changes needed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜