locked
Writing COde and Structuring RRS feed

  • Question

  • Hi,

    I have a question which is more specific in showing good knowledge on writing and structure code.  For that purpose the question would be creating a
    class named "DataAccess".  And in that i want to perform two things.

    a.  In DataAccess we need to write Linq to Objects code that pulls in the name and phone number from a table called "Employee"
    b.  Also write code in the same class that would pull that information using LINQ from an XML file called "Employee.xml"

    I want code which solved two question above in one single class and These things should focus more on writing and structuring code efficiently.
    And finally a c# class that instantiates a data access class [call it "DataAccess"]

    Any help would be greatly appreciated.

    Thanks,
    SumRaya

    Tuesday, February 9, 2010 10:09 PM

Answers

  • Here is the actual question i had from an interviewer...I want the exact answer for this question..

    There is no "exact answer" to this question. There are many options for solving this problem. I assume the interviewer wanted to know how *you* would solve it.

    I *ALREADY* gave you how I would solve it in my original reply.

    The "best" way is very open to opinion. Some teams really like Linq to SQL, others like EF, others like to use the enterprise library, others won't use anything but CSLA, and still others like the code everything themselves.

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Wednesday, February 10, 2010 4:08 PM

All replies

  • First, you need an Employee class to manage the properties and methods for an employee. Then I would name the class you are talking about above "Employees", since its purpose is to pull Employee information for a set of employees. ("DataAccess" would imply something more generalized.)

    To use Linq to Objects you first need the data in some searchable way within your application. So you need to open the database, access the table, and use the table data to populate a List<Employee>.

    Here is a link to an example on building a List<Customer> (you can change it to match your Employee requirement):

    http://msmvps.com/blogs/deborahk/archive/2009/07/03/generics-building-a-list-of-customers.aspx

    Here is link to an example of populate business objects from a SQL Server table:

    http://msmvps.com/blogs/deborahk/archive/2009/07/10/populating-a-business-object-from-a-datatable.aspx


    And here is an example of finding nodes in an XML file using Linq to XML:

    http://msmvps.com/blogs/deborahk/archive/2010/01/31/finding-a-node-in-an-xml-string.aspx

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Tuesday, February 9, 2010 11:24 PM
  • Thanks for the reply Deborah..
    Here is the actual question i had from an interviewer...I want the exact answer for this question..

    Please write this code: 

     
    A C# class that instantiates a data access class [call it “DataAccess”]

    a.
           In DataAccess write Linq to Objects code that pulls in the name and phone number from a table called “Employee”
    b.      Write code in the same class that would pull that information using LINQ from an XML file called “Exmployee.xml”

    This will help them give an idea on how they write and structure their code.

    Should i create a database access layer using Application Blocks or is there any other best way...
    What woudl be you best possible solution for the above question.
    Give me your thoughts and how to implement this.
    Any  help would be greatly appreciated.

    Thanks,
    SumRaya

    Wednesday, February 10, 2010 5:02 AM
  • The question never mentions any database. Why would you bother about database access?
    The only thing asked is the two Linq queries.

    Edit: There is a problem in that question: it asks for a Linq to Objects query to retrieve data from a table?

    Wednesday, February 10, 2010 8:56 AM
  • Here is the actual question i had from an interviewer...I want the exact answer for this question..

    There is no "exact answer" to this question. There are many options for solving this problem. I assume the interviewer wanted to know how *you* would solve it.

    I *ALREADY* gave you how I would solve it in my original reply.

    The "best" way is very open to opinion. Some teams really like Linq to SQL, others like EF, others like to use the enterprise library, others won't use anything but CSLA, and still others like the code everything themselves.

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Wednesday, February 10, 2010 4:08 PM
  • I think it is Linq to SQL to get data from a table. but we can create object for those fields that we have in table and then poulate that list using the URL

    Here is a link to an example on building a List<Customer> (you can change it to match your Employee requirement):

    http://msmvps.com/blogs/deborahk/archive/2009/07/03/generics-building-a-list-of-customers.aspx

    Please correct me if iam wrong.

    Thanks,
    SumRaya
    Wednesday, February 10, 2010 10:08 PM
  • Thanks for the reply..I am just going to send to .cs file which shows all the implementation for these questions.
    Wednesday, February 10, 2010 10:08 PM
  • Sounds like a good plan.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Wednesday, February 10, 2010 10:29 PM
  • How to you answer the second part..i am trying to build a query using linq to read from xml.

    Write code in the same class that would pull that information using LINQ from an XML file called “Exmployee.xml”

    Should we populate name and phonenumber into List object of type EMployee ?  Please let me know how can we proceed.

    Any help would be greatly appreciated.

    Thanks,
    SumRaya
    Wednesday, February 10, 2010 11:15 PM
  • I would populate business objects, yes.

    And if I keep helping you with part of your job interview, do I get part of your first year's pay? :-)
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Wednesday, February 10, 2010 11:46 PM
  • Yes I am fine with what you say.. :-)

    I have implemented that as given below.  But I am trying to see if we can populate the Names and Phone Numbers into a single List<Employee> object Instead of two separate variables.

    XElement

     

    xmlDoc = XElement.Load("Employees.xml");

     

    // variable to load Employee Names

     

    var Employes = xmlDoc.Elements("Employee").Select(emp => emp.Element("Name").Value).ToList();

     

    // variable to load Employee phone numbers

     

    var PhNumber = xmlDoc.Elements("Employee").Select(emp => emp.Element("PhoneNumber").Value).ToList();

    Any help would be greatly appreciated.

    Thanks,
    Suman

    Thursday, February 11, 2010 4:27 AM
  • var nameAndPhones = xmlDoc.Elements("Employee")
                              .Select(emp => new { Name = emp.Element("Name").Value,
                                                   PhoneNumber = emp.Element("PhoneNumber").Value
                                                 }).ToList();
    Thursday, February 11, 2010 9:58 AM
  • Thanks for the reply...my method retuns a list object of type employee List<Employee> which has Name and Phone Number.  How can i put this anonymous type nameAndPhones above into a List<Employee> object.

    Any help would be greatly appreciated.

    Thanks
    SumRaya
    Thursday, February 11, 2010 7:43 PM
  • I have a blog post on putting anonymous types into a list:

    http://msmvps.com/blogs/deborahk/archive/2009/08/17/defining-lists-of-anonymous-types.aspx

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Thursday, February 11, 2010 8:46 PM
  • Thanks for the reply...my method retuns a list object of type employee List<Employee> which has Name and Phone Number.  How can i put this anonymous type nameAndPhones above into a List<Employee> object.

    If your Employee class has a constructor taking name and phone number, just replace the

     new { ... }

    with

     new Employee(emp.Element("Name").Value, emp.Element("PhoneNumber").Value)

    If your Employee class doesn't have such a constructor, you can use an object initializer:

    new Employee { Name = emp.Element("Name").Value,
                   PhoneNumber = emp.Element("PhoneNumber").Value
                 }
    Friday, February 12, 2010 10:34 AM