How to remove the dependency on System.Web.Mvc in the assembly of a domain model containing POCOs

I have an assembly generated using the POCO template using the Entity Framework (for example, "Company.Models.dll"). In addition to the generated POCOs, I also have โ€œsubsequentโ€ partial classes that define metadata using System.ComponentModel.DataAnnotations. So, for example, Company.Models.Customer is auto-generated (in a separate folder), and then I have a partial class with the same namespace. Inside this partial class, I define an 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; }
        }
    }
}

, , , MVC MVC (, Bind). Company.Models.dll System.Web.Mvc.dll.

, , ?

...

1

: POCO MVC "" , ViewModels ( , AutoMapper ) POCOs MVC. ViewModels POCOs. , ...

2

Bind (. http://www.asp.net/mvc ). :

//
// 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");
    }

System.Web.Mvc dll POCO Bind . , ASP.NET MVC, ...

, ?;)

+3
3

, POCO , . , , EF .

, , , .

(MyRequiredAttribute, MyStringLengthAttribute ..) . , MVC . , : http://bradwilson.typepad.com/blog/2009/10/enterprise-library-validation-example-for-aspnet-mvc-2.html

+2

, 2, .

.

Jimmy Bogards MVC Conf : http://www.viddler.com/explore/mvcconf/videos/1/

:

ASAX

    protected void Application_Start()
    {

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

    }

ModelBinder

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;
    }
}
0

The easiest solution: put yours Partial Class Customerin your MVC project. No other changes are required.

0
source

Source: https://habr.com/ru/post/1777738/


All Articles