Monday, November 11, 2019

Advantages of Dependency Injection & Repository Pattern in Asp.net core | Changing the Data Source from Sql to Xml With Loose Coupling Code

Hello & As-salam u alikum ! , In the Previous article we had learnt & performed dependency Injection with Repository Pattern in asp.net Core C#,
Now we will see the benefit to  use dependency Injection, Lets Begin with the name of Almighty Allah.



Note : For Beginner Guide Articles :
https://dotnetcorecommunity.blogspot.com/p/aspnet-core-tutorials-paracticals.html

Step# 1:  
Read all the previous articles and implement all the explained code and Concepts to move one step ahead.

Step# 2:  

Create a new folder named as "Data" in wwwroot folder and  also create an xml file named as "XMLfil0.xml" and paste the following xml in that file to show some data on view.

<?xml version="1.0" encoding="utf-8" ?>
<Students>
  <Student Id="1">
    
    <Name>Salman Masood</Name>
    <Department>Software</Department>
    <Email>theideassolution@gmail.com</Email>
  </Student>

  <Student Id="2">

    <Name>Ali Ahmed</Name>
    <Department>Software</Department>
    <Email>ali@gmail.com</Email>
  </Student>
</Students>

Step# 3:  

Create a new class in the model folder & named it as "XmlRepository" ,Implement the Interface that we made in previous article name was "IStudentRepository" and code as shown below, in the below class Constructor, I just read the xml file and get into a list to perform Crud operations.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace demoinjection.Models
{
    public class XmlRepository : IStudentRepository
    {
        IEnumerable<TblStudent> list;
        
        public XmlRepository()
        {
             list = XDocument.Load("../demoinjection/wwwroot/Data/XMLFil0.xml").Element("Students")
              .Descendants("Student")
                .Select(x => new TblStudent
                {


                    Id =Convert.ToInt32(x.Attribute("Id").Value),
                    Name = x.Element("Name").Value,
                    Department = x.Element("Department").Value,
                    Email = x.Element("Email").Value
                });


        }
        public TblStudent Add(TblStudent Student)
        {
            throw new NotImplementedException();
        }

        public TblStudent Delete(int Id)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<TblStudent> GetAll()
        {
            
            return list;
        }

        public TblStudent GetStudent(int Id)
        {
            return list.Where(x => x.Id == Id).SingleOrDefault();
        }

        public TblStudent Update(TblStudent Student)
        {
            throw new NotImplementedException();
        }
    }
}


Step# 4: 
 Now, Go to the Startup.cs file to configure the middle-ware. change the class name to XmlRepository  from the class which I have shown by red circle in the below image.

after changing the class name :


Step# 5: 
 Just Run the application, your data source has been changed without making any changes in your entities and business logic layer. 

In the above Image you can clearly see the data is coming from Xml file .

Conclusion :
that's all for this blog, we have successfully change the data source without effecting the BLL and DAL & If I want to move back to Sql server data source ,then I just have to change that repository class as I shown you in above code.
Thank you for Reading. Have a Good Day. Subscribe to my youtube Channel :
https://www.youtube.com/channel/UCHAmv9m1l_BqbiPFvwWv1aw

0 Comments:

Post a Comment

Do not Add Spam links in the Comment Box

Subscribe to Post Comments [Atom]

<< Home