Ninject with Object Initializers and LINQ
- by Alexander Kahoun
I'm new to Ninject so what I'm trying may not even be possible but I wanted to ask. I free-handed the below so there may be typos. Let's say I have an interface:
public interface IPerson
{
string FirstName { get; set; }
string LastName { get; set;}
string GetFullName();
}
And a concrete:
public class Person : IPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string GetFullName()
{
return String.Concat(FirstName, " ", LastName);
}
}
What I'm used to doing is something like this when I'm retrieving data from arrays or xml:
public IEnumerable<IPerson> GetPeople(string xml)
{
XElement persons = XElement.Parse(xml);
IEnumerable<IPerson> people = (
from person in persons.Descendants("person")
select new Person
{
FirstName = person.Attribute("FName").Value,
LastName = person.Attribute("LName").Value
}).ToList();
return people;
}
I don't want to tightly couple the concrete to the interface in this manner. I haven't been able to find any information in regards to using Ninject with LINQ to Objects or with object initializers. I may be looking in the wrong places, but I've been searching for a day now with no luck at all.
I was contemplating putting the kernel into an singleton instance and seeing if that would work, but I'm not sure that it will plus I've heard that passing your kernel around is a bad thing. I'm trying to implement this in a class library currently. If this is not possible, does anyone have any examples or suggestions as to what the best practice is in this case? Thanks in advance for the help.
EDIT:
Based on some of the answers I feel I should clarify. Yes, the example above appears short lived but it was simply an example of one piece that I was trying to do. Let's give a bigger picture. Say instead of XML I am gathering all my data through a 3rd party web service and I'm creating an interface for it, the data could be a defined object in the wsdl or it could sometimes be an xml string. IPerson could be used for both the Person object and a User object. I will be doing this inside of a separate class library, because it needs to be portable and will be used in other projects, and handing it to an MVC3 Web Application and the objects will be used in javascript as well. I appreciate all the input so far.