开发者

Why won't this generics code compile?

开发者 https://www.devze.com 2022-12-08 15:43 出处:网络
//The class is defined like so.... public class CreateNewAccountHandler : ICommandHandler<CreateNewAccountCommand, CreateNewAccountResponse>
//The class is defined like so....
public class CreateNewAccountHandler : ICommandHandler<CreateNewAccountCommand, CreateNewAccountResponse>
{
        public CreateNewAccountResponse ExecuteCommand(CreateNewAccountCommand command)
        {
                throw new NotImplementedException();
        }
}

//And here it the code which won't compile
static void RegisterHandlers_Account(IUnityContainer unityContainer)
{
        unityContainer.RegisterType
                <
                        ICommandHandler
                                <
                                        TaskSmart.AppLayer.Api.Commands.Account.CreateNewAccountCommand,
                                        TaskSmart.AppLayer.Api.Commands.Account.CreateNewAccountResponse
                                >,
                        TaskSmart.AppLayer.RequestHandlers.Account.CreateNewAccountHandler
                >(new TransientLifetimeManager());
}

Error 1 The type 'TaskSmart.AppLayer.RequestHandlers.Account.CreateNewAccountHandler' cannot be used as type parameter 'TTo' in the generic type or method 'Microsoft.Practices.Unity.IUnityContainer.RegisterType(Microsoft.Practices.Unity.LifetimeManager, params Microsoft.Practices.Unity.InjectionMember[])'. There is no implicit reference conversion from 'TaskSmart.AppLayer.RequestHandlers.Account.Creat开发者_开发知识库eNewAccountHandler' to 'TaskSmart.AppLayer.Api.RequestHandlers.ICommandHandler'. C:\Data\TaskSmart\TaskSmart.AppLayer\UnityBootStrapper.cs 50 6 TaskSmart.AppLayer

I have checked it many times, but I just cannot see why this refuses to compile! I've even fully qualified the class/interface names to ensure it is not a namespace problem and I get the same error.

Any ideas?

PS: SVN is here: https://tasksmart.svn.sourceforge.net/svnroot/tasksmart/trunk


You've got two ICommandHandler (in TaskSmart.AppLayer.Api.RequestHandlers and in TaskSmart.AppLayer.RequestHandlers) in your code base, and the first part of your generic registration is not using a fully qualified name

Correct code is

static void RegisterHandlers_Account(IUnityContainer unityContainer)
        {
            unityContainer.RegisterType
                <
                    TaskSmart.AppLayer.RequestHandlers.ICommandHandler
                        <
                            TaskSmart.AppLayer.Api.Commands.Account.CreateNewAccountCommand,
                            TaskSmart.AppLayer.Api.Commands.Account.CreateNewAccountResponse
                        >,
                    TaskSmart.AppLayer.RequestHandlers.Account.CreateNewAccountHandler
                >(new TransientLifetimeManager());
        }


Try to fully qualify the ICommandHandler interface:

unityContainer.RegisterType<TaskSmart.AppLayer.RequestHandlers.ICommandHandler
    <CreateNewAccountCommand, CreateNewAccountResponse>, 
    CreateNewAccountHandler>();

Must be the exact same interface implemented by CreateNewAccountHandler:

public class CreateNewAccountHandler : 
    TaskSmart.AppLayer.RequestHandlers.ICommandHandler<CreateNewAccountCommand, CreateNewAccountResponse>
{ }

And here's the patch file to apply to your SVN repo:

From f5541188298b40515728c1ad51f645408876999c Mon Sep 17 00:00:00 2001
From: unknown <did_bfg@.(none)>
Date: Sun, 18 Oct 2009 12:14:26 +0200
Subject: [PATCH] fixed namespace

---
 TaskSmart.AppLayer/UnityBootStrapper.cs |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/TaskSmart.AppLayer/UnityBootStrapper.cs b/TaskSmart.AppLayer/UnityBootStrapper.cs
index c3ed0fe..d9748a9 100644
--- a/TaskSmart.AppLayer/UnityBootStrapper.cs
+++ b/TaskSmart.AppLayer/UnityBootStrapper.cs
@@ -41,7 +41,7 @@ namespace TaskSmart.AppLayer
                {
                        unityContainer.RegisterType
                                <
-                                       ICommandHandler
+                    TaskSmart.AppLayer.RequestHandlers.ICommandHandler
                                                <
                                                        TaskSmart.AppLayer.Api.Commands.Account.CreateNewAccountCommand,
                                                        TaskSmart.AppLayer.Api.Commands.Account.CreateNewAccountResponse
--
1.6.4.msysgit.0
0

精彩评论

暂无评论...
验证码 换一张
取 消