Interfaces

This commit is contained in:
2017-03-08 15:47:46 +02:00
parent 9ede003205
commit fc14b4d3a5
20 changed files with 339 additions and 22 deletions

17
DAL/App.config Normal file
View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>

View File

@@ -31,6 +31,9 @@ namespace DAL
}
public DbSet<Person> People { get; set; }
public DbSet<Cource> Cources { get; set; }
public DbSet<Person> Persons { get; set; }
public DbSet<PersonCources> PersonCources { get; set; }
}
}

View File

@@ -30,10 +30,16 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="EntityFramework">
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
@@ -45,13 +51,23 @@
<ItemGroup>
<Compile Include="AppDbContext.cs" />
<Compile Include="Helpers\DbInit.cs" />
<Compile Include="Repositories\PersonRepository.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Repositories\EFRepository.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Domain\Domain.csproj">
<Project>{C3E21315-C897-46BE-B9F7-8B6A2201C6D5}</Project>
<Name>Domain</Name>
</ProjectReference>
<ProjectReference Include="..\Interfaces\Interfaces.csproj">
<Project>{88F63C3D-8C2B-4C31-A2C0-51DC04D1F18B}</Project>
<Name>Interfaces</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@@ -1,5 +1,6 @@
using System.Data.Entity;
using Domain;
using Domain.Enums;
namespace DAL.Helpers
{
@@ -9,16 +10,33 @@ namespace DAL.Helpers
{
// base.Seed doesn't really do anything
base.Seed(context);
//context.People.Add(new Person()
//{
// FirstName = "Arti",
// LastName = "Zirk"
//});
//context.People.Add(new Person()
//{
// FirstName = "Sala",
// LastName = "Kala"
//});
var p1 = new Person()
{
FirstName = "Arti",
LastName = "Zirk",
PersonType = PersonType.Student
};
var p2 = new Person()
{
FirstName = "Sala",
LastName = "Kala",
PersonType = PersonType.Teacher
};
var sub1 = new Subject()
{
SubjectName = "Best Cource"
};
var sub2 = new Subject()
{
SubjectName = "Worst Cource"
};
var cource = new Cource()
{
};
}
}
}

View File

@@ -0,0 +1,74 @@
using Interfaces;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL.Repositories
{
public class EFRepository<TEntity> : IRespository<TEntity>
where TEntity : class
{
protected DbContext RepositoryDbContext;
protected DbSet<TEntity> RepositoryDbSet;
public EFRepository(DbContext dbContext)
{
if (dbContext == null)
{
throw new ArgumentNullException(nameof(dbContext));
}
RepositoryDbContext = dbContext;
RepositoryDbSet = dbContext.Set<TEntity>();
if (RepositoryDbSet == null)
{
throw new NullReferenceException(nameof(RepositoryDbSet));
}
}
public List<TEntity> All
{
get
{
var resCount = RepositoryDbSet.Count();
if (resCount < 10)
{
return RepositoryDbSet.ToList();
}
throw new Exception("To many records in result set. Custom data access method needed");
}
}
public TEntity Find(int id)
{
RepositoryDbSet.Find(id);
}
public void Remove(int id)
{
Remove(Find(id));
}
public void Remove(TEntity entity)
{
RepositoryDbSet.Remove(entity);
}
public TEntity Add(TEntity entity)
{
return RepositoryDbSet.Add(entity);
}
public void Update(TEntity entity)
{
RepositoryDbSet.Attach(entity);
}
public int SaveChanges()
{
return RepositoryDbContext.SaveChanges();
}
}
}

View File

@@ -0,0 +1,14 @@
using System.Data.Entity;
using Domain;
using Interfaces;
namespace DAL.Repositories
{
public class PersonRepository : EFRepository<Person>, IPersonRepository
{
public PersonRepository(DbContext dbContext) : base(dbContext)
{
}
}
}

4
DAL/packages.config Normal file
View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.3" targetFramework="net452" />
</packages>