Coder Social home page Coder Social logo

reinforced.typings's Introduction

Reinforced.Typings is available on NuGet.

PM> Install-Package Reinforced.Typings

Find out detailed information in Reinforced.Typings wiki

What is that?

Simply, this thing converts your .NET assemblies to TypeScript code. It integrates to VisualStudio build process and does following tricks:

Exporting ViewModels

C#TypeScript
namespace MyApp
{
    using Reinforced.Typings.Attributes;
[TsInterface]
public class Order
{
    public string ItemName { get; set; }
    public int Quantity { get; set; }
    public double Subtotal { get; set; }
    public bool IsPaid { get; set; }
    public string ClientName { get; set; }
    public string Address { get; set; }
}

[TsClass]
public class User
{
    public string FirstName { get; set; }
    public string Email { get; set; }
    public UserType Type { get; set; }
}

[TsEnum]
public enum UserType { One, Two }

}

module MyApp {
	export interface IOrder
	{
		ItemName: string;
		Quantity: number;
		Subtotal: number;
		IsPaid: boolean;
		ClientName: string;
		Address: string;
	}
	export class User
	{
		public FirstName: string;
		public Email: string;
		public Type: MyApp.UserType;
	}
	export enum UserType { 
		One = 0, 
		Two = 1, 
	}
}	
	

...even complex ViewModels

C#TypeScript
namespace MyApp
{
    using Reinforced.Typings.Attributes;
[TsInterface]
public class Page
{
    public List<Order> Orders { get; set; }

    public Dictionary<int, Order> 
                    Cache { get; set; }

    public string[] Tags { get; set; }

    public IEnumerable<object> 
                    Things { get; set; }
}

}

module MyApp {
	export interface IPage
	{
		Orders: MyApp.IOrder[];
		Cache: { [key:number]: MyApp.IOrder };
		Tags: string[];
		Things: any[];
	}
}	
	

Can disable itself. Or TS compilation. Or both.

It is needed for some situations. See RtBypassTypeScriptCompilation and RtDisable configuration parameters.

Preserves inheritance

C#TypeScript
namespace MyApp
{
    using Reinforced.Typings.Attributes;
public interface INonExport
{
    string Boom { get; }
}

[TsInterface]
public class WithoutInterface
            : INonExport
{
    public string Boom { get; set; }
}

[TsInterface]
public interface IEntity
{
    int Id { get; set; }
}

[TsInterface]
public class User : IEntity
{
    public int Id { get; set; }

    public string Login { get; set; }
}

}

module MyApp {
	export interface IWithoutInterface
	{
		Boom: string;
	}
	export interface IEntity
	{
		Id: number;
	}
	export interface IUser extends MyApp.IEntity
	{
		Id: number;
		Login: string;
	}
}	
	

Supports fluent configuration

Details can be found on the corresponding wiki page

C#TypeScript
namespace MyApp
{
    using Reinforced.Typings.Fluent;
    using System.Web.Mvc;
public class Configuration
{
    public static void 
        Configure(ConfigurationBuilder builder)
    {
        builder
        	.ExportAsInterface<SelectListItem&gt()
            .OverrideNamespace("MyApp")
            .WithPublicProperties();
    }
}

}

module MyApp {
	export interface ISelectListItem
	{
		Disabled: boolean;
		Group: any;
		Selected: boolean;
		Text: string;
		Value: string;
	}
}	
	
Reinforced.Typings.settings.xml: <RtConfigurationMethod>MyApp.Configuration.Configure</RtConfigurationMethod>

Generates any custom glue code

Please obtain full sample code here. It is long to be fully published in readme.

C#TypeScript
namespace MyApp
{
    using Reinforced.Typings.Fluent;
    using System.Web.Mvc;
[TsClass(CodeGeneratorType = typeof(AngularControllerGenerator)]
public class AngularController : Controller
{
    [AngularMethod(typeof(SampleResponseModel))]
    public ActionResult Save(Order order)
    {
        return Json(new {
            Message = "Success",
            Success = true
        });
    }
}

public class AngularMethodAttribute 
        : TsFunctionAttribute
{
    public AngularMethodAttribute(Type returnType)
    {
        StrongType = returnType;
        CodeGeneratorType = typeof 
         (AngularActionCallGenerator);
    }
}

public class AngularActionCallGenerator 
        : MethodCodeGenerator
{
    // too long - see sample
}

public class AngularControllerGenerator 
        : ClassCodeGenerator
{
    // too long - see sample
}

[TsInterface]
public class SampleResponseModel
{
    public string Message { get; set; }
    public bool Success { get; set; }    
}

}

module MyApp {
	export interface ISampleResponseModel
	{
		Message: string;
		Success: boolean;
	}
if (window['app']) {
    window['app'].factory('Api.AngularController', 
    ['$http', 
        ($http: angular.IHttpService) => new AngularController($http)]);
}

/** Result of AngularControllerGenerator activity */
export class AngularController
{
	constructor ($http: angular.IHttpService)
	{
		this.http = $http;
	}
	public Save(order: IOrder) : angular.IPromise&lt;ISampleResponseModel&gt;
	{
		var params = { 'order': order };
		return this.http.post('/Angular/Save', params)
		    .then((response) => { return response.data; });
	}
}        

}

reinforced.typings's People

Contributors

alexanderdemko avatar kfrancis avatar pavel-b-novikov avatar radiosterne avatar zivillian avatar

Stargazers

 avatar

Watchers

 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.