Entity Framework Database First를 통해 내 엔티티를 생성하면 다음과 같은 함수를 사용하려고합니다.
AuditManager.DefaultConfiguration.Exclude<T>();
내가 전화하기를 원하는 횟수가 엔티티의 수와 같아야한다고 생각해.
전의:
AuditManager.DefaultConfiguration.Exclude<Employee>();
AuditManager.DefaultConfiguration.Exclude<Department>();
AuditManager.DefaultConfiguration.Exclude<Room>();
이제 선택된 수의 엔티티를 반복하고 모든 항목을 Exclude
기능에 전달하는 방법은 무엇입니까?
분명한 해결책은 숨기려는 모든 엔티티 유형에 대해 메소드를 호출하는 것입니다. 이렇게 :
AuditManager.DefaultConfiguration.Exclude<Employee>();
AuditManager.DefaultConfiguration.Exclude<Department>();
AuditManager.DefaultConfiguration.Exclude<Room>();
(당신은 조건문을 추가 할 if
주변들)이 동적으로 그것을 할 수 있습니다.
Howevery, 메타 데이터를 기반으로 Exclude
메서드를 호출하는 유연한 솔루션을 원한다면 다른 것이 필요합니다. 이 같은:
var types = new[] { typeof(Employee), typeof(Department), typeof(Room) };
var instance = AuditManager.DefaultConfiguration;
var openGenericMethod = instance.GetType().GetMethod("Exclude");
foreach (var @type in types)
{
var closedGenericMethod = openGenericMethod.MakeGenericMethod(@type);
closedGenericMethod.Invoke(instance, null);
}
이것은 Exclude<T>
메서드가 DefaultConfiguration
가리키는 모든 인스턴스의 인스턴스 메서드라고 가정합니다.
엔티티 유형을 반복하는 대신 감사하지 않으려는 엔티티가 동일한 인터페이스를 구현하고 제외하도록 할 수 있습니다. 예 :
public interface IExcludeFromAudit
{ }
귀하의 사업체 :
public class Order : IExcludeFromAudit
{
//snip
}
이제 인터페이스를 제외하십시오.
AuditManager.DefaultConfiguration.Exclude<IExcludeFromAudit>();
이것의 이점은 어떤 것이 제외되는지를 쉽게 제어 할 수 있다는 것입니다.