Dependency
Injection is one of the basic design
pattern which makes loosely coupling between base types and derived types.
Why DIP--------advantages
- Dependency Injection is an
important pattern for creating classes that are easier to unit test in
isolation
- Promotes loose coupling between
classes and subsystems
- Adds potential flexibility to a
codebase for future changes
- Can enable better code
reuse
- The implementation is simple
and does *not* require a fancy DI tool.
Different Types
of Dependency Injections are
- Constructor Injection
- Setter Injection
- Interface Injection
- Service Locator
these are various levels of object dependencies like
public Employe(Employee eobj)
{ // this
is contructor Injection } like this other setter,interface, service
injections.
To solve these types of injections we have a principle is called
Inversion of control which promots all advantages of Denpendency injection.
We can see this problem in asp.net mvc more clearly. controller dependent on types
to over come this issue we have many Dependency Injection resolver fraworks : Structuremap, Unity,Nijict, Repository patterns.
we first define an Interface with required members like
Generic interface:
public interface IRepository
{
T
Get<T>(int id) where
T : class;
IQueryable<T> GetAll<T>() where T : class;
void SaveOrUpdate<T>(T entity) where T : class;
void Delete<T>(T entity) where T : class;
}
we can define like : IRepository<T> where T :class also.
Generic abstraction Class:
public class EntityFrameworkRepository : IRepository, IDisposable
{
private Conn
_context;
private readonly ConcurrentDictionary<Type,
object> _dbSets = new
ConcurrentDictionary<Type, object>();
public EntityFrameworkRepository()
{
_context = new Conn();
}
public EntityFrameworkRepository(Conn context)
{
_context = context;
}
public T Get<T>(int
id) where T : class
{
return GetDbSet<T>().Find(id);
}
public IQueryable<T>
GetAll<T>() where T : class
{
return GetDbSet<T>();
}
public void
SaveOrUpdate<T>(T entity) where T : class
{
if (_context.Entry(entity).State == EntityState.Detached)
{
GetDbSet<T>().Add(entity);
}
else
{
_context.Entry(entity).State = EntityState.Modified;
}
_context.SaveChanges();
}
public void
Delete<T>(T entity) where T : class
{
GetDbSet<T>().Remove(entity);
_context.SaveChanges();
}
private DbSet<T>
GetDbSet<T>() where T : class
{
return (DbSet<T>)_dbSets.GetOrAdd(typeof(T), x => _context.Set<T>());
}
public void Dispose()
{
if (_context != null)
{
_context.Dispose();
_context = null;
}
}
}
then consume like
UnityContainer :
Then global.asax
UnityContainer container = new UnityContainer();
container.RegisterType<EmployeeController>();
container.RegisterType<IRepository,
TempEmployee>();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
then Models:
public class ParementEmp :EntityFrameworkRepository
{
Conn context;
public ParementEmp()
{
context = new Conn();
}
[Required]
[DisplayName("Employee
ID")]
[Key]
public int EId { get; set; }
[Required]
[DisplayName("Employee
Name")]
public string EName {
get; set; }
[Required]
[DisplayName("Employee
Location")]
public string
ELocation { get; set;
}
}
public class TempEmployee : EntityFrameworkRepository
{
Conn context;
public TempEmployee()
{
context = new Conn();
}
[Required]
[DisplayName("Employee
ID")]
[Key]
public int EId { get; set; }
[Required]
[DisplayName("Employee
Name")]
public string EName {
get; set; }
[Required]
[DisplayName("Employee
Location")]
public string ELocation
{ get; set;
}
}
public class Conn:DbContext
{
public DbSet<TempEmployee> temps { get;
set; }
public DbSet<ParementEmp> pemps { get;
set; }
protected override void OnModelCreating(DbModelBuilder
modelBuilder)
{
modelBuilder.Entity<TempEmployee>();
modelBuilder.Entity<ParementEmp>();
base.OnModelCreating(modelBuilder);
}
}
at controller level:
public class EmployeeController : Controller
{
IRepository _empcontext=null;
public EmployeeController(IRepository
emp)
{
_empcontext = emp;
}
public ActionResult
Index()
{
var str = _empcontext.Get<TempEmployee>(3);
return View();
}
}
as well we have structure map
http://highoncoding.com/Articles/656_Introduction_to_Dependency_Injection_Using_StructureMap.aspx
http://www.c-sharpcorner.com/uploadfile/dhanaid/introduction-to-structure-map/
http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/unity-framework/
No comments:
Post a Comment