Cello How-To Guide Entity POCO Generator Plugin

Cello How-To Guide
Entity POCO Generator Plugin
Entity POCO Generator Plugin
Contents
1
2
Introduction to Entity Framework POCO Helper ......................................................................................... 3
1.1
Pre-Requisites to use Cello’s POCO Generator .................................................................................... 4
1.2
Installation ........................................................................................................................................... 5
1.3
How to use POCO Generator Helper ................................................................................................... 6
1.4
Advantages of Cello POCO Generator ............................................................................................... 12
Contact Information................................................................................................................................... 14
2
Entity POCO Generator Plugin
1 Introduction to Entity Framework POCO Helper
Entity Framework is an Object Relational Mapper (ORM). It basically generates business objects and entities
according to the database tables and provides the mechanism for:



Performing basic CRUD (Create, Read, Update, and Delete) operations.
Easily managing "1 to 1", "1 to many", and "many to many" relationships.
Ability to have inheritance relationships between entities.
Entity Framework brings in several advantages, some of them are
1. Provides dedicated functionality for CRUD operation (Create, Read, Update, and Delete). Easy to
implement CRUD operations.
2. If you want to replace the data store, it is very easy to replace without modifying the data access logic
since all data access logic are present in higher level.
3. Easy to manage one to one, one to many and many to many relationship between tables.
4. Conceptual model can be represented in a better way.
5. Developer can reduce the code in classes and sub-classes for data access
Although EF simplifies the Data Access part of the web application, but it also brings in lot of challenges
when building Multi-Tenant applications. While developing applications using Cello Framework, it
recommends you to alter the POCO to include Tenant ID, Inheriting from Base Class provided, Additional
Attributes to leverage the features comes from Cello. But while Generating POCO from EF, it will only create
plain model without the other aspects mentioned above, So Cello provides EF POCO Generator utility to
simplify the process of Creating Models along with the recommended add-ons.
Plain POCO created by EF without POCO Utility
namespace
{
using
using
using
using
Sample.TodoModule.Model
System;
System.Collections.Generic;
System.Runtime.Serialization;
CelloSaaS.Model;
[Serializable]
[DataContract]
public partial class Todo
{
[DataMember]
public System.Guid Id { get; set; }
[DataMember]
public override string TenantId { get; set; }
[DataMember]
public string TaskName { get; set; }
[DataMember]
public string TaskDescription { get; set; }
[DataMember]
public System.DateTime EndDate { get; set; }
[DataMember]
public System.DateTime CreatedOn { get; set; }
[DataMember]
public string CreatedBy { get; set; }
[DataMember]
public Nullable<System.DateTime> UpdatedOn { get; set; }
[DataMember]
public string UpdatedBy { get; set; }
[DataMember]
public bool Status { get; set; }
3
Entity POCO Generator Plugin
Plain POCO created by EF without POCO Utility
//-----------------------------------------------------------------------------// <auto-generated>
//
This code was generated from a template.
//
//
Manual changes to this file may cause unexpected behavior in your application.
//
Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//-----------------------------------------------------------------------------namespace
{
using
using
using
using
Sample.TodoModule.Model
System;
System.Collections.Generic;
System.Runtime.Serialization;
CelloSaaS.Model;
[Serializable]
[DataContract]
[EntityIdentifier(Name="Todo")]
[EntityDescriptor(SchemaTableName = "Todoes", PrimaryKeyName="Id", DisplayColumnName = "Name",
ExtnSchemaTableName = "TodoesExtn", TenantIdColumnName = "TenantId",
SchemaTableConnectionStringName = "ApplicationConnectionString",
ExtnSchemaTableConnectionStringName = "ApplicationConnectionString", IsExtensible = true,
Privileges = "View_Todo,Add_Todo,Delete_Todo,Edit_Todo")]
public partial class Todo : TenantTransactionalEntity
{
[DataMember]
public System.Guid Id { get; set; }
[DataMember]
public override string TenantId { get; set; }
[DataMember]
public string TaskName { get; set; }
[DataMember]
public string TaskDescription { get; set; }
[DataMember]
public System.DateTime EndDate { get; set; }
[DataMember]
public System.DateTime CreatedOn { get; set; }
[DataMember]
public string CreatedBy { get; set; }
[DataMember]
public Nullable<System.DateTime> UpdatedOn { get; set; }
[DataMember]
public string UpdatedBy { get; set; }
[DataMember]
public bool Status { get; set; }
}
}
1.1 Pre-Requisites to use Cello’s POCO Generator
In order to install the plugin, the development environment must have the following pre-requisites, they are


VS2012
Entity Framework 6.0
4
Entity POCO Generator Plugin
1.2 Installation
The POCO Generator is Visual Studio Plugin that can be found in the installer package. Follow the below
procedure to install the CelloSaaS.POCOGenerator.vsix in the developer environment.
1. Double click on CelloSaaS.POCOGenerator.vsix and install
5
Entity POCO Generator Plugin
1.3 How to use POCO Generator Helper
Follow the below steps to use the CelloSaaS EF POCO Generator template:

Add Reference to CelloSaaS.Model.dll
Add Service Reference

Add your edmx file (e.g.: HelloWorldModel.edmx)
Add EDMX
6
Entity POCO Generator Plugin

Delete Model.tt and Model.Context.tt files
Select the Database
7
Entity POCO Generator Plugin
Set the Connection
8
Entity POCO Generator Plugin
Select the Entitities
9
Entity POCO Generator Plugin
EDMX View

Remove <<ModelName>>.Context.tt & <<ModelName>>.tt File which was automatically generated
Remove <<ModelName>>.Context.tt & <<ModelName>>.tt

Open edmx file and right click on the empty surface and choose “Add Code Generation Item”.

Add Code Generation
Choose EF 6.x CelloSaaS POCO Generator
10
Entity POCO Generator Plugin
Select EF POCO Generator

Click on Add

CelloSaaS POCO Generator generates your Model and DbContext class with below modifications
related to CelloSaaS
11
Entity POCO Generator Plugin
1.4 Advantages of Cello POCO Generator


The entity is automatically inherited from TenantTransactionalEntity and applied necessary attributes,
refer here
Your DbContext class is inherited from CelloDbContext which takes care of data partitioning. That is
routing the queries to the database according to the logged in tenant configuration. It also applies
tenant filtering for your select queries.
public partial class HelloWorldModelContainer : CelloDbContext
{
/// Creates Db Context with the given connection string name and metadata
/// <param name="connectionStringName">connection string name e.g.:
ApplicationConnectionString</param>
/// <param name="metadata">metadata url</param>
public HelloWorldModelContainer(string connectionStringName, string metadata = "res://*/")
: base(connectionStringName, metadata)
{
this.Configuration.LazyLoadingEnabled = false;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{}
public virtual DbSet<HelloWorld> HelloWorlds { get; set; }
public virtual IQueryable<HelloWorld> HelloWorldsQuery { get { return
this.ApplySecurity<HelloWorld>(this.HelloWorlds); } }}
Above two classes are auto generated by CelloSaaS POCO Generator templates.
In your DAL Implementation class:
private const string _metadata =
"res://*/Model.HelloWorldModel.csdl|res://*/Model.HelloWorldModel.ssdl|res://*/Model.HelloWorldModel.msl";
private const string ConnectionStringName = "HelloWorldConnectionString";
public string Create(Model.HelloWorld entity)
{
using (var ctx = new HelloWorldModelContainer(ConnectionStringName, _metadata))
{
ctx.HelloWorlds.Add(entity);
ctx.SaveChanges();
entity.Identifier = entity.Id.ToString();
return entity.Identifier;
}}
public Dictionary<string, Model.HelloWorld> Search(HelloWorldSearchCondition condition){
using (var ctx = new HelloWorldModelContainer(ConnectionStringName, _metadata))
{
var query = ctx.HelloWorldsQuery; // This will apply auto tenant filter
if (!string.IsNullOrEmpty(condition.Message))
{
query = query.Where(x => x.Message.Contains(condition.Message));
}
var entities = query.ToDictionary(x => x.Id.ToString(), y => y);
Notes

}}
foreach (var e in entities)
{
e.Value.Identifier = e.Key;
to
remember:}
return
entities;
Your Entity
must
contain Primary Key named “Id”.
12
Entity POCO Generator Plugin



Your Entity must contain one column named “TenantId”.
Use DbContext.EntityNames for Create, Update and Delete operations
Use DbContext.EntityNamesQuery for select operations.
13
Entity POCO Generator Plugin
2 Contact Information
Any problem using this guide (or) using Cello Framework. Please feel free to contact us, we will be happy to
assist you in getting started with Cello.
Email: support@techcello.com
Phone: +1(609)503-7163
Skype: techcello
14