Code Monkey home page Code Monkey logo

dto-writer's Introduction

DTO Writer

Extension for Visual Studio 2017 and 2019

Simple code generation tool for DTO-classes creation. You can easily create DTO-classes for your models in three clicks:

  1. Make right mouse click in Solution Explorer on your .cs file that contains model classes.
  2. Click "Create DTO" in context menu (You can find the same menu item in context menu of code editor area).
  3. Click "OK" in creator dialog window.

This is it! Your DTO(s) will be generated, saved into new file and this file will be added to your project.

During DTO creation you can select which model properties should be skipped, do you need mapping methods or not, and change the location where the output file should be created. Optionally you can choose to add the following attributes:

  • JsonProperty attribute from Newtonsoft.Json nuget package and / or
  • DataContract + DataMember attributes from System.Runtime.Serialization assembly.

These attributes can be added later. You can find code fix suggestions for adding/removing attributes (they will be offered when cursor is placed at DTO class name or one of it's property).

It's already available at Visual Studio Marketplace

You can find change log here

Example

Source file content:

using System;
using System.Collections.Generic;

namespace SampleApp.Models
{
  public class Person
  {
	public string FullName { get; set; }

	public int Age { get; set; }

	public DateTime Birthdate { get; set; }

	public string[] Skills { get; set; }

	public List<Person> Friends { get; set; }
  }
}

Output file content:

using System;
using System.Collections.Generic;
using System.Linq;

namespace SampleApp.Models.Dto
{
    public class PersonDto
    {
        public string FullName { get; set; }

        public int Age { get; set; }

        public DateTime Birthdate { get; set; }

        public string[] Skills { get; set; }

        public List<PersonDto> Friends { get; set; }

        public static PersonDto FromModel(Person model)
        {
            return new PersonDto()
            {
                FullName = model.FullName, 
                Age = model.Age, 
                Birthdate = model.Birthdate, 
                Skills = model.Skills.ToArray(), 
                Friends = model.Friends.Select(PersonDto.FromModel).ToList(), 
            }; 
        }

        public Person ToModel()
        {
            return new Person()
            {
                FullName = FullName, 
                Age = Age, 
                Birthdate = Birthdate, 
                Skills = Skills.ToArray(), 
                Friends = Friends.Select(dto => dto.ToModel()).ToList(), 
            }; 
        }
    }
}

dto-writer's People

Contributors

ilchenkob avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.