Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
CleanArchitecture
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
CleanArchitecture
Commits
61436fa4
Commit
61436fa4
authored
Oct 18, 2016
by
Steve Smith
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleaning up EF code and writing some more tests.
parent
0d6a317e
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
132 additions
and
56 deletions
+132
-56
BaseDomainEvent.cs
src/CleanArchitecture.Core/Model/BaseDomainEvent.cs
+1
-1
BaseEntity.cs
src/CleanArchitecture.Core/Model/BaseEntity.cs
+1
-0
AppDbContext.cs
src/CleanArchitecture.Infrastructure/Data/AppDbContext.cs
+1
-52
EfRepository.cs
src/CleanArchitecture.Infrastructure/Data/EfRepository.cs
+59
-0
Startup.cs
src/CleanArchitecture.Web/Startup.cs
+7
-2
EfRepositoryShould.cs
...Architecture.Tests/Integration/Data/EfRepositoryShould.cs
+54
-0
project.json
tests/CleanArchitecture.Tests/project.json
+9
-1
No files found.
src/CleanArchitecture.Core/Model/BaseDomainEvent.cs
View file @
61436fa4
...
@@ -4,6 +4,6 @@ namespace CleanArchitecture.Core.Model
...
@@ -4,6 +4,6 @@ namespace CleanArchitecture.Core.Model
{
{
public
abstract
class
BaseDomainEvent
public
abstract
class
BaseDomainEvent
{
{
public
DateTime
DateOccurred
{
get
;
protected
set
;
}
=
DateTime
.
Now
;
public
DateTime
DateOccurred
{
get
;
protected
set
;
}
=
DateTime
.
Utc
Now
;
}
}
}
}
\ No newline at end of file
src/CleanArchitecture.Core/Model/BaseEntity.cs
View file @
61436fa4
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
namespace
CleanArchitecture.Core.Model
namespace
CleanArchitecture.Core.Model
{
{
// This can be modified to BaseEntity<TId> to support multiple key types (e.g. Guid)
public
abstract
class
BaseEntity
public
abstract
class
BaseEntity
{
{
public
int
Id
{
get
;
set
;
}
public
int
Id
{
get
;
set
;
}
...
...
src/CleanArchitecture.Infrastructure/Data/AppDbContext.cs
View file @
61436fa4
using
System.Collections.Generic
;
using
CleanArchitecture.Core.Interfaces
;
using
CleanArchitecture.Core.Interfaces
;
using
CleanArchitecture.Core.Model
;
using
CleanArchitecture.Core.Model
;
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.EntityFrameworkCore
;
using
System.Linq
;
using
System.Linq
;
...
@@ -37,54 +36,4 @@ namespace CleanArchitecture.Infrastructure.Data
...
@@ -37,54 +36,4 @@ namespace CleanArchitecture.Infrastructure.Data
return
base
.
SaveChanges
();
return
base
.
SaveChanges
();
}
}
}
}
public
class
EfRepository
<
T
>
:
IRepository
<
T
>
where
T
:
BaseEntity
{
private
readonly
AppDbContext
_dbContext
;
public
EfRepository
(
AppDbContext
dbContext
)
{
_dbContext
=
dbContext
;
}
public
T
GetById
(
int
id
)
{
return
_dbContext
.
Set
<
T
>().
FirstOrDefault
(
e
=>
e
.
Id
==
id
);
}
public
List
<
T
>
List
()
{
return
_dbContext
.
Set
<
T
>().
ToList
();
}
public
T
Add
(
T
entity
)
{
if
(
entity
.
Id
==
0
)
{
int
newId
=
1
;
var
entities
=
List
();
if
(
entities
.
Any
())
{
newId
=
entities
.
Max
(
z
=>
z
.
Id
)
+
1
;
}
entity
.
Id
=
newId
;
}
_dbContext
.
Set
<
T
>().
Add
(
entity
);
_dbContext
.
SaveChanges
();
return
entity
;
}
public
void
Delete
(
T
entity
)
{
_dbContext
.
Set
<
T
>().
Remove
(
entity
);
_dbContext
.
SaveChanges
();
}
public
void
Update
(
T
entity
)
{
_dbContext
.
Entry
(
entity
).
State
=
EntityState
.
Modified
;
_dbContext
.
SaveChanges
();
}
}
}
}
\ No newline at end of file
src/CleanArchitecture.Infrastructure/Data/EfRepository.cs
0 → 100644
View file @
61436fa4
using
System.Collections.Generic
;
using
System.Linq
;
using
CleanArchitecture.Core.Interfaces
;
using
CleanArchitecture.Core.Model
;
using
Microsoft.EntityFrameworkCore
;
namespace
CleanArchitecture.Infrastructure.Data
{
public
class
EfRepository
<
T
>
:
IRepository
<
T
>
where
T
:
BaseEntity
{
private
readonly
AppDbContext
_dbContext
;
public
EfRepository
(
AppDbContext
dbContext
)
{
_dbContext
=
dbContext
;
}
public
T
GetById
(
int
id
)
{
return
_dbContext
.
Set
<
T
>().
SingleOrDefault
(
e
=>
e
.
Id
==
id
);
}
public
List
<
T
>
List
()
{
return
_dbContext
.
Set
<
T
>().
ToList
();
}
public
T
Add
(
T
entity
)
{
// if using in memory EF, need to support IDENTITY keys
//if (entity.Id == 0)
//{
// int newId = 1;
// var entities = List();
// if (entities.Any())
// {
// newId = entities.Max(z => z.Id) + 1;
// }
// entity.Id = newId;
//}
_dbContext
.
Set
<
T
>().
Add
(
entity
);
_dbContext
.
SaveChanges
();
return
entity
;
}
public
void
Delete
(
T
entity
)
{
_dbContext
.
Set
<
T
>().
Remove
(
entity
);
_dbContext
.
SaveChanges
();
}
public
void
Update
(
T
entity
)
{
_dbContext
.
Entry
(
entity
).
State
=
EntityState
.
Modified
;
_dbContext
.
SaveChanges
();
}
}
}
\ No newline at end of file
src/CleanArchitecture.Web/Startup.cs
View file @
61436fa4
using
CleanArchitecture.Infrastructure.Data
;
using
CleanArchitecture.Core.Interfaces
;
using
CleanArchitecture.Infrastructure
;
using
CleanArchitecture.Infrastructure.Data
;
using
CleanArchitecture.Infrastructure.DomainEvents
;
using
Microsoft.AspNetCore.Builder
;
using
Microsoft.AspNetCore.Builder
;
using
Microsoft.AspNetCore.Hosting
;
using
Microsoft.AspNetCore.Hosting
;
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.EntityFrameworkCore
;
...
@@ -32,6 +35,8 @@ namespace CleanArchitecture.Web
...
@@ -32,6 +35,8 @@ namespace CleanArchitecture.Web
options
.
UseSqlServer
(
Configuration
.
GetConnectionString
(
"DefaultConnection"
)));
options
.
UseSqlServer
(
Configuration
.
GetConnectionString
(
"DefaultConnection"
)));
services
.
AddMvc
();
services
.
AddMvc
();
services
.
AddTransient
<
IDomainEventDispatcher
,
DomainEventDispatcher
>();
}
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
...
...
tests/CleanArchitecture.Tests/Integration/Data/EfRepositoryShould.cs
0 → 100644
View file @
61436fa4
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.Extensions.DependencyInjection
;
using
Xunit
;
using
CleanArchitecture.Infrastructure.Data
;
using
CleanArchitecture.Core.Model
;
using
System.Linq
;
using
CleanArchitecture.Core.Events
;
using
CleanArchitecture.Core.Interfaces
;
using
Moq
;
namespace
CleanArchitecture.Tests.Integration.Data
{
public
class
EfRepositoryAddShould
{
private
static
DbContextOptions
<
AppDbContext
>
CreateNewContextOptions
()
{
// Create a fresh service provider, and therefore a fresh
// InMemory database instance.
var
serviceProvider
=
new
ServiceCollection
()
.
AddEntityFrameworkInMemoryDatabase
()
.
BuildServiceProvider
();
// Create a new options instance telling the context to use an
// InMemory database and the new service provider.
var
builder
=
new
DbContextOptionsBuilder
<
AppDbContext
>();
builder
.
UseInMemoryDatabase
()
.
UseInternalServiceProvider
(
serviceProvider
);
return
builder
.
Options
;
}
[
Fact
]
public
void
AddItemAndSetId
()
{
var
repository
=
GetRepository
();
var
item
=
new
ToDoItem
();
repository
.
Add
(
item
);
var
newItem
=
repository
.
List
().
FirstOrDefault
();
Assert
.
Equal
(
item
,
newItem
);
Assert
.
True
(
newItem
.
Id
>
0
);
}
private
EfRepository
<
ToDoItem
>
GetRepository
()
{
var
options
=
CreateNewContextOptions
();
var
mockDispatcher
=
new
Mock
<
IDomainEventDispatcher
>();
return
new
EfRepository
<
ToDoItem
>(
new
AppDbContext
(
options
,
mockDispatcher
.
Object
));
}
}
}
\ No newline at end of file
tests/CleanArchitecture.Tests/project.json
View file @
61436fa4
...
@@ -3,13 +3,21 @@
...
@@ -3,13 +3,21 @@
"testRunner"
:
"xunit"
,
"testRunner"
:
"xunit"
,
"dependencies"
:
{
"dependencies"
:
{
"CleanArchitecture.Core"
:
"1.0.0-*"
,
"CleanArchitecture.Core"
:
"1.0.0-*"
,
"CleanArchitecture.Infrastructure"
:
"1.0.0-*"
,
"NETStandard.Library"
:
"1.6.0"
,
"NETStandard.Library"
:
"1.6.0"
,
"xunit"
:
"2.2.0-beta2-build3300"
,
"xunit"
:
"2.2.0-beta2-build3300"
,
"dotnet-test-xunit"
:
"2.2.0-preview2-build1029"
"dotnet-test-xunit"
:
"2.2.0-preview2-build1029"
,
"Microsoft.EntityFrameworkCore"
:
"1.0.0"
,
"Microsoft.EntityFrameworkCore.InMemory"
:
"1.0.0"
,
"System.Diagnostics.TraceSource"
:
"4.0.0"
,
"Moq"
:
"4.6.25-alpha"
},
},
"frameworks"
:
{
"frameworks"
:
{
"netcoreapp1.0"
:
{
"netcoreapp1.0"
:
{
"imports"
:
[
"dotnet5.6"
],
"dependencies"
:
{
"dependencies"
:
{
"Microsoft.NETCore.App"
:
{
"Microsoft.NETCore.App"
:
{
"type"
:
"platform"
,
"type"
:
"platform"
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment