Share via


Asp.Net MVC Solution Architecture project with Async Repository pattern by Service having Dependency Injection by Unity - Page 2

The entire Solution structure is shown in the snapshots below

Infrastructure

Domain


**
**

Presentation

Using the 'Entity Framework Power Tools Beta 4 (Visual Studios Extension)' we auto-generate the entity classes, mapper classes and the database context class, namely 'StarDesc.cs', 'StarDescMap.cs' and 'StarSystemContext.cs'.

StarDesc.cs

namespace CG.StarSystem.Data.Models
{
    public partial class StarDesc
    {
        public int Id { get; set; }
        public string StarName { get; set; }
        public string StarSize { get; set; }
        public string StarDistanceFromSun { get; set; }
        public string StarGalaxyName { get; set; }
        public string StarBrightness { get; set; }
        public string SpectralType { get; set; }
    }
}

StarDescMap.cs

using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
 
namespace CG.StarSystem.Data.Models.Mapping
{
    public class StarDescMap : EntityTypeConfiguration<StarDesc>
    {
        public StarDescMap()
        {
            // Primary Key
            this.HasKey(t => t.Id);
 
            // Properties
            this.Property(t => t.Id)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
 
            this.Property(t => t.StarName)
                .HasMaxLength(150);
 
            this.Property(t => t.StarSize)
                .HasMaxLength(50);
 
            this.Property(t => t.StarDistanceFromSun)
                .HasMaxLength(50);
 
            this.Property(t => t.StarGalaxyName)
                .HasMaxLength(50);
 
            this.Property(t => t.StarBrightness)
                .HasMaxLength(50);
 
            this.Property(t => t.SpectralType)
                .HasMaxLength(50);
 
            // Table & Column Mappings
            this.ToTable("StarDesc");
            this.Property(t => t.Id).HasColumnName("Id");
            this.Property(t => t.StarName).HasColumnName("StarName");
            this.Property(t => t.StarSize).HasColumnName("StarSize");
            this.Property(t => t.StarDistanceFromSun).HasColumnName("StarDistanceFromSun");
            this.Property(t => t.StarGalaxyName).HasColumnName("StarGalaxyName");
            this.Property(t => t.StarBrightness).HasColumnName("StarBrightness");
            this.Property(t => t.SpectralType).HasColumnName("SpectralType");
        }
    }
}

StarSystemContext.cs

using System.Data.Entity;
using CG.StarSystem.Data.Models.Mapping;
 
namespace CG.StarSystem.Data.Models
{
    public partial class StarSystemContext : DbContext
    {
        static StarSystemContext()
        {
            Database.SetInitializer<StarSystemContext>(null);
        }
 
        public StarSystemContext()
            : base("Name=StarSystemContext")
        {
            // the terrible hack
            var ensureDLLIsCopied =
                    System.Data.Entity.SqlServer.SqlProviderServices.Instance;
        }
 
        public DbSet<SpectralClassesSubType> SpectralClassesSubTypes { get; set; }
        public DbSet<StarDesc> StarDescs { get; set; }
        public DbSet<StarTypeByLuminosityClass> StarTypeByLuminosityClasses { get; set; }
        public DbSet<StarTypeBySpectralClass> StarTypeBySpectralClasses { get; set; }
 
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new SpectralClassesSubTypeMap());
            modelBuilder.Configurations.Add(new StarDescMap());
            modelBuilder.Configurations.Add(new StarTypeByLuminosityClassMap());
            modelBuilder.Configurations.Add(new StarTypeBySpectralClassMap());
        }
    }
}

IStarDescRepository.cs

using CG.StarSystem.Data.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
 
namespace CG.StarSystem.Repository.StarDescs
{
     
    public interface IStarDescRepository: IDisposable
    {
        Task<List<StarDesc>> GetStarDescsAsync();
        Task<StarDesc> GetStarDescByIdAsync(int? id);
        Task CreateStarAsync(StarDesc stardesc);
        Task DeleteStarAsync(int? id);
        Task EditStarDescAsync(StarDesc stardesc);
    }
}

