[英]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.