Thursday, January 12, 2012

Where We Need Dependency Injection

Recently I was working with a .Net project which uses Entity Framework .It has a layered architecture where we can find following separate layers.

 Service layer (Eg: Order Services)
 Domain Layer (Eg: Domain entities like book,Order)
 Infrastructure Layer (Repositories, DBContexts,Unit of work pattern Impl) 
 MVC3 Client (Presentation Layer)

 In my case Service methods are called in controller classes in MVC3 client package(according to the mvc pattern). MVC3 client package and Service layer are implemented by two developers .In this case MVC3 client project does not know much details about Service package and all he knows about the service package is that he can get some services by passing values to the service method.

 Eg: Lets say in service package we are having BookService class which is offering borrow book functionality. Then from the controller’s point of view what he wants to do is borrow a book by just passing the book id. Simply the controller doesnot want to know the underline functionality of Bookservice class (DBcontext,repositories,unitofworks)

 But according to the implementation of the BookService class it has only one constructor which has all its dependencies which are unknown by controller (MVC3 client project). Then when it comes to instance creation of BookService class we may end up with big issue. To solve this issue we need to inject dependencies of BookService by using a third party which knows about object creation of BookService class. This is called contractor dependency injection .Actually what controller wants to know is how to use bookservice object instead of knowing how to create BookService object.

Following are the structure of CSharp classes  I was explaining above.

  
public class BookService : IBookService
{
       public IBookRepository _bookrepository;
       public IUnitOfWork _unitOfWork;
    public BookService(IBookRepository bookRepository,IUnitOfWork unitOfWork)
    {
         _bookrepository = bookRepository;
         _unitOfWork = unitOfWork;
     }
    public Book BorrowBook(int bookId)
   {
         //Logic Goes Here
    }
}

Public class BookController
{
   IBookService _bookService;
    public BookController(IBookService bookService)
   {
           _bookService = bookService;
    }

   //Borrow book HTTP POST 
   public void BorrowBook(int bookId)
   {
        _bookService.BorrowBook(bookId);
    }
}

Here We can see we have not create object of BookService so BookController does not need to know about BookService dependencies. In order to get this working we need to use third party dependency injection tools. For .Net project i would recommend winsor container which is more flexible to use. Following tutorial illustrate how to use winsor for dependency injection.

http://stw.castleproject.org/Windsor.MainPage.ashx

Wednesday, August 24, 2011

දයාබර අම්මේ.....



සිහි කරන්නට ප්ර්‍ථම මතකය කළෙමි තැත්  මම ඔබ ගැන
දිවුණි මතකය දශක දෙකකට පැයට සැතපුම් වේගයෙන් 
දැණුනි මනසට සිනිදු සිතුවිලි  කිරි සුවඳ සහ මුසු වෙමින්
දිටිමි මා පොඩි පැටෙකු විලසින ළී අසෙකු මත සුරතළින්....

දිටිමි මෑතින් සිටිනු ඔබ මා දෙසම නෙත් දෙක යොමුවමින්
මවක් යනු පින් කඳක් බව ඔබ හැඟෙව්වේ මිතුරකු ලෙසින්
එදා මට ඒ දැණුන සෙනෙහස දැනේ අදටත් නොඅඩුවෙන් 
ණය ගැතියි මම ජාති ජාතිත්  ඒ පෙවූ කිරි නාමයෙන්......














Tuesday, August 23, 2011

Change the Icon of Eclipse internal XML Editor

Eclipse IDE is very popular since it can be easily configurable according to the developer's  requirements. Creating plugins for third party applications made our life too easy.So during this summer I thought of involving Google Summer of codes 2011 program and contribute for the project Apache Cayenne. Here My task was to create an external editor for Eclipse which supports Cayenne Modeler which is a Java bases application.
     There I had to deal with a bit tricky problem which I have to filter cayenne related xml files and load them with the cayenne icon.But When we double click it should be opened with default eclipse xml editor. Initially I thought it is going to be a bit harder task and thought of writing a new mxl editor for eclipse. But after few hours research i found an easy way of implementing this.
