//EntityADto GetById(Int32 entityAId, UserContextDto userContextDto); public EntityBDto GetById(Int32 entityBId) { // Variable de respuesta. EntityBDto entityDto = null; try { IEntityBRepository repo = this.unityContainer.Resolve <IEntityBRepository>(); // Obtener y comprobar la entidad. //ISpecification<EntityB> spec = new DirectSpecification<EntityB>(t => t.Id == entityBId); EntityB entity = repo.GetFilteredElements(t => t.Id == entityBId).Single(); string s = string.Format(Inflexion2.Resources.Framework.NoDataById, "Entity B", entityBId); if (s == null) { s = ""; } Guard.ArgumentIsNotNull(entity, s); // Mapeamos los datos. entityDto = this.EntityBMapper.EntityMapping(entity); // Confirmamos la transacción. this.Commit(); } catch (Exception ex) { // Escribimos en el Log. logger.Error(ex.Message, ex); throw ex; } // Devolvemos el resultado. return(entityDto); }
//Int32 Create(EntityADto entityADto, UserContextDto userContextDto);// invocacion con identificación de usuario public int Create(EntityBDto entityBDto) { #region Preconditions // Comprobar el DTO de entrada. Guard.ArgumentIsNotNull( entityBDto, string.Format( Inflexion2.Resources.Framework.DataTransferObjectIsNull, "Entity B")); //// usar un fichero de recursos para el dominio de negocio Company.Product.BoundedContext.Resources.Business.EntityBAlias // Comprobar los campos mandatory dentro del DTO. Guard.ArgumentNotNullOrEmpty( entityBDto.Name, string.Format( Inflexion2.Resources.Framework.PropertyRequired, "Name", "Entity B")); //// usar un fichero de recursos para el dominio de negocio Company.Product.BoundedContext.Resources.Business.EntityBNameAlias // el dto debe corresponder a un transient el id debe tener el valor por defecto //if (entityBDto.Id != default(int)) Guard.Against <ArgumentException>(entityBDto.Id != default(int), string.Format( Inflexion2.Resources.Framework.IsNotTransient, "Entity B")); //// usar un fichero de recursos para el dominio de negocio Company.Product.BoundedContext.Resources.Business.EntityBNameAlias #endregion EntityB entityB = EntityBFactory.Create(entityBDto.Name); IEntityBRepository repo = this.unityContainer.Resolve <IEntityBRepository>(); repo.Add(entityB); this.Commit(); return(entityB.Id); }
//IEnumerable<EntityADto> GetAll(UserContextDto userContextDto);// invocación con identificación de usuario public IEnumerable <EntityBDto> GetAll() { // Variable de respuesta. List <EntityBDto> result = new List <EntityBDto>(); try { IEntityBRepository repo = this.unityContainer.Resolve <IEntityBRepository>(); var entities = repo.GetAll(); // Mapeamos los datos. entities.ToList() .ForEach(entity => { var entityDto = this.EntityBMapper.EntityMapping(entity); result.Add(entityDto); }); // Confirmamos la transacción. this.Commit(); } catch (Exception ex) { // Escribir en el Log. //logger.Error(ex.Message, ex); throw ex; } return(result); }
//bool Update(EntityADto entity1Dto, UserContextDto userContextDto); public bool Update(EntityBDto entityBDto) { #region preconditions // Comprobar el DTO de entrada. Guard.ArgumentIsNotNull( entityBDto, string.Format( Inflexion2.Resources.Framework.DataTransferObjectIsNull, "Entity B")); // comprobamos los campos mandatory ¿EN UNA ACTUALIZACIÓN TAMBIEN? Guard.ArgumentNotNullOrEmpty( entityBDto.Name, string.Format( Inflexion2.Resources.Framework.PropertyRequired, "Entity B")); #endregion try { IEntityBRepository repo = this.unityContainer.Resolve <IEntityBRepository>(); // Obtener y comprobar la entidad. //ISpecification<EntityB> spec = new DirectSpecification<EntityB>(t => t.Id == entityBDto.Id); EntityB entity2Update = repo.GetFilteredElements(t => t.Id == entityBDto.Id).Single(); Guard.ArgumentIsNotNull( entity2Update, string.Format( Inflexion2.Resources.Framework.CanNotUpdateInexistenceEntity, "Entity B")); // Mapeamos los datos, teniendo encuenta que el id lo hemos recuperado y comprobado // ademas comprobamos que valores han sido modificados if (entity2Update.Name != entityBDto.Name) { entity2Update.Name = entityBDto.Name; // opcion a trazar las modificaciones } // igualmente hemos de mapear las entidades emparentadas. if (!entity2Update.CanBeSaved()) { return(false); } repo.Modify(entity2Update); // Confirmamos la transacción. this.Commit(); } catch (Exception ex) { // Escribimos en el Log. //logger.Error(ex.Message, ex); throw ex; } // Devolvemos el resultado. return(true); }
//PagedElements<Entity1Dto> GetPaged(SpecificationDto specificationDto, UserContextDto userContextDto); public PagedElements <EntityBDto> GetPaged(SpecificationDto specificationDto) { #region Preconditions // Comprobar el DTO de entrada. Guard.ArgumentIsNotNull( specificationDto, string.Format( Inflexion2.Resources.Framework.EspecificationDataTransferObjectIsNull, "Entity B")); //// usar un fichero de recursos para el dominio de negocio Company.Product.BoundedContext.Resources.Business.EntityBAlias #endregion List <EntityBDto> result = new List <EntityBDto>(0); int totalElements = 0; try { // Creamos el repositorio de la entidad. IEntityBRepository repo = this.unityContainer.Resolve <IEntityBRepository>(); // Obtenemos las entidades aplicando la especificación. ISpecification <EntityB> filter = specificationDto.ToSpecification <EntityB>(); PagedElements <EntityB> entities = repo.GetPagedElements( specificationDto.PageIndex, specificationDto.PageSize, filter.IsSatisfiedBy(), entity => entity.Id, true); totalElements = entities.TotalElements; // Mapeamos los datos. entities.ToList().ForEach(entity => { var entityDto = this.EntityBMapper.EntityMapping(entity); result.Add(entityDto); }); // Confirmamos la transacción. this.Commit(); } catch (Exception ex) { // Escribir en el Log. //logger.Error(ex.Message, ex); throw ex; } // Devolver el resultado. return(new PagedElements <EntityBDto>(result, totalElements)); }
//bool Delete(Int32 Entity1Id, UserContextDto userContextDto);// invocacion con identificación de usuario public bool Delete(Int32 EntityBId) { #region Preconditions // Comprobar el DTO de entrada. if (EntityBId == default(int)) { return(false); } #endregion IEntityBRepository repo = this.unityContainer.Resolve <IEntityBRepository>(); IEnumerable <EntityB> results = repo.GetFilteredElements(u => u.Id == EntityBId); EntityB entityB2Delete = results.First(); if (!entityB2Delete.CanBeDeleted()) { return(false); } repo.Remove(entityB2Delete); this.Commit(); return(true); }
public void starter() { this.unityContainer = new UnityContainer(); ServiceLocator.Initialize( (x, y) => this.unityContainer.RegisterType(x, y), (x, y) => this.unityContainer.RegisterInstance(x, y), (x) => { return(this.unityContainer.Resolve(x)); }, (x) => { return(this.unityContainer.ResolveAll(x)); }); // Context Factory connString = this.ConnectionString(); RootAggregateFrameworkUnitOfWorkFactory <BootstrapUnitOfWork> ctxFactory = new RootAggregateFrameworkUnitOfWorkFactory <BootstrapUnitOfWork>(connString); if (!ctxFactory.DatabaseExists()) { ctxFactory.CreateDatabase(); } ctxFactory.ValidateDatabaseSchema(); this.unityContainer.RegisterInstance <IDatabaseManager>(ctxFactory); this.unityContainer.RegisterType <DbContext, BootstrapUnitOfWork>(this.contextPerTestLifeTimeManager, new InjectionConstructor(connString)); this.unityContainer.RegisterType <IUnitOfWork, EntityFrameworkUnitOfWork>(this.unitOfWorkPerTestLifeTimeManager); // Repositories this.unityContainer.RegisterType <IEntityARepository, EntityARepository>(new PerResolveLifetimeManager()); this.unityContainer.RegisterType <IEntityBRepository, EntityBRepository>(new PerResolveLifetimeManager()); ApplicationContext.User = new CorePrincipal(new CoreIdentity("cmendible", "hexa.auth", "*****@*****.**"), new string[] { }); // comienzan las operaciones // añadimos una entidad // metodo sin factoria y con constructor publico //var entityA = new EntityA(); // entityA.Name = "Martin"; // metodo con factoria y constructor interno IEntityA entityA = EntityAFactory.Create("Martin"); entityA.CanBeDeleted(); IEntityARepository repo = this.unityContainer.Resolve <IEntityARepository>(); repo.Add((EntityA)entityA); this.Commit(); entityA.CanBeDeleted(); // añadimos 2 entidades y su relación var a = EntityAFactory.Create("Willi"); var b = EntityBFactory.Create("matematicas"); a.AddB(b); IEntityARepository repoA = this.unityContainer.Resolve <IEntityARepository>(); IEntityBRepository repoB = this.unityContainer.Resolve <IEntityBRepository>(); repoB.Add((EntityB)b); repoA.Add((EntityA)a); this.Commit(); //añadimos una entidad c dependiente de entidad a la primera entidad a EntityA padre = repoA.GetFilteredElements(e => e.Id == 1).FirstOrDefault(); EntityC hijo = EntityCFactory.Create(padre, "hijo"); this.Commit(); padre = repoA.GetFilteredElements(e => e.Id == 1).FirstOrDefault(); padre.CanBeDeleted(); }