Unity IoC database transaction, not fetching updated data, using ContainerControlledLifetimeManager [on hold]












-3














i am using unity ioc in c#,and DbContext with ContainerControlledLifetimeManager for application context with three layer architecture using entity framework. I have to ask either this pattern is better in window form application or is there any better suggestion . I also face a problem in getting latest data when data is updated in database and after refreshing window form it loads old data.



As I am also using transactions using entity framework and therefore I can not use TransientLifetimeManager for DbContext class. I need same context in different DA classes to perform multiple operation in single transaction. Can any one suggest me the better approach to tackle this kind of situation. Any help is appreciated.



Sample Project Example




//Generic Repository




public interface IRepository<ModelClass, EntityClass>
where ModelClass : class
where EntityClass : class
{
void Add(ModelClass entity);
void Delete(ModelClass entity);
void Update(ModelClass entity,int id);
bool Save();
}



//Generic Repository implementation




public class GenericRepository<ModelClass, EntityClass> : IRepository<ModelClass, EntityClass>
where ModelClass : class
where EntityClass : class
{
private ApplicationContext context = null;
private DbSet<EntityClass> Entity = null;

public GenericRepository(ApplicationContext context)
{
this.context = context;
Entity = context.Set<EntityClass>();
}
public void Add(ModelClass model)
{
EntityClass enityClass = AutoMapper.Mapper.Map<ModelClass, EntityClass>(model);
this.Entity.Add(enityClass);
}

public void Delete(ModelClass model)
{
EntityClass enityClass = AutoMapper.Mapper.Map<ModelClass, EntityClass>(model);
this.Entity.Remove(enityClass);
context.SaveChanges();
}

public void Update(ModelClass model, int id)
{
EntityClass dbEntity = Entity.Find(id);
EntityClass entityClass = AutoMapper.Mapper.Map<ModelClass, EntityClass>(model);
context.Entry(dbEntity).CurrentValues.SetValues(entityClass);
context.Entry(dbEntity).State = EntityState.Modified;

}

public bool Save()
{

bool status = false;
try
{
int result = context.SaveChanges();
if (result > 0)
{
return status = true;
}
else
return status;
}
catch (DbEntityValidationException dbEx)
{
throw;
}
return status;
}



// student dataaccess interface




 public interface IStudent : IRepository<Student, StudentEntity>
{
List<Student> GetStudentById(int studentId);
}


//implementation of student access interfaces

public class StudentAccess : GenericRepository<Student, StudentEntity>, IStudent
{

public StudentAccess(ApplicationContext context)
: base(context)
{
this.context = context;
}

Public void Add(student std)
{
base.Add(std);
base.save();
}

Public void Update(student std,int studentid)
{
base.update(std,studentId);
base.save();
}


public List<student> GetStudentById()
{

var data=context.student.ToList();
return data;

}
}



//student service layer




 public class StudentService 
{
ApplicationContext Context;
IStudent student;

public StudentService(ApplicationContext Context, IStudent student)
{
this.Context = Context;
this.student = student;
}

Public void SaveStudent(student std)
{
this.student.Add(std);
}

Public void UpdateStudent(student std,int studentId)
{
this.student.Update(std,studentId);
}
}



//implementation of unity container




 public class DependencyOfDependency : UnityContainerExtension
{
protected override void Initialize()
{

// Register Data access layer

Container.RegisterType<DbContext, LogisticsERP.DA.Entities.ApplicationContext>(new ContainerControlledLifetimeManager());
Container.RegisterType< IStudent,StudentAccess>();


//Register Service Layer Classes

Container.RegisterType<StudentService>();
}
}









share|improve this question







New contributor




amir javaid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by t3chb0t, Sᴀᴍ Onᴇᴌᴀ, Graipher, Jamal yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – t3chb0t, Sᴀᴍ Onᴇᴌᴀ, Graipher, Jamal

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    You're on the wrong site. We're not fixing bugs here.
    – t3chb0t
    yesterday










  • Actually my question title is miss leading ,i want to have a code review and need suggestions to improve the code structure specially in context of using DBContext with Unit (IOC). If anyone do me this favor, I will be really thankful
    – amir javaid
    yesterday








  • 1




    mhmm... to me this I also face a problem in getting latest data when data is updated in database and after refreshing window form it loads old data. is a bug, isn't it?
    – t3chb0t
    yesterday










  • As i already specifies that my question context is misleading to you ,but in the sample code this kind of issue is not exists,only you have to review my code ,your help will be highly valuable for me .thanks in advance
    – amir javaid
    yesterday






  • 4




    I reckon if you don't want this page to be closed, take the time to make your title and review requirements NOT misleading. (Edits are free)
    – mickmackusa
    yesterday
















-3














i am using unity ioc in c#,and DbContext with ContainerControlledLifetimeManager for application context with three layer architecture using entity framework. I have to ask either this pattern is better in window form application or is there any better suggestion . I also face a problem in getting latest data when data is updated in database and after refreshing window form it loads old data.



As I am also using transactions using entity framework and therefore I can not use TransientLifetimeManager for DbContext class. I need same context in different DA classes to perform multiple operation in single transaction. Can any one suggest me the better approach to tackle this kind of situation. Any help is appreciated.



Sample Project Example




//Generic Repository




public interface IRepository<ModelClass, EntityClass>
where ModelClass : class
where EntityClass : class
{
void Add(ModelClass entity);
void Delete(ModelClass entity);
void Update(ModelClass entity,int id);
bool Save();
}



//Generic Repository implementation




public class GenericRepository<ModelClass, EntityClass> : IRepository<ModelClass, EntityClass>
where ModelClass : class
where EntityClass : class
{
private ApplicationContext context = null;
private DbSet<EntityClass> Entity = null;

public GenericRepository(ApplicationContext context)
{
this.context = context;
Entity = context.Set<EntityClass>();
}
public void Add(ModelClass model)
{
EntityClass enityClass = AutoMapper.Mapper.Map<ModelClass, EntityClass>(model);
this.Entity.Add(enityClass);
}

public void Delete(ModelClass model)
{
EntityClass enityClass = AutoMapper.Mapper.Map<ModelClass, EntityClass>(model);
this.Entity.Remove(enityClass);
context.SaveChanges();
}

public void Update(ModelClass model, int id)
{
EntityClass dbEntity = Entity.Find(id);
EntityClass entityClass = AutoMapper.Mapper.Map<ModelClass, EntityClass>(model);
context.Entry(dbEntity).CurrentValues.SetValues(entityClass);
context.Entry(dbEntity).State = EntityState.Modified;

}

public bool Save()
{

bool status = false;
try
{
int result = context.SaveChanges();
if (result > 0)
{
return status = true;
}
else
return status;
}
catch (DbEntityValidationException dbEx)
{
throw;
}
return status;
}



// student dataaccess interface




 public interface IStudent : IRepository<Student, StudentEntity>
{
List<Student> GetStudentById(int studentId);
}


//implementation of student access interfaces

public class StudentAccess : GenericRepository<Student, StudentEntity>, IStudent
{

public StudentAccess(ApplicationContext context)
: base(context)
{
this.context = context;
}

Public void Add(student std)
{
base.Add(std);
base.save();
}

Public void Update(student std,int studentid)
{
base.update(std,studentId);
base.save();
}


public List<student> GetStudentById()
{

var data=context.student.ToList();
return data;

}
}



//student service layer




 public class StudentService 
{
ApplicationContext Context;
IStudent student;

public StudentService(ApplicationContext Context, IStudent student)
{
this.Context = Context;
this.student = student;
}

Public void SaveStudent(student std)
{
this.student.Add(std);
}

Public void UpdateStudent(student std,int studentId)
{
this.student.Update(std,studentId);
}
}



//implementation of unity container




 public class DependencyOfDependency : UnityContainerExtension
{
protected override void Initialize()
{

// Register Data access layer

Container.RegisterType<DbContext, LogisticsERP.DA.Entities.ApplicationContext>(new ContainerControlledLifetimeManager());
Container.RegisterType< IStudent,StudentAccess>();


//Register Service Layer Classes

Container.RegisterType<StudentService>();
}
}









share|improve this question







New contributor




amir javaid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by t3chb0t, Sᴀᴍ Onᴇᴌᴀ, Graipher, Jamal yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – t3chb0t, Sᴀᴍ Onᴇᴌᴀ, Graipher, Jamal

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    You're on the wrong site. We're not fixing bugs here.
    – t3chb0t
    yesterday










  • Actually my question title is miss leading ,i want to have a code review and need suggestions to improve the code structure specially in context of using DBContext with Unit (IOC). If anyone do me this favor, I will be really thankful
    – amir javaid
    yesterday








  • 1




    mhmm... to me this I also face a problem in getting latest data when data is updated in database and after refreshing window form it loads old data. is a bug, isn't it?
    – t3chb0t
    yesterday










  • As i already specifies that my question context is misleading to you ,but in the sample code this kind of issue is not exists,only you have to review my code ,your help will be highly valuable for me .thanks in advance
    – amir javaid
    yesterday






  • 4




    I reckon if you don't want this page to be closed, take the time to make your title and review requirements NOT misleading. (Edits are free)
    – mickmackusa
    yesterday














-3












-3








-3







i am using unity ioc in c#,and DbContext with ContainerControlledLifetimeManager for application context with three layer architecture using entity framework. I have to ask either this pattern is better in window form application or is there any better suggestion . I also face a problem in getting latest data when data is updated in database and after refreshing window form it loads old data.



As I am also using transactions using entity framework and therefore I can not use TransientLifetimeManager for DbContext class. I need same context in different DA classes to perform multiple operation in single transaction. Can any one suggest me the better approach to tackle this kind of situation. Any help is appreciated.



Sample Project Example




//Generic Repository




public interface IRepository<ModelClass, EntityClass>
where ModelClass : class
where EntityClass : class
{
void Add(ModelClass entity);
void Delete(ModelClass entity);
void Update(ModelClass entity,int id);
bool Save();
}



//Generic Repository implementation




public class GenericRepository<ModelClass, EntityClass> : IRepository<ModelClass, EntityClass>
where ModelClass : class
where EntityClass : class
{
private ApplicationContext context = null;
private DbSet<EntityClass> Entity = null;

public GenericRepository(ApplicationContext context)
{
this.context = context;
Entity = context.Set<EntityClass>();
}
public void Add(ModelClass model)
{
EntityClass enityClass = AutoMapper.Mapper.Map<ModelClass, EntityClass>(model);
this.Entity.Add(enityClass);
}

public void Delete(ModelClass model)
{
EntityClass enityClass = AutoMapper.Mapper.Map<ModelClass, EntityClass>(model);
this.Entity.Remove(enityClass);
context.SaveChanges();
}

public void Update(ModelClass model, int id)
{
EntityClass dbEntity = Entity.Find(id);
EntityClass entityClass = AutoMapper.Mapper.Map<ModelClass, EntityClass>(model);
context.Entry(dbEntity).CurrentValues.SetValues(entityClass);
context.Entry(dbEntity).State = EntityState.Modified;

}

public bool Save()
{

bool status = false;
try
{
int result = context.SaveChanges();
if (result > 0)
{
return status = true;
}
else
return status;
}
catch (DbEntityValidationException dbEx)
{
throw;
}
return status;
}



// student dataaccess interface




 public interface IStudent : IRepository<Student, StudentEntity>
{
List<Student> GetStudentById(int studentId);
}


//implementation of student access interfaces

public class StudentAccess : GenericRepository<Student, StudentEntity>, IStudent
{

public StudentAccess(ApplicationContext context)
: base(context)
{
this.context = context;
}

Public void Add(student std)
{
base.Add(std);
base.save();
}

Public void Update(student std,int studentid)
{
base.update(std,studentId);
base.save();
}


public List<student> GetStudentById()
{

var data=context.student.ToList();
return data;

}
}



//student service layer




 public class StudentService 
{
ApplicationContext Context;
IStudent student;

public StudentService(ApplicationContext Context, IStudent student)
{
this.Context = Context;
this.student = student;
}

Public void SaveStudent(student std)
{
this.student.Add(std);
}

Public void UpdateStudent(student std,int studentId)
{
this.student.Update(std,studentId);
}
}



//implementation of unity container




 public class DependencyOfDependency : UnityContainerExtension
{
protected override void Initialize()
{

// Register Data access layer

Container.RegisterType<DbContext, LogisticsERP.DA.Entities.ApplicationContext>(new ContainerControlledLifetimeManager());
Container.RegisterType< IStudent,StudentAccess>();


//Register Service Layer Classes

Container.RegisterType<StudentService>();
}
}









share|improve this question







New contributor




amir javaid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











i am using unity ioc in c#,and DbContext with ContainerControlledLifetimeManager for application context with three layer architecture using entity framework. I have to ask either this pattern is better in window form application or is there any better suggestion . I also face a problem in getting latest data when data is updated in database and after refreshing window form it loads old data.



As I am also using transactions using entity framework and therefore I can not use TransientLifetimeManager for DbContext class. I need same context in different DA classes to perform multiple operation in single transaction. Can any one suggest me the better approach to tackle this kind of situation. Any help is appreciated.



Sample Project Example




//Generic Repository




public interface IRepository<ModelClass, EntityClass>
where ModelClass : class
where EntityClass : class
{
void Add(ModelClass entity);
void Delete(ModelClass entity);
void Update(ModelClass entity,int id);
bool Save();
}



//Generic Repository implementation




public class GenericRepository<ModelClass, EntityClass> : IRepository<ModelClass, EntityClass>
where ModelClass : class
where EntityClass : class
{
private ApplicationContext context = null;
private DbSet<EntityClass> Entity = null;

public GenericRepository(ApplicationContext context)
{
this.context = context;
Entity = context.Set<EntityClass>();
}
public void Add(ModelClass model)
{
EntityClass enityClass = AutoMapper.Mapper.Map<ModelClass, EntityClass>(model);
this.Entity.Add(enityClass);
}

public void Delete(ModelClass model)
{
EntityClass enityClass = AutoMapper.Mapper.Map<ModelClass, EntityClass>(model);
this.Entity.Remove(enityClass);
context.SaveChanges();
}

public void Update(ModelClass model, int id)
{
EntityClass dbEntity = Entity.Find(id);
EntityClass entityClass = AutoMapper.Mapper.Map<ModelClass, EntityClass>(model);
context.Entry(dbEntity).CurrentValues.SetValues(entityClass);
context.Entry(dbEntity).State = EntityState.Modified;

}

public bool Save()
{

bool status = false;
try
{
int result = context.SaveChanges();
if (result > 0)
{
return status = true;
}
else
return status;
}
catch (DbEntityValidationException dbEx)
{
throw;
}
return status;
}



// student dataaccess interface




 public interface IStudent : IRepository<Student, StudentEntity>
{
List<Student> GetStudentById(int studentId);
}


//implementation of student access interfaces

public class StudentAccess : GenericRepository<Student, StudentEntity>, IStudent
{

public StudentAccess(ApplicationContext context)
: base(context)
{
this.context = context;
}

Public void Add(student std)
{
base.Add(std);
base.save();
}

Public void Update(student std,int studentid)
{
base.update(std,studentId);
base.save();
}


public List<student> GetStudentById()
{

var data=context.student.ToList();
return data;

}
}



//student service layer




 public class StudentService 
{
ApplicationContext Context;
IStudent student;

public StudentService(ApplicationContext Context, IStudent student)
{
this.Context = Context;
this.student = student;
}

Public void SaveStudent(student std)
{
this.student.Add(std);
}

Public void UpdateStudent(student std,int studentId)
{
this.student.Update(std,studentId);
}
}



//implementation of unity container




 public class DependencyOfDependency : UnityContainerExtension
{
protected override void Initialize()
{

// Register Data access layer

Container.RegisterType<DbContext, LogisticsERP.DA.Entities.ApplicationContext>(new ContainerControlledLifetimeManager());
Container.RegisterType< IStudent,StudentAccess>();


//Register Service Layer Classes

Container.RegisterType<StudentService>();
}
}






c# sql database entity-framework unity-container






share|improve this question







New contributor




amir javaid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




amir javaid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




amir javaid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









amir javaid

1




1




New contributor




amir javaid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





amir javaid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






amir javaid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as off-topic by t3chb0t, Sᴀᴍ Onᴇᴌᴀ, Graipher, Jamal yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – t3chb0t, Sᴀᴍ Onᴇᴌᴀ, Graipher, Jamal

If this question can be reworded to fit the rules in the help center, please edit the question.




put on hold as off-topic by t3chb0t, Sᴀᴍ Onᴇᴌᴀ, Graipher, Jamal yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – t3chb0t, Sᴀᴍ Onᴇᴌᴀ, Graipher, Jamal

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1




    You're on the wrong site. We're not fixing bugs here.
    – t3chb0t
    yesterday










  • Actually my question title is miss leading ,i want to have a code review and need suggestions to improve the code structure specially in context of using DBContext with Unit (IOC). If anyone do me this favor, I will be really thankful
    – amir javaid
    yesterday








  • 1




    mhmm... to me this I also face a problem in getting latest data when data is updated in database and after refreshing window form it loads old data. is a bug, isn't it?
    – t3chb0t
    yesterday










  • As i already specifies that my question context is misleading to you ,but in the sample code this kind of issue is not exists,only you have to review my code ,your help will be highly valuable for me .thanks in advance
    – amir javaid
    yesterday






  • 4




    I reckon if you don't want this page to be closed, take the time to make your title and review requirements NOT misleading. (Edits are free)
    – mickmackusa
    yesterday














  • 1




    You're on the wrong site. We're not fixing bugs here.
    – t3chb0t
    yesterday










  • Actually my question title is miss leading ,i want to have a code review and need suggestions to improve the code structure specially in context of using DBContext with Unit (IOC). If anyone do me this favor, I will be really thankful
    – amir javaid
    yesterday








  • 1




    mhmm... to me this I also face a problem in getting latest data when data is updated in database and after refreshing window form it loads old data. is a bug, isn't it?
    – t3chb0t
    yesterday










  • As i already specifies that my question context is misleading to you ,but in the sample code this kind of issue is not exists,only you have to review my code ,your help will be highly valuable for me .thanks in advance
    – amir javaid
    yesterday






  • 4




    I reckon if you don't want this page to be closed, take the time to make your title and review requirements NOT misleading. (Edits are free)
    – mickmackusa
    yesterday








1




1




You're on the wrong site. We're not fixing bugs here.
– t3chb0t
yesterday




You're on the wrong site. We're not fixing bugs here.
– t3chb0t
yesterday












Actually my question title is miss leading ,i want to have a code review and need suggestions to improve the code structure specially in context of using DBContext with Unit (IOC). If anyone do me this favor, I will be really thankful
– amir javaid
yesterday






Actually my question title is miss leading ,i want to have a code review and need suggestions to improve the code structure specially in context of using DBContext with Unit (IOC). If anyone do me this favor, I will be really thankful
– amir javaid
yesterday






1




1




mhmm... to me this I also face a problem in getting latest data when data is updated in database and after refreshing window form it loads old data. is a bug, isn't it?
– t3chb0t
yesterday




mhmm... to me this I also face a problem in getting latest data when data is updated in database and after refreshing window form it loads old data. is a bug, isn't it?
– t3chb0t
yesterday












As i already specifies that my question context is misleading to you ,but in the sample code this kind of issue is not exists,only you have to review my code ,your help will be highly valuable for me .thanks in advance
– amir javaid
yesterday




As i already specifies that my question context is misleading to you ,but in the sample code this kind of issue is not exists,only you have to review my code ,your help will be highly valuable for me .thanks in advance
– amir javaid
yesterday




4




4




I reckon if you don't want this page to be closed, take the time to make your title and review requirements NOT misleading. (Edits are free)
– mickmackusa
yesterday




I reckon if you don't want this page to be closed, take the time to make your title and review requirements NOT misleading. (Edits are free)
– mickmackusa
yesterday










0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

How to make a Squid Proxy server?

Is this a new Fibonacci Identity?

19世紀