c# - Linq to Entities - 3-tier Architecture -



c# - Linq to Entities - 3-tier Architecture -

in past few months i've learned alot linq-to-entities , 3-tier architecture dao/dal/repository. i've few things in mind keeps bugging me. i've 3 questions you'll see below.

there alot of ways create repository work way "the" way of making repository work in ways of performance.

1) initialize datacontext in constructor

public class repository : irepository { private datacontext context; public repository() { context = new datacontext(); } public ilist<entity> getentities() { homecoming (from e in context.entity select e).tolist(); } }

2) use "using"

public class repository : irepository { public ilist<entity> getentities() { using (datacontext context = new datacontext()) { homecoming (from e in context.entity select e).tolist(); } } }

3) in way (please comment)

i'll set suggestion here others comment

also seems people repository should homecoming iqueryable businesslayer while others it's improve homecoming ilist. oppinion on this?

the above code samples in first question pointing repository, bestway implement repository in businesslayer (initialize in constructor, utilize "using"??)

either works think. main thing should making object contexts short lived (imho). hence think have 2 choices: -

create / destroy context within single method phone call e.g. using statement per sec example.

create / destroy context when create / destroy repository - in case repository should both implement idisposable , wrapped in using statement, , should short lived. benefit of approach repository methods query, there no using (new objectcontext()) polluting method; flip-side reposibility passes onto client dispose of repository. using mechanism means can compose queries within iqueryable<> (provided execute query before disposing of repository). example:

public class repository : idisposable { datahubcontext context = new datahubcontext();

public iqueryable<payment> getpayments() { homecoming context.payments; } public void dispose() { context.dispose(); }

}

formatting has gone bit funny in - sorry....and in calling code: -

public class clientcode { public void displaypaymentsonscreen() { payment[] payments; using (var repository = new repository()) { payments = repository.getpayments().where(p => p.amount > 100).toarray(); } // stuff info here... } }

c# linq-to-entities dao 3-tier

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -