is there a way I can read in a namespace and loop through all the classes in a t4 template using reflection or something?
<#foreach (class poco in LoadNamespace("Web.Code.Entities.Poco").Classes ) { #>
public interface I<# poco.ClassName #>Repository
{
IQueryable< <# poco.ClassName #> > Get();
<# poco.ClassName #> Save(<# poco.ClassName #> entity);
bool Delete(<# poco.ClassName #> entity);
}
<#} #>
example meta data:
namespace Web.Code.Entities.Poco
{
public class Product
{
public int Id { get; set; }
public string Name{ get; set; }
public string Category{ get; set; }
}
public class Employee
{
public int Id { get; set; }
public string Name{ get; set; }
}
}
desired output:
public interface IProductRepository
{
IQueryable<Product> Get();
Product Save(Product entity);
bool Delete(Product entity);
}
public interface IEmployeeRepository
{
IQueryable<Employee> Get();
Employee Save(Employee entity);
bool Delete(Employee entity);
}
First install T4Toolbox from here
Then In your Template ensure you have the following lines at top :
Then add these lines of codes:
Then you can find all classes in a namespace by calling like this (the second parameter filters all classes which their names contain the passed string) :
OR
—————————————— UPDATE FOR VS 2012 ————————–
Use these functions and namespaces if your T4Toolbox is updated for VS 2012 :
so your search will be something like this :