开发者

Is there a tool in Visual Studio or plug-in to automate creating buddy class validation?

开发者 https://www.devze.com 2023-01-31 00:39 出处:网络
I am walked by Scott Hanselman\'s book through how to create NerdDinner MVC application. To validate the entity type Dinner that is generated by Entity Data Model Wizard, he extended the entity Dinne

I am walked by Scott Hanselman's book through how to create NerdDinner MVC application.

To validate the entity type Dinner that is generated by Entity Data Model Wizard, he extended the entity Dinner first by using partial class trick and then made a buddy class to be associated with Dinner.

See the following code for the details.

using System.ComponentModel.DataAnnotations;

namespace NerdDinner.Models
{
    [MetadataType(typeof(DinnerValidation))]
    public partial class Dinner { }

    public class DinnerValidation
    {
        [Required(ErrorMessage = "Title is required")]
        [StringLength(50, ErrorMessage = "Title may not be longer than 50 characters")]
        public string Title { get; set; }

        [Required(ErrorMessage = "Description is required")]
        [StringLength(256, ErrorMessage = "Description may not be longer than 256 characters")]
        public string Description { get; set; }

        [Required(ErrorMessage = "Addresss is required")]
        public string Address { get; set; }

        [Required(ErrorMessage = "Country is required")]
        public string Co开发者_如何学Gountry { get; set; }

        [Required(ErrorMessage = "Contact phone is required")]
        public string ContactPhone { get; set; }
    }
}

My question is

Is there a "convenient" way to create and associate the buddy class to Dinner? I means we can use a tool in Visual Studio or any plug-in to create the buddy class and automatically associate the buddy with Dinner. Of course I should make some adjustment to the generated code to suit my need, but it is not a big problem.

EDIT 1: I will add some additional info. Entity Data Model wizard is helpful because it create schema validation based on the database schema. For example, DinnerId property will be set to Int32 by default to match the type of column DinnerID which is of int.


it's not possible out of the box with the Entity Data Model Wizard. But what you can do is extend the Generator (.tt file that generates entities) and let it add [MetadataType(typeof(<#classname#>Validation))]. This way all the generated entities are already prepared for buddy classing...then you don't have to set up a separate partial class to make the association. Next, you can make the buddy classes by hand or eventually generate them alongside the "Microsoft" generated entities is separate files or one file with all the buddy classes together. We use a the same approach as you described with Self Tracking Entities. And at first it;s little bit of work. But it's really worth it. Because you get rock solid classes which you can validate at any given point: client-side, service layer, bussiness acces layer, etc. Also you have to set up all the validation criteria fromout the database. We have looked at ways to generate the this from the .edmx but found it more ättractive to specify them by hand for 30+ classes or so. A couple of hours work but afterwards it's showtime! ;) Probably less maintainable then generated buddy classes en auto-generated validation criteria, but everywhere is a trade-off (spending days / weeks to extend the standard template). Good luck!


Yes. It looks like you're creating a Domain Service. If that's the case, then when you create the Domain Service there is an option to generated associated classes for metadata (bottom of the window). The file will be called yourDomainServiceName.metadata.cs.

That file won't have any annotations on the fields, so you'll have to do that yourself.

0

精彩评论

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