D
Dan_Bev
Guest
Hi to all, I'm studying "Dependency Injection Principles, Practices and Patterns" by Manning.
I want to share with you a simple architecture just to know if I'm on the correct path or I miss something.
Suppose we have a simple program that read CDR (Charging data record) from text file and write them to a DB.
I can assume a code like that
void DoStuff(string filePath)
{
CDRReader cdrReader = new CDRReader(filePath);
CDR cdr = null;
List<CDR> listOfCdrs = new List<CDR>();
while ( (cdr = cdrReader.GetNext()) != null)
{
listOfCdrs.Add(cdr);
}
DBLayer databaseLayer = new DBLayer();
databaseLayer.SaveCdrs(listOfCdrs);
}
Now, I analize the code for introducing DI:
void DoStuff(string filePath)
{
iCDRParser cdrParser = new CDRPlainTextParser();
iCDRReader cdrReader = new CDRStreamReader(filePath, cdrParser);
CDR cdr = null;
List<CDR> listOfCdrs = new List<CDR>();
while ( (cdr = cdrReader.GetNext()) != null)
{
listOfCdrs.Add(cdr);
}
//this does not interest me for now
DBLayer databaseLayer = new DBLayer();
databaseLayer.SaveCdrs(listOfCdrs);
}
interface iCDRReader
{
CDR GetNext();
}
class CDRStreamReader : iCDRReader
{
//It depends on Stream has it declare on class name, I think it's correct
StreamReader _stream;
iCdrParser _cdrParser;
public CDRStreamReader(string path, iCDRParser cdrParser)
{
//guard not shown for brevity
_stream = new StreamReader(path);
_cdrParser = cdrParser;
}
public CDR GetNext()
{
//don't care for the example how to dispose stream
string line;
if ( (line = _stream.ReadLine()) == null)
return null;
return _cdrParser.Parse(line);
}
interface iCDRParser
{
CDR Parse(string line);
}
class CDRPlainTextParser : iCDRParser
{
public CDR Parse(string line)
{
CDR cdr = new CDR();
//stuffs here
return cdr;
}
}
And now what torments me:
Thanks to all those who will dedicate a few minutes of their time to me
Continue reading...
I want to share with you a simple architecture just to know if I'm on the correct path or I miss something.
Suppose we have a simple program that read CDR (Charging data record) from text file and write them to a DB.
I can assume a code like that
void DoStuff(string filePath)
{
CDRReader cdrReader = new CDRReader(filePath);
CDR cdr = null;
List<CDR> listOfCdrs = new List<CDR>();
while ( (cdr = cdrReader.GetNext()) != null)
{
listOfCdrs.Add(cdr);
}
DBLayer databaseLayer = new DBLayer();
databaseLayer.SaveCdrs(listOfCdrs);
}
Now, I analize the code for introducing DI:
- The core object of all it's the CDR class, a POCO. I think I don't need to extract an interface for this: real implementations need to depends directly by CDR
- Let's concentrate for now only on CDRReader: I assume that I can have in future various way to read from CDR' source (e.g. stored from database). So I think I can extract an interface for this. Moreover, I can assume that I can have different file representations of CDR (plain text, json, xml), so I refactor in this way
void DoStuff(string filePath)
{
iCDRParser cdrParser = new CDRPlainTextParser();
iCDRReader cdrReader = new CDRStreamReader(filePath, cdrParser);
CDR cdr = null;
List<CDR> listOfCdrs = new List<CDR>();
while ( (cdr = cdrReader.GetNext()) != null)
{
listOfCdrs.Add(cdr);
}
//this does not interest me for now
DBLayer databaseLayer = new DBLayer();
databaseLayer.SaveCdrs(listOfCdrs);
}
interface iCDRReader
{
CDR GetNext();
}
class CDRStreamReader : iCDRReader
{
//It depends on Stream has it declare on class name, I think it's correct
StreamReader _stream;
iCdrParser _cdrParser;
public CDRStreamReader(string path, iCDRParser cdrParser)
{
//guard not shown for brevity
_stream = new StreamReader(path);
_cdrParser = cdrParser;
}
public CDR GetNext()
{
//don't care for the example how to dispose stream
string line;
if ( (line = _stream.ReadLine()) == null)
return null;
return _cdrParser.Parse(line);
}
interface iCDRParser
{
CDR Parse(string line);
}
class CDRPlainTextParser : iCDRParser
{
public CDR Parse(string line)
{
CDR cdr = new CDR();
//stuffs here
return cdr;
}
}
And now what torments me:
- It's correct that GetNext() and Parse(string) method return a CDR? I assume that's a POCO, so I think it's an overkill implements an iCDR interface
- On parser's Parse(string) method it's correct the "new CDR()" line? Where should i be able to create CDR objects?
- It's correct parser's Parse(string) signature, forcing a "string" as parameter?
Thanks to all those who will dedicate a few minutes of their time to me
Continue reading...