这是分离的代码和我在做对吗? [英] Is this code decoupled and am I doing it right?

查看:160
本文介绍了这是分离的代码和我在做对吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我努力学习并付诸实践的IoC以及如何对接口,而不是对象编程。这是相当难受。这里是我到目前为止的代码。是否有我犯任何错误?指出它们对我会帮助我了解如何它实际上当付诸实践相符。

I'm trying to learn and put into practice IoC and how to program against interfaces instead of objects. This is quite hard for me. Here is the code I have so far. Are there any mistakes I made? Point them out to me will help me understand how it actually fits in when put into practice.

谢谢!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpDIC.Api.Interfaces
{
    interface IDownloader
    {
        void DownloadInformation();
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpDIC.Api.Interfaces;

namespace SharpDIC.Api.Models
{
    public class Member
    {
        /********************************************************************************
        * Some of these attributes aren't even used. The API doesn't provide them yet, *
        * so I'll have to scrape the information from the HTML itself. Still thinking  *
        * about how to tackle this.                                                    *
        *                                                                              *
        * Author:  Sergio Tapia                                                         *
        * Website: http://www.alphaot.com      
        * Date:    16/12/2010
        * ******************************************************************************/

        #region "Attributes"
        public string ID { get; set; }
        public string Name { get; set; }
        public string Rating { get; set; }
        public string Photo { get; set; }
        public string LastActive { get; set; }
        public string Location { get; set; }
        public string Birthday { get; set; }
        public string Age { get; set; }
        public string Gender { get; set; }
        public string Email { get; set; }


        public string Title { get; set; }
        public string Reputation { get; set; }
        public string DreamKudos { get; set; }
        public string Group { get; set; }
        public string Posts { get; set; }
        public string PostsPerDay { get; set; }
        public string MostActiveIn { get; set; }
        public string JoinDate { get; set; }
        public string ProfileViews { get; set; }

        public string FavoriteOs { get; set; }
        public string FavoriteBrowser { get; set; }
        public string FavoriteProcessor { get; set; }
        public string FavoriteConsole { get; set; }

        public List<Visitor> Visitors { get; set; }
        public List<Friend> Friends { get; set; }
        public List<Comment> Comments { get; set; }
        public string ProgrammingLanguages { get; set; }

        public string Aim { get; set; }
        public string Msn { get; set; }
        public string Website { get; set; }
        public string Icq { get; set; }
        public string Yahoo { get; set; }
        public string Jabber { get; set; }
        public string Skype { get; set; }
        public string LinkedIn { get; set; }
        public string Facebook { get; set; }
        public string Twitter { get; set; }
        public string XFire { get; set; }
        #endregion
    }

    public class Comment
    {
        public string ID { get; set; }
        public string Text { get; set; }
        public string Date { get; set; }
        public string Owner { get; set; }
    }

    public class Friend
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public string Url { get; set; }
        public string Photo { get; set; }
    }

    public class Visitor
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public string Url { get; set; }
        public string Photo { get; set; }
        public string TimeOfLastVisit { get; set; }
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using SharpDIC.Api.Interfaces;
using SharpDIC.Api.Models;

namespace SharpDIC.Api
{
    public class Wrapper : IDownloader
    {
        public void DownloadInformation()
        {

        }

        public Member SearchForMember(int memberID)
        {
            XDocument response = GetXmlResponse(memberID);
            //Member then is responsible to parse and fill his contents.
            Member member = new Member(response);
        }
    }
}



你会在这个变化码?我这样做的权利。

What would you change in this code? Am I doing this right?

编辑:注意DownloadInformation()方法实际上没有做任何事情。我的意图是有一个接口有一个方法,这样我可以从XML的信息是(目前)还能够切换到JSON或其他供应商可能会在将来提供。

Notice that the DownloadInformation() method isn't actually doing anything. My intentions was to have a interface have that method, that way I can get the information either from the xml (for now) but also be able to switch to JSON or whatever the provider might offer in the future.

推荐答案

这是什么代码将被做?

我倾向于做的IoC的方式,我的实现是从我的接口一个单独的程序。业务逻辑只是引用接口和IoC容器(StructureMap是我的首选武器,但每一个他自己......你甚至可以做手工)导线起来的实现。

The way I tend to do IoC, my implementations are in a separate assembly from my interfaces. The business logic just references the interfaces and the IoC container (StructureMap is my weapon of choice, but to each his own... you could even do it manually) wires up the implementations.

要对比,与你在这里至今:

To contrast that with what you have here so far:


  1. 您实现在同一个命名空间(我假设组装)作为你的接口。你自己的勤奋外,没什么好说的,停止你对编程执行,而不是接口。所以耦合的风险依然存在。

  2. 您的实现具有这不是在接口上的公共方法。任何事情对界面的程序将无法看到此方法。这是罚款随着系统的增长。没有理由,一个单一的实现无法实现多个接口。接口隔离原则允许这一点,只要接口本身是独立和独特的一个原因。但是,如果该方法是实施只是一个内部的一部分,私人会更有意义。

  1. Your implementation is in the same namespace (and I assume assembly) as your interfaces. Other than your own diligence, there's really nothing to stop you from programming against the implementation rather than the interface. So the risk of coupling still exists.
  2. Your implementation has a public method that's not on the interface. Anything which programs against the interface won't be able to see this method. This is fine as the system grows. There's no reason that a single implementation can't implement multiple interfaces. The interface segregation principle allows this, as long as the interfaces themselves are separate and distinct for a reason. But if that method is just an internal part of the implementation, private would make more sense.

后面的IoC的基本思想是,一个类应当被提供有依赖性,而不是实例之一。现在,它看起来像你实例化的唯一的东西是一个的XDocument 成员。前者看起来像它的 IDownloader 内部实现的只是其中一部分绘制XML依赖出域(界面)。 (根据您的编辑,这是完全正确的。您可以稍后创建一个柄,而不是XML JSON的 IDownloader 的实施和域不会知道/关心的区别。)的后者仅仅是一个贫血的模式,所以我看不出有什么问题实例化。

The basic idea behind IoC is that a class should be provided with a dependency rather than instantiate one. Right now, it looks like the only things you instantiate are an XDocument and a Member. The former looks like it's just part of the internal implementation for IDownloader which draws the XML dependency out of the domain (interface). (Based on your edit, that's exactly right. You can later create an IDownloader implementation which handles JSON instead of XML and the domain won't know/care the difference.) The latter is just an anemic model, so I see no problem instantiating that.

国际奥委会的实部将是你使用 IDownloader ,它看起来并不像它被尚未使用。

The real part of IoC will be where you use IDownloader, which doesn't look like it's being used yet.

这篇关于这是分离的代码和我在做对吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