[英]Create a Generic Service Factory
我正在尝试创建类似2层架构的东西,其中工厂总是需要在服务层中创建服务层实例。 我希望该服务工厂是通用的,并返回一个接口。 我做了这样的事情:
// service base interface
namespace SimpleFW.Services
{
public interface IServiceBase
{
void Get();
void Create();
void Update();
void Delete();
}
}
下面的代码被创建以添加每个服务的自定义功能:
// interface for the custom implementation
namespace SimpleFW.Services
{
public interface ISimpleService : IServiceBase
{
void CustomMethod();
}
}
这是服务的实现方式
// implementation of the service
namespace SimpleFW.Services
{
public class SimpleService : ISimpleService
{
public void CustomMethod()
{
Debug.Write("CustomMethod Method Called.");
}
public void Get()
{
Debug.Write("Get Method Called.");
}
public void Create()
{
Debug.Write("Create Method Called.");
}
public void Update()
{
Debug.Write("Update Method Called.");
}
public void Delete()
{
Debug.Write("Delete Method Called.");
}
}
}
这是一个通用工厂,它将创建服务的实例:
// service factory
namespace SimpleFW.Services
{
public class ServiceFactory
{
public static IServiceBase GetService<T>() where T : IServiceBase, new()
{
return new T();
}
}
}
这是单独程序集中的服务客户端
// service client in separate assembly
namespace SimpleFW.Client
{
class Program
{
static void Main(string[] args)
{
IServiceBase service = ServiceFactory.GetService<SimpleService>();
service.Get(); // should be callable from within the same assembly not from client assembly
service.Create(); // should be callable from within the same assembly not from client assembly
service.Update(); // should be callable from within the same assembly not from client assembly
service.Delete(); // should be callable from within the same assembly not from client assembly
((ISimpleService)service).CustomMethod(); // this is fine. I should be able to cast and call the methods like this
SimpleService serObject = new SimpleService(); // how to make this impossible
}
}
}
尽管我在任何有顾虑的地方都在代码旁边写了注释,但仍然存在以下问题:
一些解决方案(也许):这是我要做的事情:
SimpleService将始终显式实现接口,如下所示:
// implementation of the service
namespace SimpleFW.Services
{
public class SimpleService : ISimpleService
{
public void ISimpleService.CustomMethod()
{
Debug.Write("CustomMethod Method Called.");
}
public void IServiceBase.Get()
{
Debug.Write("Get Method Called.");
}
public void IServiceBase.Create()
{
Debug.Write("Create Method Called.");
}
public void IServiceBase.Update()
{
Debug.Write("Update Method Called.");
}
public void IServiceBase.Delete()
{
Debug.Write("Delete Method Called.");
}
}
}
通过进行上述更改,将不会阻止服务层的使用者创建SimpleService的实例,但是它们将无法直接调用任何已实现的方法。 他们将只能访问该服务的自定义公共方法。
如何从外部程序集中隐藏IServiceBase接口的实现?
使用内部访问修饰符 。
如何仅将SimpleService类的构造限制为Factory,以便始终需要外部程序集从通用服务类型的工厂调用GetService方法?
内部访问修饰符将解决您的SimpleService问题。
对于您的工厂,我建议使用Enum创建您的服务。
public enum ServiceType
{
SimpleService,
.
.
}
// For naming convention use CreateService
IServiceBase service = ServiceFactory.GetService(ServiceType.SimpleService);
public class ServiceFactory
{
public static T GetService(enum type)
{
// create you service here
}
}
assembly SimpleFW.Services.Core -> IServiceBase interface
assembly SimpleFW.Services.Implementation -> SimpleService class
请注意,在汇编中实际上必须组成整个应用程序,您必须在服务实现中引用汇编。 因此,该服务的实现将在该程序集中可见。 但是其他程序集只能使用Services.Core
程序集并使用编程来进行接口 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.