使用ContainerControlledLifetimeManager

时间:2019-01-02 15:51:54

标签: sql c#-4.0 dependency-injection entity-framework-6 unity-container

我正在C#中使用Unity IoC,并使用ContainerControlledLifetimeManager与DbContext一起使用具有实体框架的三层体系结构的应用程序上下文。我必须问这种模式在Windows Forms应用程序中更好还是有更好的建议。在数据库中更新数据时以及在刷新窗口窗体后加载旧数据时,我也遇到了获取最新数据的问题。

由于我也在使用使用实体框架的事务,因此不能将TransientLifetimeManager用于DbContext类。我需要在不同的DA类中使用相同的上下文,以在单个事务中执行多个操作。有没有更好的方法来解决这种情况?

这是演示的项目链接:

Sample Project

通用存储库

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();
    }

通用存储库实现

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;
        }

学生数据访问界面

 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;
        }
    }

学生服务层

 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);
        }
      }

实施统一容器

 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>();
         }
    }

0 个答案:

没有答案