Generating View Models with CatFactory.DotNetCore

0
76

Introduction

CatFactory provides a mechanism for modeling CLR structure, in that way we can use all objects definitions from CatFactory as we want. This guide applies for .NET Core and I’ll show you how to generate view model classes using CatFactory.DotNetCore package.

CatFactory is an open source framework, of course, I have the ownership for this package but you can create your own code builder using the model provided by CatFactory.

Background

There are a large number of tools to generate code for .NET platform, but with this package we want to provide an useful guide to know about CatFactory, at this moment this is a preview release for CatFactory.DotNetCore, this package is the base for all CatFactory packages related to .NET world.

With this package we can define classes and interfaces, there are some extension methods to add some code in an easy way, for example AddViewModelProperty, that method adds a field and a property with get and set block.

Using the Code

Step 01 – Create Console Project

Create a new console project with Visual Studio.

Step 02 – Add Package for Console Project

Visual Studio 2015

Add the following package in project.json file:

Name Version Description
CatFactory.DotNetCore 1.0.0-alpha-build36 Provides object model and code generation for .NET Core

Save all changes and build the project.

Visual Studio 2017

Add package with Nuget Package Manager for your console project:

Nuget Package Manager in Visual Studio 2017


Noow add thisthe following code in Program.cs file:

using System;
using CatFactory.DotNetCore;
using CatFactory.OOP;

namespace ConsoleApp1
{
 public class Program
 {
 public static void Main(string[] args)
 {
 var classDefinition = new CSharpClassDefinition();

 classDefinition.Namespace = "ViewModels";
 classDefinition.Name = "ProductViewModel";

 classDefinition.Namespaces.Add("System");
 classDefinition.Namespaces.Add("System.ComponentModel");

 classDefinition.Implements.Add("INotifyPropertyChanged");

 classDefinition.Events.Add(
 new EventDefinition("PropertyChangedEventHandler", "PropertyChanged")
 );

 classDefinition.AddViewModelProperty("Int32?", "ProductId");
 classDefinition.AddViewModelProperty("String", "ProductName");
 classDefinition.AddViewModelProperty("Int32?", "ProductCategoryID");
 classDefinition.AddViewModelProperty("Decimal?", "UnitPrice");
 classDefinition.AddViewModelProperty("String", "ProductDescription");

 var classBuilder = new CSharpClassBuilder
 {
 ObjectDefinition = classDefinition,
 OutputDirectory = "C:\\Temp"
 };

 classBuilder.CreateFile();
 }
 }
}

Now we can build and run the project to generate the output file.

How It Works?

  1. Set the value for Namespace property, that’s class’ namespace.
  2. Set the name of the class in Name property.
  3. Add all namespaces for class.
  4. This class has a dependency from INotifyPropertyChanged, it’s an interface that’s the reason why we added on Implements property
  5. For this case, we want to notify all changes in properties, so we need to add the event PropertyChanged in class definition.
  6. We add all properties for class definition, as we know the properties aren’t automatic because we need to raise the event PropertyChanged so we need to specify the code for set block, the method AddViewModelProperty is an extension method, this method adds a field for class definition and a property with get and set block also an invocation for notify event.
  7. As a last step, we create an instance of CSharpClassBuilder class and set the object definition and output directory in order to generate the file with CreateFile() method invocation.

Now we proceed to check the output file:

using System;
using System.ComponentModel;

namespace ViewModels
{
 public class ProductViewModel : INotifyPropertyChanged
 {
 public event PropertyChangedEventHandler PropertyChanged;

 private Int32? m_productId;
 private String m_productName;
 private Int32? m_productCategoryID;
 private Decimal? m_unitPrice;
 private String m_productDescription;

 public Int32? ProductId
 {
 get
 {
 return m_productId;
 }
 set
 {
 if (m_productId != value)
 {
 m_productId = value;
 
 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ProductId)));
 }
 }
 }

 public String ProductName
 {
 get
 {
 return m_productName;
 }
 set
 {
 if (m_productName != value)
 {
 m_productName = value;
 
 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ProductName)));
 }
 }
 }

 public Int32? ProductCategoryID
 {
 get
 {
 return m_productCategoryID;
 }
 set
 {
 if (m_productCategoryID != value)
 {
 m_productCategoryID = value;
 
 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ProductCategoryID)));
 }
 }
 }

 public Decimal? UnitPrice
 {
 get
 {
 return m_unitPrice;
 }
 set
 {
 if (m_unitPrice != value)
 {
 m_unitPrice = value;
 
 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(UnitPrice)));
 }
 }
 }

 public String ProductDescription
 {
 get
 {
 return m_productDescription;
 }
 set
 {
 if (m_productDescription != value)
 {
 m_productDescription = value;
 
 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ProductDescription)));
 }
 }
 }
 }
}

Points of Interest

  • Inside of set block, there is an invocation like PropertyChanged?, that’s because in .NET Core we can invokea method on instance in safe mode, the equivalence for that in previous versions of C# is if (PropertyChanged != null) { … }, I think the extension method can have a flag parameter to indicates if we want to simply the event invocation, also there is a using of nameof operator, that operator gets the name from existing member, the benefit is there isn’t magic string and if we change the name of member (property, method, etc) the compiler show compilation error.
  • CatFactory.DotNetCore provides a model for .NET Core.
  • There is a code builder for C#, code builder requires an object definition for .NET Core to know object members: events, fields, constructors, properties and methods.
  • CatFactory.DotNetCore is independent from other frameworks such as EF Core, Web API, MVC, etc. but with this package, we can build specific packages to generate code in C#, take a look at the links section to know more about this.

Related Links

Code Improvements

  • In order to get a better experience with this framework, you can have an object definition list to generate in a massive way

History

  • 8th January, 2017: Initial version

LEAVE A REPLY