C# 创建并注册命名模式
我正在开发一个基于C#的应用程序,其中我有一个带有内部后端的实用程序接口,该接口创建一些对象并在应用程序内部注册它们。 因此,代码如下所示:C# 创建并注册命名模式,c#,.net,design-patterns,C#,.net,Design Patterns,我正在开发一个基于C#的应用程序,其中我有一个带有内部后端的实用程序接口,该接口创建一些对象并在应用程序内部注册它们。 因此,代码如下所示: public interface Utility { public SomeObject CreateSomeObject(String id); } 只有实用程序接口在我的程序集外部可见,具体的后端类是内部的。 我真的很想为创建方法找到一个合适的名称,它应该告诉用户想要的对象将被创建和注册。 有什么建议吗 编辑我已经考虑过给我的方法命名Cre
public interface Utility {
public SomeObject CreateSomeObject(String id);
}
只有实用程序
接口在我的程序集外部可见,具体的后端类是内部的。
我真的很想为创建方法找到一个合适的名称,它应该告诉用户想要的对象将被创建和注册。
有什么建议吗
编辑我已经考虑过给我的方法命名CreateAndRegisterSomeObject(),但是有些对象的名称非常长,这使得有些方法的名称非常长
提前谢谢,你应该这样做 使用factory,您将获得如下内容:
public Class Factory
{
public static object CreateAndRegister(string className)
{
Assembly assem = Assembly.GetExecutingAssembly();
Object obj = assem.CreateInstance(className, false);
//Code for registration
return obj;
}
}
编码
SomeObj obj = (SomeObj)Factory.CreateAndRegister("SomeObj");
你应该这样做 使用factory,您将获得如下内容:
public Class Factory
{
public static object CreateAndRegister(string className)
{
Assembly assem = Assembly.GetExecutingAssembly();
Object obj = assem.CreateInstance(className, false);
//Code for registration
return obj;
}
}
编码
SomeObj obj = (SomeObj)Factory.CreateAndRegister("SomeObj");
我真的不喜欢你的方法在创建和注册两方面都做了那么多的工作,但是如果必须的话,我很可能会使用泛型来实现它
public interface IUtility {
T CreateAndRegister<T>(String id) where T : IIdentifiable, new();
T CreateAndRegister<T>(ComplexInitInfo somethingElse) where T : ISomethingElseable, new();
}
然后,您可以按照以下方式实现实用程序类:
public class Utility : IUtility
{
public T CreateAndRegister<T>(string id) where T : IIdentifiable, new()
{
T result = new T();
result.Id = id;
// do your registration
return result;
}
public T CreateAndRegister<T>(ComplexInitInfo somethingElse) where T : ISomethingElseable, new()
{
T result = new T();
result.Id = somethingElse.SomeId;
result.SomethingElse = somethingElse.SomethingElse;
// do your registration
return result;
}
}
公共类实用程序:IUtility
{
public T CreateAndRegister(字符串id),其中T:IIdentifiable,new()
{
T结果=新的T();
结果:Id=Id;
//你登记了吗
返回结果;
}
public T CreateAndRegister(ComplexInitInfo somethingElse),其中T:isomethingelable,new()
{
T结果=新的T();
result.Id=somethingElse.SomeId;
result.SomethingElse=SomethingElse.SomethingElse;
//你登记了吗
返回结果;
}
}
另一种方法
我还想指出,您尝试使用的CreateAndRegister
模式是一个与框架稍有不同的问题(例如,除其他外)
它们允许您注册给定类型并将其与对象的生存期相关联。例如,如果您想将类型注册为单例,IoC框架可以为您完成这项工作,那么当您解析时,您将得到一个您想要的类型实例,该实例与已配置的生命周期有关。例如,您可能希望每次解析类型时都有一个新实例,或者每个线程只需要一个新实例,或者在解析时始终返回相同的实例
无论如何,祝你好运 我真的不喜欢你的方法在创建和注册两方面都做了那么多的工作,但是如果必须这样做的话,我很可能会使用泛型
public interface IUtility {
T CreateAndRegister<T>(String id) where T : IIdentifiable, new();
T CreateAndRegister<T>(ComplexInitInfo somethingElse) where T : ISomethingElseable, new();
}
然后,您可以按照以下方式实现实用程序类:
public class Utility : IUtility
{
public T CreateAndRegister<T>(string id) where T : IIdentifiable, new()
{
T result = new T();
result.Id = id;
// do your registration
return result;
}
public T CreateAndRegister<T>(ComplexInitInfo somethingElse) where T : ISomethingElseable, new()
{
T result = new T();
result.Id = somethingElse.SomeId;
result.SomethingElse = somethingElse.SomethingElse;
// do your registration
return result;
}
}
公共类实用程序:IUtility
{
public T CreateAndRegister(字符串id),其中T:IIdentifiable,new()
{
T结果=新的T();
结果:Id=Id;
//你登记了吗
返回结果;
}
public T CreateAndRegister(ComplexInitInfo somethingElse),其中T:isomethingelable,new()
{
T结果=新的T();
result.Id=somethingElse.SomeId;
result.SomethingElse=SomethingElse.SomethingElse;
//你登记了吗
返回结果;
}
}
另一种方法
我还想指出,您尝试使用的CreateAndRegister
模式是一个与框架稍有不同的问题(例如,除其他外)
它们允许您注册给定类型并将其与对象的生存期相关联。例如,如果您想将类型注册为单例,IoC框架可以为您完成这项工作,那么当您解析时,您将得到一个您想要的类型实例,该实例与已配置的生命周期有关。例如,您可能希望每次解析类型时都有一个新实例,或者每个线程只需要一个新实例,或者在解析时始终返回相同的实例
无论如何,祝你好运 首先,
CreateAndRegister
(如评论中所建议的)几乎不是一个“长”名字。就在本周,我可能编写了更长的命名方法。如果这是对您的方法的最佳描述,请使用它
但是如果您想要一个较短的名称,您可以考虑从类名中获得一些帮助。“效用”是一个噪音词,像“经理”或“助手”;这绝对没有意义。因此,i实用性
毫无意义;对于从类型名称中获得的所有值,您也可以将其称为I
public interface IRegistrar {
Foo GetFoo(string id);
Bar GetBar(string id);
}
var myFoo = _registrar.GetFoo(id);
但是如果类型被命名,比如说,IRegistrar
,那么在开始编写方法名称之前,您就突然获得了有意义的上下文。注册器是注册东西的东西,如果它有返回值的方法,那么你知道它也会给你一些东西。现在,您可以将该方法命名为简短的名称——也许只是GetXxx
。它正在创建一些东西这一事实是显而易见的(它必须从某个地方获取),并且它正在注册它这一事实在类型名中是隐式的
public interface IRegistrar {
Foo GetFoo(string id);
Bar GetBar(string id);
}
var myFoo = _registrar.GetFoo(id);
首先,
CreateAndRegister
(如评论中所建议的)几乎不是一个“长”名称。就在本周,我可能编写了更长的命名方法。如果这是对您的方法的最佳描述,请使用它
但是如果您想要一个较短的名称,您可以考虑从类名中获得一些帮助。“效用”是一个噪音词,像“经理”或“助手”;这绝对没有意义。因此,i实用性
毫无意义;对于从类型名称中获得的所有值,您也可以将其称为I
public interface IRegistrar {
Foo GetFoo(string id);
Bar GetBar(string id);
}
var myFoo = _registrar.GetFoo(id);
但是如果类型被命名,比如说,IRegistrar
,那么在开始编写方法名称之前,您就突然获得了有意义的上下文。注册器是注册事物的东西,如果它有方法w