私は、ASP.Net MVC3 の Web API プロジェクトで T4 テンプレートに取り組んでいます。ビジネス層からクラスとメソッドを取得し、.tt ファイルを使用して cs ファイルを作成する必要があります。また、[PrivateApi] タグのみを持つメソッドを取得する必要があります。
これは私の t4 テンプレート クラスです。
<# Assembly ab = AppDomain.CurrentDomain.GetAssemblies() .Where(b=>b.GetName().Name.Trim().ToLower() == "Empite.Give360.Business".ToLower()) .FirstOrDefault() as Assembly; foreach (var type in ab.GetTypes()) { if (type.Name.EndsWith("Service") && type.IsInterface ) { CreateAPI(type); SaveOutput(type.Name + "API.cs"); } } DeleteOldOutputs(); #>
<#+ public void CreateAPI(Type businessObjType ) {#>
public class <#= businessObjType.Name.Substring(1) #>API : <#= businessObjType.Name #>API { } public interface <#= businessObjType.Name #>API { } <#+ } #>
これは生成された CS ファイルです
public class DonationServiceAPI : IDonationServiceAPI
{
}
public interface IDonationServiceAPI
{
}
これは私が再現する必要があるクラスです
public class DonationService : IDonationService
{
private readonly IDonationRepository _donationRepository;
private readonly IDonationStatusTypeRepository _donationStatusTypeRepository;
private readonly IPayrollDeductionRepository _payrollDeductionRepository;
public DonationService() : this(new DonationRepository(),new DonationStatusTypeRepository(),new PayrollDeductionRepository())
{
}
public DonationService(IDonationRepository donationRepository,IDonationStatusTypeRepository donationStatusTypeRepository,IPayrollDeductionRepository payrollDeductionRepository)
{
_donationRepository = donationRepository;
_donationStatusTypeRepository = donationStatusTypeRepository;
_payrollDeductionRepository = payrollDeductionRepository;
}
[PrivateApi]
public ServiceResponse<Donation> GetDonationByDonationId(int donationId)
{
var donationObj = _donationRepository.Get(donationId);
return new ServiceResponse<Donation>(donationObj);
}
}
私は T4 テンプレートの初心者で、その方法を知っている人はいますか?