code first - Using Entity Framework w/o Global State -
i made small test function creating entity framework code-first dbcontext instance directly connected sql ce 4.0 file don't global state approach. isn't there better way this, without using dbdatabase static properties?
using system.data; using system.data.entity; using system.data.entity.database; using system.data.sqlserverce; public class sqlcedb { public static t instance<t>() t: dbcontext, new() { dbdatabase.defaultconnectionfactory = new sqlceconnectionfactory("system.data.sqlserverce.4.0"); dbdatabase.setinitializer<t>(new dropcreatedatabasealways<t>()); return new t(); } }
dependency injection lot of people doing. write class has dependency on dbcontext (i.e. it's constructor argument or property decorated dependency), , ioc (inversion of control) container give instance of when class created. every ioc container i've worked has way of registering single instance (instead of creating new instance every time).
popular ioc containers:
- structuremap
- unity
- ninject
there others, these ones see used often.
Comments
Post a Comment