StarDescRepository.cs

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CG.StarSystem.Data.Models;
using System.Data.Entity;
 
namespace CG.StarSystem.Repository.StarDescs
{
    public class StarDescRepository : IStarDescRepository, IDisposable
    {
 
        private StarSystemContext _context;
 
        /// <summary>
        ///     Create a new instance of <see cref="StarDescRepository" />.
        /// </summary>
        /// <param name="transaction">Active transaction</param>
        /// <exception cref="ArgumentNullException">transaction</exception>
        public StarDescRepository()
        {
            _context = new StarSystemContext();
 
        }
 
         
        public async Task<List<StarDesc>> GetStarDescsAsync()
        {
            return await _context.StarDescs.ToListAsync();
        }
 
        public async Task<StarDesc> GetStarDescByIdAsync(int? id)
        {
            return await _context.StarDescs.FindAsync(id);
        }
 
        public  async Task CreateStarAsync(StarDesc stardesc)
        {
             _context.StarDescs.Add(stardesc);
            await _context.SaveChangesAsync(); 
        }
 
        public async Task EditStarDescAsync(StarDesc stardesc)
        {
            _context.Entry(stardesc).State = EntityState.Modified;
            await _context.SaveChangesAsync();
        }
 
        public async Task DeleteStarAsync(int? id)
        {
            StarDesc stardesc = await _context.StarDescs.FindAsync(id);
            _context.StarDescs.Remove(stardesc);
            await _context.SaveChangesAsync();
        }
            
         
        
 
        #region IDisposable Support
        private bool disposedValue = false; // To detect redundant calls
 
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                     
                    _context.Dispose();
                }
 
                disposedValue = true;
            }
        }
 
        // This code added to correctly implement the disposable pattern.
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
 
 
        #endregion
    }
}

IStarDescService.cs

using CG.StarSystem.Data.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
 
namespace CG.StarSystem.ApplicationServices
{
    public interface IStarDescService : IDisposable
    {
        Task<List<StarDesc>> GetAllStarsAsync();
        Task<StarDesc> GetStarDescriptionByIdAsync(int? id);
        Task AddStarAsync(StarDesc stardesc);
        Task DeleteStarAsync(int? id);
        Task EditStarDescAsync(StarDesc stardesc);
 
    }
}

StarDescService.cs

using CG.StarSystem.Data.Models;
using CG.StarSystem.Repository.StarDescs;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
 
namespace CG.StarSystem.ApplicationServices
{
    public class StarDescService : IStarDescService, IDisposable
    {
        private readonly IStarDescRepository _StarDescRepository;
 
        //public StarDescService(IStarDescRepository IStarDescRepository) {
        //    _IStarDescRepository = IStarDescRepository;
        //}
        public StarDescService()
        {
            _StarDescRepository =  new StarDescRepository();
            
        }
 
        public async Task<List<StarDesc>> GetAllStarsAsync()
        {
 
            return await _StarDescRepository.GetStarDescsAsync();
        }
 
        public async Task<StarDesc> GetStarDescriptionByIdAsync(int? id)
        {
            
            return await _StarDescRepository.GetStarDescByIdAsync(id);
        }
 
        public async Task AddStarAsync(StarDesc stardesc)
        {
 
            await _StarDescRepository.CreateStarAsync(stardesc);
        }
 
        public async Task DeleteStarAsync(int? id)
        {
 
            await _StarDescRepository.DeleteStarAsync(id);
        }
 
        public async Task EditStarDescAsync(StarDesc stardesc)
        {
 
             await _StarDescRepository.EditStarDescAsync(stardesc);
        }
 
        
 
 
        #region IDisposable Support
        private bool disposedValue = false; // To detect redundant calls
 
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    _StarDescRepository.Dispose();
                }
 
                disposedValue = true;
            }
        }
 
        // This code added to correctly implement the disposable pattern.
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion
 
 
    }
}

Return to Top