在我公司,我们的服务实现基本上只是将调用传递回业务层,业务层进行实际处理。因此,例如,如果我们有一个如下所示的服务合同:
public interface IService
{
void ServiceMethod1(string a, object b);
int ServiceMethod2(int a, int b);
}
我们的服务可能看起来很像这样:
public class Service : IService
{
private ServiceBL _serviceBL;
public Service()
{
_serviceBL = new ServiceBL();
}
public void ServiceMethod1(string a, object b)
{
_serviceBL.ServiceMethod1(a, b);
}
public int ServiceMethod2(int a, int b)
{
return _serviceBL.ServiceMethod2(a, b);
}
}
因为这变得相当重复,我想知道是否有一些我可以制作的基类会根据合同自己发出服务方法。我希望代码看起来像这样:
public abstract class MagicServiceBase<T>
{
protected dynamic InterfaceImplementor { get; }
public MagicServiceBase()
{
// Magic that makes the methods defined in T real.
}
}
public class Service : MagicServiceBase<IService>, IService
{
protected override dynamic InterfaceImplementor
{
get
{
return new ServiceBL();
}
}
}
有没有办法创建这个,或者我只是想懒得合理?