asp.net mvc 3 - unity mvc3 - configuring using database first approach -
i´ve been checking microsot unity ioc , found examples using code first approach. on other hand cannot find tutorial or configuration in order include unity ioc edmx files using database first approach. glad in shed light on it.
i tried using http://unitymvc3.codeplex.com/ , using unity 2.1 directly = http://unity.codeplex.com/
sorry cannot provide code i´m confused ioc patterns , not able generate demo solution. brgds.
ioc turning of object inside out instead of containing internal hard references objects (dependencies), instead same objects passed outside. turning inside out inversion of control, , injecting of objects needs dependency injection , done container (unity).
all ioc containers same, have way register or discover dependencies , way resolve reference. resolution involves asking reference of fooclass
, getting object in return. don't ask concrete type fooclass , instead ask ifooclass
decouple usage actual type gets passed in.
so in case need register ef data context dependency in unity. have not used unity before please forgive minor errors.
container.registertype<yourcontext, yourcontext>();
add dependency class. have foorepository implements ifoorepository.
public foorepository : ifoorepository { private yourcontext context; public foorepository(yourdatacontext context) { this.context = context; } }
in mvc 3 register unity default dependency resolver means requests controllers fed through it;
protected void application_start() { ... var container = new unitycontainer(); container.registertype<yourcontext, yourcontext>(); container.registertype<ifoorepository, foorepository>(); container.registercontrollers(); dependencyresolver.setresolver(new unitydependencyresolver(container)); }
now controller can add arguments it's constructor , have them populated.
public class mycontroller : controller { private ifoorepository repository; public mycontroller(ifoorepository repository) { this.repository = repository; } }
when controller instantiated receive instance of ifoorepository, receive reference yourcontext. continues way down chain.
edit
the edmx file designer creates c# context class under covers. click on surface , view properties see name of class.
so can register in same way other class. in case.
container.registertype<model1container, model1container>();
hope helps.
Comments
Post a Comment