像这样使用 StructureMap 可以吗? ASP.NET MVC 3

发布于 2024-12-07 19:57:09 字数 1951 浏览 2 评论 0原文

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

一切工作正常,但只是脑子里一片混乱。

我的代码看起来像这样。

global.asax

  IContainer container = new Container(
            x => { 

                    x.For<IUserRepo>().Use<UserRepo>();
                    x.For<IPostRepo>().Use<PostRepo>(); // this is the soultion for the error
            });
        DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));

PostController

private readonly IPostRepo _postRepo;

    public PostController(IPostRepo postRepo)
    {
        this._postRepo = postRepo;
    }

StructureMapDependencyResolver

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>();
        }
    }

这里是 IPostRepo 看起来像

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);
    }

谢谢马丁的帮助

i have a doubt that i am not using the best practice for using Structure-Map.

all working fine but just a confusion in mind.

my code look like this.

global.asax

  IContainer container = new Container(
            x => { 

                    x.For<IUserRepo>().Use<UserRepo>();
                    x.For<IPostRepo>().Use<PostRepo>(); // this is the soultion for the error
            });
        DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));

PostController

private readonly IPostRepo _postRepo;

    public PostController(IPostRepo postRepo)
    {
        this._postRepo = postRepo;
    }

StructureMapDependencyResolver

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>();
        }
    }

here is the IPostRepo looks like

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);
    }

thx martin for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒路情人 2024-12-14 19:57:09

不。就像我在您的其他问题中所说的那样,取出控制器激活器......除非您将其用于特定目的(您似乎并非如此)。

另外,这一行是完全错误的:

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

您不应该为您的 UserRepo 使用 new ...这就是上面一行所关心的:

x.For<IUserRepo>().Use<UserRepo>();

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

No. Like I said in your other question, take out the Controller Activator ... unless you are using it for a purpose (which it doesn't seem like you are).

Also, this line is plain WRONG:

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

You should not be using new for your UserRepo ... that is what the line above is taking care of:

x.For<IUserRepo>().Use<UserRepo>();

If you take out the ControllerActivator, you should have a nice start to an MVC app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文