c# - Ninject with Object Initializers and LINQ -
i'm new ninject i'm trying may not possible wanted ask. free-handed below there may typos. let's have interface:
public interface iperson { string firstname { get; set; } string lastname { get; set;} string getfullname(); }
and 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 doing when i'm retrieving data arrays or xml:
public ienumerable<iperson> getpeople(string xml) { xelement persons = xelement.parse(xml); ienumerable<iperson> people = ( 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 tightly couple concrete interface in manner. haven't been able find information in regards using ninject linq objects or object initializers. may looking in wrong places, i've been searching day no luck @ all.
i contemplating putting kernel singleton instance , seeing if work, i'm not sure plus i've heard passing kernel around bad thing. i'm trying implement in class library currently. if not possible, have examples or suggestions best practice in case? in advance help.
edit: based on of answers feel should clarify. yes, example above appears short lived example of 1 piece trying do. let's give bigger picture. instead of xml gathering data through 3rd party web service , i'm creating interface it, data defined object in wsdl or xml string. iperson used both person object , user object. doing inside of separate class library, because needs portable , used in other projects, , handing mvc3 web application , objects used in javascript well. appreciate input far.
your person
class short lived object , doesn't lend used dependency injection. besides this, doesn't contain behavior , poco (plain old clr object). because pocos don't depend on worth abstracting, there no reason abstract them. in other words: given example. don't need iperson
interface. can work directly person
class throughout application.
the getpeople
method though, typically part of service abstract away using di configuration. service interface contains getpeople(string xml)
method however, wrong abstraction, because means supply xml string. when have xml string, there reason ever parse xml string in other way? more convenient have ipersonrepository
interface getallpeople()
method. given implementation xmlpersonrepository
uses xml data source fetch people (from disk, database or knows what).
Comments
Post a Comment