我是Ninject的新手,在我实现通用存储库模式时发现自己陷入困境.我想将依赖关系IRepository <IEntityType>绑定到类ConcreteRepository <EntityType>,其中ConcreteRepository <T>实现IRepository <T>,EntityType实现IEntityType.我试过这个:
kernel.Bind<IRepository<IEntityType>>().To<ConcreteRepository<EntityType>>();
Run Code Online (Sandbox Code Playgroud)
...但是Ninject不会接受它,因为它不知道或不关心EntityType实现IEntityType.我怎样才能绑定这种依赖?
UPDATE
这是我得到的错误:
错误3类型'ICM.Dependency.Repository.ConcreteRepository'不能在泛型类型或方法'Ninject.Syntax.IBindingToSyntax.To()'中用作类型参数'TImplementation'.没有从"ConcreteRepository <EntityType>"到"IRepository <IEntityType>"的隐式引用转换.
解
我仍然不太明白为什么我的绑定不起作用,但显然我在那里错误地使用泛型.因此,该解决方案与NInject无关.我结束了指定ConcreteRepository以显式连接IEntityType和TEntityType:
public class ConcreteRepository<TInterface, TEntity> : IRepository<TInterface> where TEntity : TInterface { ... }
Run Code Online (Sandbox Code Playgroud)
然后注射可写如下:
kernel.Bind<IRepository<IEntityType>>().To<ConcreteRepository<IEntityType,EntityType>>()
Run Code Online (Sandbox Code Playgroud) 这是方法重载的一个非常基本的示例,两个方法具有相同的名称但具有不同的签名:
int MyMethod(int a)
int MyMethod(int a, string b)
Run Code Online (Sandbox Code Playgroud)
现在假设我定义了两个通用接口,共享完全相同的名称但具有不同数量的类型参数,例如:
IMyInterface<T>
IMyInterface<T1,T2>
Run Code Online (Sandbox Code Playgroud)
我可以说这代表"通用接口重载"吗?或者"重载"术语是否仅适用于此类背景下的方法?它仍然看起来与方法重载非常相似,因为我们保持一个完全相同的名称,但改变参数.
如果我不能说"通用接口过载/重载"我可以说这两个不同的接口共享相同的名称?
感谢和抱歉,如果这是一个愚蠢的问题,但谷歌搜索"通用接口重载"或"通用接口重载"并没有给我很多,但有关接口方法重载的结果,这不是我感兴趣的.
以下警告显示在我的项目中 -
未选中调用'getWeatherData(T,Boolean,String)'作为原始类型'IWeatherCallbackListener'的成员.
我创建了以下界面 -
public interface IWeatherCallbackListener<T> {
void getWeatherData(T weatherModel, Boolean success, String errorMsg);
}
Run Code Online (Sandbox Code Playgroud)
并按以下方式调用它,
public class WeatherConditions {
private static IWeatherApi mWeatherApi;
/**
* @param city
* @param appId
* @param listener
*/
public static void getOpenWeatherData(String city, String appId, IWeatherCallbackListener listener) {
mWeatherApi = ApiService.getRetrofitInstance(BASE_URL_OPEN_WEATHER).create(IWeatherApi.class);
Call<OpenWeatherModel> resForgotPasswordCall = mWeatherApi.getOpenWeatherData(appId, city);
resForgotPasswordCall.enqueue(new Callback<OpenWeatherModel>() {
@Override
public void onResponse(Call<OpenWeatherModel> call, Response<OpenWeatherModel> response) {
if (response.body() != null) {
if (listener != null)
listener.getWeatherData(response.body(), true, "");
}
} …Run Code Online (Sandbox Code Playgroud) 我已经定义了一个结构如下,
(struct vector (x y z)
#:methods gen:custom-write
[(define (write-proc vector port mode)
(let ([print (if mode write display)])
(write-string "<")
(print (vector-x vector))
(write-string ", ")
(print (vector-y vector))
(write-string ", ")
(print (vector-z vector))
(write-string ">")))])
Run Code Online (Sandbox Code Playgroud)
但是我在REPL中得到了一个奇怪的行为,其中结构显示3次:
> (define a (vector 1 2 3))
> a
<1, 2, 3><1, 2, 3><1, 2, 3>
Run Code Online (Sandbox Code Playgroud)
我一定是做错了,但找不到我的问题.有人可以告诉我为什么我有3次输出?
这是我的代码
public interface ITranslator<E, R>
{
E ToEntity<T>(R record);
}
class Gens : ITranslator<string, int>
{
#region ITranslator<string,int> Members
public string ToEntity<MyOtherClass>(int record)
{
return record.ToString();
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
当我编译它时,我收到一个错误 Type parameter declaration must be an identifier not a type
为什么我不能ToEntity<MyOtherClass>但只能拥有ToEntity<T>?
编辑:正在MyOtherClass做什么?我在多个表/类之间转换实体(相当于实体框架的POCO)和记录(框架返回的对象).所以我想用它来进行我的类特定转换
我有一些通用接口相互链接.
public interface IA
{
int val { get; set; }
}
public interface IB<T> where T:IA
{
T a_val { get; set; }
}
public interface IC<T> where T : IB<IA>
{
T b_val { get; set; }
}
public class a:IA
{
public int val { get; set; }
}
public class b:IB<a>
{
public a a_val { get; set; }
}
public class c:IC<b>
{
public b b_val { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
对于最后一节课c,我有一个错误:
类型'b'不能用作通用类型或方法'IC'中的类型参数'T'.没有从'b'到'IB'的隐式引用转换.
在这种情况下,如何正确使用通用接口?
我是一个快速的初学者。学习的时候有件事让我很困惑。现在我想定义一个抽象类或定义一些纯虚方法,但我找不到方法来做到这一点。我有一个带有关联类型的协议(这也让我困惑,为什么不使用泛型协议),并且一些方法需要在基类中实现,而从基类继承的其他类,它们应该实现协议中的其他方法,我能怎么做?例如:
Protocol P{
typealias TypeParam
func A()
func B()
}
class BaseClass<TypeParam> : P {
abstract func A()
func B(){
if someCondition {
A()
}
}
}
class ChildClass : BaseClass<Int> {
func A(){}
}
Run Code Online (Sandbox Code Playgroud)
看起来很奇怪,我仍然找不到解决抽象问题的方法。
我在浏览时找到了通用DAO接口的代码:
public interface GenericDAO<T, ID extends Serializable> {
Class<T> getEntityClass();
T findById(final ID id);
List<T> findAll();
List<T> findByExample(final T exampleInstance);
List<T> findByNamedQuery(
final String queryName,
Object... params
);
List<T> findByNamedQueryAndNamedParams(
final String queryName,
final Map<String, ?extends Object> params
);
int countAll();
int countByExample(final T exampleInstance);
T save(final T entity);
boolean delete(final T entity);
}
Run Code Online (Sandbox Code Playgroud)
是否有任何理由特别留下具有默认访问修饰符的方法(类/包:是,子类/世界:否)?
PS:一个补充问题.ID通常是在不依赖于RDBMS(XML,平面文件......)的实现中找到的吗?
我对Fortran不熟悉.这是一个包含一些子程序的通用接口.gfortran 4.8抱怨说:
(1)中通用接口'sorti'中的模糊接口'sortic4'和'sortic'
INTERFACE SORTI
SUBROUTINE SORTIC( N, IND, TBLC )
INTEGER , INTENT(IN ) :: N
INTEGER , INTENT(INOUT) :: IND( N )
CHARACTER*(*), INTENT(IN ) :: TBLC( * )
END SUBROUTINE SORTIC
SUBROUTINE SORTIC4( N, IND, TBLC )
INTEGER , INTENT(IN ) :: N
INTEGER , INTENT(INOUT) :: IND( N )
CHARACTER*(*), INTENT(IN ) :: TBLC( * )
END SUBROUTINE SORTIC4
SUBROUTINE SORTIC8( N, IND, TBLC )
INTEGER(8) , INTENT(IN ) :: N
INTEGER(8) , INTENT(INOUT) :: IND( …Run Code Online (Sandbox Code Playgroud)