Created
August 6, 2018 04:29
-
-
Save danielplawgo/1f76fcbb27f3184f060c2eeb411321f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BaseModel | |
{ | |
public BaseModel() | |
{ | |
IsActive = true; | |
} | |
public int Id { get; set; } | |
public bool IsActive { get; set; } | |
public DateTime CreatedDate { get; set; } | |
public string CreatedUser { get; set; } | |
public DateTime UpdatedDate { get; set; } | |
public string UpdatedUser { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IRepository<T> where T : BaseModel, new() | |
{ | |
void Add(T entity); | |
void Delete(T entity); | |
void Delete(int id); | |
T GetById(int id); | |
IQueryable<T> GetAllActive(); | |
IQueryable<T> GetAll(); | |
void SaveChanges(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ include file="IRepositoryTemplate.tt" #> | |
<#+ | |
// <copyright file="IRepositoryGenerator.tt" company=""> | |
// Copyright © . All Rights Reserved. | |
// </copyright> | |
public class IRepositoryGenerator : Generator | |
{ | |
public IRepositoryTemplate Template = new IRepositoryTemplate(); | |
protected override void RunCore() | |
{ | |
var classes = new string[]{"User", "Product"}; | |
foreach(var className in classes){ | |
Template.ClassName = className; | |
Template.RenderToFile("I" + className + "Repository.generated.cs"); | |
} | |
} | |
} | |
#> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template language="C#" debug="True" #> | |
<#@ output extension="cs" #> | |
<#@ include file="T4Toolbox.tt" #> | |
<#@ include file="IRepositoryGenerator.tt" #> | |
<# | |
// <copyright file="IRepositoryScript.tt" company=""> | |
// Copyright © . All Rights Reserved. | |
// </copyright> | |
IRepositoryGenerator generator = new IRepositoryGenerator(); | |
generator.Run(); | |
#> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#+ | |
// <copyright file="IRepositoryTemplate.tt" company=""> | |
// Copyright © . All Rights Reserved. | |
// </copyright> | |
public class IRepositoryTemplate : CSharpTemplate | |
{ | |
public string ClassName; | |
public override string TransformText() | |
{ | |
base.TransformText(); | |
#> | |
using T4Repositories.Models; | |
namespace <#= DefaultNamespace #> | |
{ | |
public partial interface I<#= Identifier(ClassName) #>Repository : IRepository<<#= Identifier(ClassName) #>> | |
{ | |
} | |
} | |
<#+ | |
return this.GenerationEnvironment.ToString(); | |
} | |
} | |
#> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <autogenerated> | |
// This file was generated by T4 code generator IRepositoryScript.tt. | |
// Any changes made to this file manually will be lost next time the file is regenerated. | |
// </autogenerated> | |
using T4Repositories.Models; | |
namespace T4Repositories.Logic.Repositories | |
{ | |
public partial interface IUserRepository : IRepository<User> | |
{ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Repository<T> : IRepository<T> where T : BaseModel, new() | |
{ | |
public Repository(Lazy<DataContext> dataContext) | |
{ | |
if (dataContext == null) | |
{ | |
throw new ArgumentNullException("dataContext"); | |
} | |
_dataContext = dataContext; | |
} | |
private Lazy<DataContext> _dataContext; | |
protected DataContext DataContext | |
{ | |
get | |
{ | |
return _dataContext.Value; | |
} | |
} | |
public void Add(T entity) | |
{ | |
DataContext.Set<T>().Add(entity); | |
} | |
public void Delete(T entity) | |
{ | |
DataContext.Set<T>().Remove(entity); | |
} | |
public void Delete(int id) | |
{ | |
T entity = new T() | |
{ | |
Id = id | |
}; | |
DataContext.Entry(entity).State = EntityState.Deleted; | |
} | |
public T GetById(int id) | |
{ | |
return DataContext.Set<T>().FirstOrDefault(e => e.Id == id); | |
} | |
public IQueryable<T> GetAllActive() | |
{ | |
return DataContext.Set<T>().Where(e => e.IsActive); | |
} | |
public IQueryable<T> GetAll() | |
{ | |
return DataContext.Set<T>(); | |
} | |
public void SaveChanges() | |
{ | |
DataContext.SaveChanges(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ include file="RepositoryTemplate.tt" #> | |
<#@ assembly name="System" #> | |
<#+ | |
// <copyright file="RepositoryGenerator.tt" company=""> | |
// Copyright © . All Rights Reserved. | |
// </copyright> | |
public class RepositoryGenerator : Generator | |
{ | |
public RepositoryTemplate Template = new RepositoryTemplate(); | |
protected override void RunCore() | |
{ | |
var classes = new string[]{"User", "Product"}; | |
foreach(var item in classes){ | |
Template.ClassName = item; | |
Template.RenderToFile(item + "Repository.generated.cs"); | |
} | |
} | |
} | |
#> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template language="C#" debug="True" #> | |
<#@ output extension="cs" #> | |
<#@ include file="T4Toolbox.tt" #> | |
<#@ include file="RepositoryGenerator.tt" #> | |
<# | |
// <copyright file="RepositoryScript.tt" company=""> | |
// Copyright © . All Rights Reserved. | |
// </copyright> | |
RepositoryGenerator generator = new RepositoryGenerator(); | |
generator.Run(); | |
#> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#+ | |
// <copyright file="RepositoryTemplate.tt" company=""> | |
// Copyright © . All Rights Reserved. | |
// </copyright> | |
public class RepositoryTemplate : CSharpTemplate | |
{ | |
public string ClassName; | |
public override string TransformText() | |
{ | |
base.TransformText(); | |
#> | |
using T4Repositories.Models; | |
using T4Repositories.Logic.Repositories; | |
using System; | |
namespace <#= DefaultNamespace #> | |
{ | |
public partial class <#= Identifier(ClassName) #>Repository : Repository<<#= Identifier(ClassName) #>>, I<#= Identifier(ClassName) #>Repository | |
{ | |
public <#= Identifier(ClassName) #>Repository(Lazy<DataContext> db) | |
: base(db) | |
{ | |
} | |
} | |
} | |
<#+ | |
return this.GenerationEnvironment.ToString(); | |
} | |
} | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment