Is this code untied and am I doing it right?

I am trying to learn and practice IoC and how to program against interfaces instead of objects. It is quite difficult for me. Here is the code I have. Are there any mistakes that I made? Call them to me to help me understand how this actually fits when in practice.

Thanks!

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 it right?

Edit: Note that the DownloadInformation () method does not actually do anything. My intentions were to have an interface with this method, so I can get information either from xml (at the moment), but also be able to switch to JSON or whatever the supplier would not offer in the future.

+7
source share
4 answers

What will this code do?

As I usually do IoC, my implementations are in a separate assembly from my interfaces. Business logic simply refers to the interfaces and the IoC container (StructureMap is my weapon of choice, but to each of them ... you can even do it manually) paves the way for implementation.

To contrast with what you still have:

  • Your implementation is in the same namespace (and I assume the assembly) is your interface. Besides your own zeal, you really have nothing to interfere with programming against implementation, not interface. Thus, the risk of adhesion still exists.
  • Your implementation has a public method that is not associated with an interface. Anything that programs against the interface cannot see this method. This is normal as the system grows. There is no reason to believe that one implementation cannot implement multiple interfaces. The principle of interface segregation allows this if the interfaces themselves are separate and clear for some reason. But if this method is just an internal part of the implementation, private will make more sense.

The basic idea of ​​IoC is that the class should be provided with a dependency, and not create an instance. Right now, it seems the only thing you are creating is XDocument and a Member . The first looks like part of an internal implementation for IDownloader , which infers XML dependency on a domain (interface). (Based on your editing, that's for sure. You can later create an IDownloader implementation that processes JSON instead of XML, and the domain will not know / ignore the difference). The latter is just an anemic model, so I do not see a problem with this.

The real part of IoC will be where you use the IDownloader , which does not yet look the way it is used.

+3
source

Sergio,

a few things I would change (not so much based on IoC - more interface specification):

 interface IDownloader<T> { T DownloadInformation(); } 

it seems better to me, then you can implement this in your concrete class:

 public class Wrapper : SharpDIC.Api.Interfaces.IDownloader<string> { public Member SearchForMember(int memberID) { XDocument response = GetXmlResponse(memberID); //Member then is responsible to parse and fill his contents. Member member = new Member(response); } public string DownloadInformation() { throw new NotImplementedException(); } } 

obviously i used "string" as a type, but you could use any type needed for implementation. I also changed List to IList:

 public IList<Visitor> Visitors { get; set; } public IList<Friend> Friends { get; set; } public IList<Comment> Comments { get; set; } 

just improves implementation details (after all, we discuss interfaces :-))

that is all - David’s answer concerns β€œmental” material (good David) ...

+2
source

Some quick points (not about ioc):

Your member class must be reorganized.

Add IList<IInstantMessanger> instead of all addresses like Msn, Icq, etc. In addition, you need to change your class every time you want to remove or add an IM type (and therefore violate the Open / Closed principle)

Move favorites to a separate class and create an IList<> . For the same reason.

0
source

About the data types of your objects.

  • try using other data types other than line Id -> int, Guid
  • Reputation β†’ numeric value? int, float
  • List -> IList -> use interfaces instead
0
source

All Articles