The actual problem, apparently, is that you cannot figure out how to set the return type to Func<> . To be able to do this in general, you will need to make InDb a generic type as well.
public TRet InDb<TRet>(Func<BodModelContainer, TRet> workingWithDb) { TRet ret = default(TRet); using (var dbContext = new BodModelContainer()) { ret = workingWithDb(dbContext); } return ret; } public Member GetMemberById(int memberId) { return InDb(dbContext => { return dbContext.Members.Find(new[] { memberId }); }); }
It should work (all code is untested here) or using anonymous methods you can have a local variable and make .Net do all the unpleasant work.
public void InDb(Action<BodModelContainer> workingWithDb) { using (var dbContext = new BodModelContainer()) { workingWithDb(dbContext); } } public Member GetMemberById(int memberId) { Member member; InDb(dbContext => { member = dbContext.Members.Find(new[] { memberId }); }); return member; }
Of course, thatβs all said, I donβt know if this level of redirection / abstraction is useful - you make it a little more complicated with a small tangible gain (if any). If there is not much work to configure and break the dbContext used in InDb , I don't think this is very useful.
source share