Simple template, generating basic business rules

This template is more sophisticated, contains a trick to rule of business mention (changing "info" by "BR"), to write in CamelCase entity name, identify all properties that make up the key, and so on.
<#@ template debug="True" language="C#"#>
<#@ assembly name="SFS.Model" #>
<#@ import namespace="SFS.Model" #> 
<#@ dom processor="SFSEdmxProcessor" requires="EntityModel='MyModel.edmx'"  #>
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version: 2.0.50727.1434 
//     Generate:        7/2/2008 4:24:56 PM
//     Author:          Emir
//     Comany:            
//     Email:            
//     Web site:          
//      			
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using AdventureWorksModel;

namespace MyWindowsApplication 
{
<#	foreach(EntityInfo entity in this.Model.Entities) {#>
	#region <#= entity.Name #>
	/// <summary>
	/// <#= entity.Name #> Business rule 
	/// </summary>
	public partial class <#=entity.Name.Replace("Info", "BR")#>{
		AdventureWorksModel.AdventureWorksEntities context;
	    public  <#= entity.Name.Replace("Info", "BR") #>(){
		    context = new AdventureWorksModel.AdventureWorksEntities();
        }
        public List<<#= entity.Name #>> GetAll(){
            var query = from c in context.<#= entity.Name.Replace("Info", "Set") #>
                        select c;
            return query.ToList<<#= entity.Name #>>();
        }
<#
       
		System.Text.StringBuilder sb = new System.Text.StringBuilder();
		foreach(PropertyKeyInfo key in entity.EntityKey){
			sb.Append(" " + key.Property.NetType + " " + 
				key.Property.Name.Replace(
					key.Property.Name.Substring(0,1), 
					key.Property.Name.Substring(0, 1).ToLower()
					)
				);
		}
        
#>
		public <#= entity.Name #> GetByKey(<#= sb.ToString() #>){
			<#	string ccEntityName = entity.Name.Replace(
					entity.Name.Substring(0,1), 
					entity.Name.Substring(0, 1).ToLower()
					); #>
			<#= entity.Name #> <#= ccEntityName #> = null;
			
			var query = from c in context.<#= entity.Name.Replace("Info", "Set") #>
						select c;
			<#= ccEntityName #> = query.FirstOrDefault<<#= entity.Name #>>();
            return <#= ccEntityName #>;
						
		}
        #region Properties
        //Only for demostration
<#	foreach(PropertyInfo property in entity.Properties) {#>
		//public <#= property.NetType #> <#=property.Name#> { get; set; }	
<#	}#>
	    #endregion
	}
	#endregion
<# } #> 
	
}


And this te result:
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version: 2.0.50727.1434 
//     Generate:        7/2/2008 4:24:56 PM
//     Author:          Emir
//     Comany:            
//     Email:            
//     Web site:          
//      			
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using AdventureWorksModel;

namespace MyWindowsApplication 
{
	#region AddressInfo
	/// <summary>
    /// AddressInfo Business rule 
    /// </summary>
    public partial class AddressBR{
		AdventureWorksModel.AdventureWorksEntities context;
	    public  AddressBR(){
		    context = new AdventureWorksModel.AdventureWorksEntities();
        }
        public List<AddressInfo> GetAll(){
            var query = from c in context.AddressSet
                        select c;
            return query.ToList<AddressInfo>();
        }
        public AddressInfo GetByKey( Int32 addressID){
						AddressInfo addressInfo = null;
			
			var query = from c in context.AddressSet
						select c;
			addressInfo = query.FirstOrDefault<AddressInfo>();
            return addressInfo;
						
        }
        #region Properties
        //Only for demostration
		//public Int32 AddressID { get; set; }	
		//public String AddressLine1 { get; set; }	
		//public String AddressLine2 { get; set; }	
		//public String City { get; set; }	
		//public String PostalCode { get; set; }	
		//public Guid rowguid { get; set; }	
		//public DateTime ModifiedDate { get; set; }	
	    #endregion
	}
	#endregion
	#region AddressTypeInfo
	/// <summary>
    /// AddressTypeInfo Business rule 
    /// </summary>
    public partial class AddressTypeBR{
		AdventureWorksModel.AdventureWorksEntities context;
	    public  AddressTypeBR(){
		    context = new AdventureWorksModel.AdventureWorksEntities();
        }
        public List<AddressTypeInfo> GetAll(){
            var query = from c in context.AddressTypeSet
                        select c;
            return query.ToList<AddressTypeInfo>();
        }
        public AddressTypeInfo GetByKey( Int32 addressTypeID){
						AddressTypeInfo addressTypeInfo = null;
			
			var query = from c in context.AddressTypeSet
						select c;
			addressTypeInfo = query.FirstOrDefault<AddressTypeInfo>();
            return addressTypeInfo;
						
        }
        #region Properties
        //Only for demostration
		//public Int32 AddressTypeID { get; set; }	
		//public String Name { get; set; }	
		//public Guid rowguid { get; set; }	
		//public DateTime ModifiedDate { get; set; }	
	    #endregion
	}
	#endregion
	#region ContactInfo
	/// <summary>
    /// ContactInfo Business rule 
    /// </summary>
    public partial class ContactBR{
		AdventureWorksModel.AdventureWorksEntities context;
	    public  ContactBR(){
		    context = new AdventureWorksModel.AdventureWorksEntities();
        }
        public List<ContactInfo> GetAll(){
            var query = from c in context.ContactSet
                        select c;
            return query.ToList<ContactInfo>();
        }
        public ContactInfo GetByKey( Int32 contactID){
						ContactInfo contactInfo = null;
			
			var query = from c in context.ContactSet
						select c;
			contactInfo = query.FirstOrDefault<ContactInfo>();
            return contactInfo;
						
        }
        #region Properties
        //Only for demostration
		//public Int32 ContactID { get; set; }	
		//public Boolean NameStyle { get; set; }	
		//public String Title { get; set; }	
		//public String FirstName { get; set; }	
		//public String MiddleName { get; set; }	
		//public String LastName { get; set; }	
		//public String Suffix { get; set; }	
		//public String EmailAddress { get; set; }	
		//public Int32 EmailPromotion { get; set; }	
		//public String Phone { get; set; }	
		//public String PasswordHash { get; set; }	
		//public String PasswordSalt { get; set; }	
		//public String AdditionalContactInfo { get; set; }	
		//public Guid rowguid { get; set; }	
		//public DateTime ModifiedDate { get; set; }	
	    #endregion
	}
	#endregion
	#region ContactTypeInfo
	/// <summary>
    /// ContactTypeInfo Business rule 
    /// </summary>
    public partial class ContactTypeBR{
		AdventureWorksModel.AdventureWorksEntities context;
	    public  ContactTypeBR(){
		    context = new AdventureWorksModel.AdventureWorksEntities();
        }
        public List<ContactTypeInfo> GetAll(){
            var query = from c in context.ContactTypeSet
                        select c;
            return query.ToList<ContactTypeInfo>();
        }
        public ContactTypeInfo GetByKey( Int32 contactTypeID){
						ContactTypeInfo contactTypeInfo = null;
			
			var query = from c in context.ContactTypeSet
						select c;
			contactTypeInfo = query.FirstOrDefault<ContactTypeInfo>();
            return contactTypeInfo;
						
        }
        #region Properties
        //Only for demostration
		//public Int32 ContactTypeID { get; set; }	
		//public String Name { get; set; }	
		//public DateTime ModifiedDate { get; set; }	
	    #endregion
	}
	#endregion
	#region CountryRegionInfo
	/// <summary>
    /// CountryRegionInfo Business rule 
    /// </summary>
    public partial class CountryRegionBR{
		AdventureWorksModel.AdventureWorksEntities context;
	    public  CountryRegionBR(){
		    context = new AdventureWorksModel.AdventureWorksEntities();
        }
        public List<CountryRegionInfo> GetAll(){
            var query = from c in context.CountryRegionSet
                        select c;
            return query.ToList<CountryRegionInfo>();
        }
        public CountryRegionInfo GetByKey( String countryRegioncode){
						CountryRegionInfo countryRegionInfo = null;
			
			var query = from c in context.CountryRegionSet
						select c;
			countryRegionInfo = query.FirstOrDefault<CountryRegionInfo>();
            return countryRegionInfo;
						
        }
        #region Properties
        //Only for demostration
		//public String CountryRegionCode { get; set; }	
		//public String Name { get; set; }	
		//public DateTime ModifiedDate { get; set; }	
	    #endregion
	}
	#endregion
	#region StateProvinceInfo
	/// <summary>
    /// StateProvinceInfo Business rule 
    /// </summary>
    public partial class StateProvinceBR{
		AdventureWorksModel.AdventureWorksEntities context;
	    public  StateProvinceBR(){
		    context = new AdventureWorksModel.AdventureWorksEntities();
        }
        public List<StateProvinceInfo> GetAll(){
            var query = from c in context.StateProvinceSet
                        select c;
            return query.ToList<StateProvinceInfo>();
        }
        public StateProvinceInfo GetByKey( Int32 stateProvinceID){
						StateProvinceInfo stateProvinceInfo = null;
			
			var query = from c in context.StateProvinceSet
						select c;
			stateProvinceInfo = query.FirstOrDefault<StateProvinceInfo>();
            return stateProvinceInfo;
						
        }
        #region Properties
        //Only for demostration
		//public Int32 StateProvinceID { get; set; }	
		//public String StateProvinceCode { get; set; }	
		//public Boolean IsOnlyStateProvinceFlag { get; set; }	
		//public String Name { get; set; }	
		//public Int32 TerritoryID { get; set; }	
		//public Guid rowguid { get; set; }	
		//public DateTime ModifiedDate { get; set; }	
	    #endregion
	}
	#endregion
 
	
}

See the the tutorial step by step

Last edited Jul 4, 2008 at 5:46 AM by emirtrevino, version 7

Comments

No comments yet.