Asp.net mvc 像这样使用StructureMap可以吗?asp.NETMVC3

Asp.net mvc 像这样使用StructureMap可以吗?asp.NETMVC3,asp.net-mvc,asp.net-mvc-3,structuremap,Asp.net Mvc,Asp.net Mvc 3,Structuremap,我怀疑我没有使用最佳实践来使用结构图 一切都很好,只是脑子里有点混乱 我的代码是这样的 global.asax StructureMappDependencyResolver 公共类结构MapDependencyResolver:IDependencyResolver { 专用只读IContainer\u容器; 公共结构MapDependencyResolver(IContainer容器) { 这个._容器=容器; } 公共对象GetService(类型serviceType) { 对象实例=_

我怀疑我没有使用最佳实践来使用结构图

一切都很好,只是脑子里有点混乱

我的代码是这样的

global.asax StructureMappDependencyResolver
公共类结构MapDependencyResolver:IDependencyResolver
{
专用只读IContainer\u容器;
公共结构MapDependencyResolver(IContainer容器)
{
这个._容器=容器;
}
公共对象GetService(类型serviceType)
{
对象实例=_container.TryGetInstance(serviceType);
if(instance==null&!serviceType.IsAbstract)
{
_Configure(c=>c.AddType(serviceType,serviceType));
instance=\u container.TryGetInstance(serviceType);
}
返回实例;
}
公共IEnumerable GetServices(类型serviceType)
{
return_container.GetAllInstances(serviceType.Cast();
}
}
这是IPostRepo的外观
公共接口IPostRepo
{
boolcreatepost(Post-newPost);
列出ShowAllPosts();
Post FindPostById(int postId);
Post EditPost(Post EditPost);
UserPostCommentViewModel FindAllPostCommentCommentCommentComments(int postId);
int?AddPlusOneToNumberOfViews(int postId);
}

谢谢你的帮助

不。就像我在你的另一个问题中说的,拿出控制器激活器。。。除非你是为了某个目的而使用它(看起来你不是)

此外,这一行显然是错误的:

x.ForRequestedType<AccountController>().TheDefault.Is.
              ConstructedBy(() => new AccountController(new UserRepo()));

如果你拿出控制器激活器,你应该有一个好的开始,MVC应用程序。

不。就像我在你的另一个问题中说的,拿出控制器激活器。。。除非你是为了某个目的而使用它(看起来你不是)

此外,这一行显然是错误的:

x.ForRequestedType<AccountController>().TheDefault.Is.
              ConstructedBy(() => new AccountController(new UserRepo()));

如果您取出ControllerActivator,MVC应用程序应该有一个良好的开端。

ok。。一切都很好…当我试着去后控制。。它给了我这样的错误。。没有为PluginFamily MBP_Blog.Interfaces.ipostrepoops定义默认实例!!我不告诉结构图当你得到IPostRepo时要用什么…一切都像奶油一样工作…thx martin…好的。。一切都很好…当我试着去后控制。。它给了我这样的错误。。没有为PluginFamily MBP_Blog.Interfaces.ipostrepoops定义默认实例!!我不能告诉结构图当你得到IPostRepo时要用什么…一切都像奶油一样工作。。。
public class StructureMapDependencyResolver : IDependencyResolver
    {
        private readonly IContainer _container;
        public StructureMapDependencyResolver(IContainer container )
        {
            this._container = container;
        }

        public object GetService(Type serviceType)
        {
            object instance = _container.TryGetInstance(serviceType);
            if(instance == null && !serviceType.IsAbstract)
            {
                _container.Configure(c => c.AddType(serviceType,serviceType));
                instance = _container.TryGetInstance(serviceType);
            }
            return instance;

        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return _container.GetAllInstances(serviceType).Cast<object>();
        }
    }
public interface IPostRepo
    {
        bool CreatePost(Post newPost);

        List<Post> ShowAllPosts();

        Post FindPostById(int postId);

        Post EditPost(Post editPost);

        UserPostCommentViewModel FindAllPostComments(int postId);

        int? AddPlusOneToNumberOfViews(int postId);
    }
x.ForRequestedType<AccountController>().TheDefault.Is.
              ConstructedBy(() => new AccountController(new UserRepo()));
x.For<IUserRepo>().Use<UserRepo>();