Pre-Requirements for this example : Basic knowledge is Eclipse PDE.



        
            
                
            
        



                         
            
            
         
 
As describes in above plugin.xml file we are using an external editor call "testing.other.validator" which has the content type binding called "internal.editor". In this binding it filter all xml files which has the root element as "domian-map". In your case you can ad the root element of your preferred xml files which you want to load it with your own icon. So after it find that specific xml file in your eclipse workspace it loads that xml file with the the "cayenne.png" as in above example. Once you double click on this icon the launcher is executed.(Actually it's open method is fired)

Follwoing is the Launcher class mentioned in pulugin.xml.
public void open(IPath file) {
 System.out.println("Launch Successful : file Location = "+ file.toString());
 File fileToOpen = file.toFile();
 if (fileToOpen.exists() && fileToOpen.isFile()) {
   IFileStore fileStore = EFS.getLocalFileSystem().getStore(fileToOpen.toURI());
   IWorkbenchPage page = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage( );
 try {
            // Select the appropriate internal editor and launch the file
     IDE.openInternalEditorOnFileStore(page, fileStore);
 } catch (PartInitException e) {
  logger.error("error while opening cayenne file");
     }
}

Wednesday, March 30, 2011

Object-Relational Mapping

Basic idea of Object Relational Mapping(ORM) is converting data between incompatible types in oop languages.When it comes to data bases it is about creating creating virtual data base objects which can be used in object oriented programming.




Above diagram clearly explains the task of ORM mapping. To get the overall task done first we need to select a good data base driver which create the connection between our program and the data base. this method is efficient than manually create data base connection every where and close the connection.(Better to hand over db connection creation part and closing part to a middle man so that we can have a more readable code) This is a good tutorial to be familiar with jdbc templates. Also it is good to use spring hibernate for ORM modelling so that we can implement our program in a configurable manner using XML based technologies. Following diagram explains clear overview about spring architecture.[reference]



But still we have to do some boring coding part for mapping objects classes for the data base tables.In this context i recommended you to use apache cayenne which auto mated the class implementation. Here we just have to use UI and create needed tables and mentioned the relation ships (one to one or many to one etc.). And also we can mentioned data base drivers and create data base in our disk. Here is the official tutorial provided by apache cayenne team which is very interesting one.

By using above technologies you can easily work with data base programming which was a really boring and harder task earlier.So now companies do not need special type come data base programming developers. istead it can be handle by normal oop programmer.


Monday, August 30, 2010

Friday, July 23, 2010

සෙනිඩ් එකේ අපි...

ඔන්න දෑන් අපි ටිකක් නිදහස්...සෙනිඩ් එකේ ට්‍රේනින් නම් ඉවර උනා.... එත් මේ කෙහෙල්මල් රෙපෝර්ට් එකක් තියනවනෙ ලියන්න...ලියන්න කිව්වට ලියන්නම නෙවේ..ට්යිප් කරන්න...

කරන්න දේවල් ගොඩක් තියනව මේ මාස දෙකේ කරන්න.මොනා උනත් දැන් මොනිටරය දිහා පැය ගානක් බලගෙන ඉන්න එක සිම්ප්ලෙ දෙයක්.... සෙනිඩ් එකෙන් තමා පුරුදු උනේ... මොනා උනත් කට්ට්ය දල යන්න නම් දුකඉ.. විශේශයෙන්ම අපේ සොල්ටුරා අක්කවඉ ( ;) ) පල්ලයා ඉන්න Hර්‍ එක්සෙකටිව් අක්කවඉ ... තිලන්කා වඉ ... එතකොට...ගයනි අක්ක... එයා තමා මේ කොම්පැනියෙ එකෙ ඉන්න හොන්ඩම එක්කෙනා...

ටෙක්නිකල් පත්තෙන් ගත්තොත් නම් විමල් අය්යා , සම්පත් අය්යා (මෙය ගිය අත්මෙ කී බොර්ඩ් එකක් එක්ක පොඩි ටරහක් ථියන කෙනෙක්) නිරොජන් අය්යා.. ජේසන් ම්ම්ම් එතකොට දිමුතු අය්යා (මෙයා තම කොම්පැනියෙ අහින්සකම කෙන .. දෙක නිරොජන්)... එතකොට මේකෙ ඉන්න තැම්පත් ම කෙනා තමා අපේ වරුන අය්ය.. කෑම බීම අතින් නම් අපේ කුමුදු අය්යට ගයන්න කෙනෙක් නැහැ... එතකොට අපේ සදරේනු අක්කවත් මතක් කරන්න ඔනේ.. ( ලව්ඩ් ස්පිකර් ).
දුලීක අක්කවත් මතක් කරන්නට් ඔනේ.

එතකොට රවීශ් ව මතක් කරන්නත් ඔනෙ... ( මෙය අපේ මැශින් එකක් හඩන්න නම් එන්නෙ කිව්වට පස්සෙ පැය බාගෙකට විටර පස්සෙ.. එත් උශානිගෙ.... එහෙම )

අය්යො හර්ශ අය්යව අමතක උනා නෙ.. මෙය තමා කොම්පැනියෙ ඉන්න ඩයල් එක..( ඔයල සෙනිඩ් එකට ගියාම ලොකු මග් එකක් එක්ක මිටි කොල්ලෙක් දැක්කොත් එය තමා හර්ශ.. මොනා උනත් නියම වැඩ කාරයෙක්)

තව ලියන්න දේවල් නම් ගොඩාක් තියනව.. එත් සින්හලෙන් ලියන්න කම්මැලි..මම බ්ලොග් ලියන්න පුරුදු උනේ විමල් අය්යා නිසා. ගොඩාක් ස්තුටි එකට. මෙයත් කවද හරි හර්ශ වගේ ඩයල් එකක් වෙනව සුවර්.. එහෙනම් මම නවතිනවා.. නම් ලියන්න අමතක උන කට්ටිය තරහ වෙන්න නම් එපා හොදේ.

සෙනිඩිඅන් කෙනෙක් වෙලා අපහු එන්න ලොකු කැමත්ත කුත් තියනව.. කට්ටියටම ගොඩාක් ස්තුති

Wednesday, July 21, 2010

Last few days @hsenid mobile solutions

This gonna be the last couple of days @ hsenid mobile solutions "http://www.hsenidmobile.com/. I want to recall my memory when we were asked to select training places. I had a dream to have my internship at mubhaser http://www.mubasher.net/en/CareersSriLanka.aspx

Unfortunately i missed that opportunity and finally i had to chose hsenid. I thought it is a company which develops mobile applications.( mobile operating systems , applications). OOPS...!!!
I was totally in correct. It is a place where develop core mobile applications. It means they are using USSD gate ways, SMSC ,MMSC and create some valuable applications. As an example at the bigging i worked on a recharge application for http://www.dst-group.com/ .I am not going to talk more about this.

When i go back to past 6 moths i can find a significant change in my life.Before this internship i could not even stand in front of a monitor more than one hour. And also dint have a self confidence regarding what i am i capable to do. But this training has injected me those qualities. Actually now what i feel is hsenid is a paradise to learn. Before this internship i have never used linux based operating system. But here we always using ferora 6 and fedora 12. It was a real challenge for me to get familiar with os. Because of the supportive supervisors i could manage it. Specially Mr.Vimalaharan. As a result of this energy which was injected to my brain by hsenid mobile solutions this time i could participate to google summer of code 2010 and could able to pass from mid evaluations. Thanks lot hsenid. i am very lucky to have my internship @hsenid mobile solutions.

Actually it is really hard to leave this supportive members. But what to do.... time has come to leave... But i am having a idea to join hsenid as a employ one day..