Skip to content

Commit

Permalink
Adding some more skeleton infra:
Browse files Browse the repository at this point in the history
 - DataStore abstraction for stores
 - EntityConfiguration, a configuration model and EntityContext factory.
 - EntityServices, a static default IServiceProvider factory, matching the current K standard pattern
 - ConsoleLoggerFactory, an impl. of ILoggerFactory
  • Loading branch information
anpete committed Feb 5, 2014
1 parent 3a1b8cb commit 6d12f1e
Show file tree
Hide file tree
Showing 31 changed files with 425 additions and 17 deletions.
13 changes: 13 additions & 0 deletions Data.sln
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Data.Entity.UnitT
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Data.Entity.FunctionalTest.net45", "test\Microsoft.Data.Entity.FunctionalTest\Microsoft.Data.Entity.FunctionalTest.net45.csproj", "{CA431AD7-2D95-402B-AC17-214994E81943}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Data.SqlServer.UnitTest.net45", "test\Microsoft.Data.SqlServer.UnitTest\Microsoft.Data.SqlServer.UnitTest.net45.csproj", "{3159FABC-0C89-41FF-A279-48665CC068DC}"

This comment has been minimized.

Copy link
@anpete

anpete Feb 5, 2014

Author Contributor

I need to re-add the proj file - It was ignored by default, which I constantly forget :-)

EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -167,6 +169,16 @@ Global
{CA431AD7-2D95-402B-AC17-214994E81943}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{CA431AD7-2D95-402B-AC17-214994E81943}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{CA431AD7-2D95-402B-AC17-214994E81943}.Release|x86.ActiveCfg = Release|Any CPU
{3159FABC-0C89-41FF-A279-48665CC068DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3159FABC-0C89-41FF-A279-48665CC068DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3159FABC-0C89-41FF-A279-48665CC068DC}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{3159FABC-0C89-41FF-A279-48665CC068DC}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{3159FABC-0C89-41FF-A279-48665CC068DC}.Debug|x86.ActiveCfg = Debug|Any CPU
{3159FABC-0C89-41FF-A279-48665CC068DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3159FABC-0C89-41FF-A279-48665CC068DC}.Release|Any CPU.Build.0 = Release|Any CPU
{3159FABC-0C89-41FF-A279-48665CC068DC}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{3159FABC-0C89-41FF-A279-48665CC068DC}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{3159FABC-0C89-41FF-A279-48665CC068DC}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -187,5 +199,6 @@ Global
{04E6620B-5B41-45FE-981A-F40EB7686B0E} = {4427BB69-D9D9-47D3-B6BB-59578B1FCF24}
{EF361118-7D0D-453E-ADA4-2F24FBEE196C} = {13FEFC36-1C4B-47FC-9086-8D922285D8C4}
{CA431AD7-2D95-402B-AC17-214994E81943} = {13FEFC36-1C4B-47FC-9086-8D922285D8C4}
{3159FABC-0C89-41FF-A279-48665CC068DC} = {13FEFC36-1C4B-47FC-9086-8D922285D8C4}
EndGlobalSection
EndGlobal
8 changes: 8 additions & 0 deletions src/Microsoft.Data.Entity/DataStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace Microsoft.Data.Entity
{
public abstract class DataStore
{
}
}
57 changes: 57 additions & 0 deletions src/Microsoft.Data.Entity/EntityConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace Microsoft.Data.Entity
{
using System;
using JetBrains.Annotations;
using Microsoft.AspNet.DependencyInjection;
using Microsoft.Data.Entity.Resources;
using Microsoft.Data.Entity.Utilities;

public class EntityConfiguration
{
private readonly IServiceProvider _serviceProvider;

private DataStore _dataStore;

public EntityConfiguration()
: this(EntityServices.CreateDefaultProvider())
{
}

public EntityConfiguration([NotNull] IServiceProvider serviceProvider)
{
Check.NotNull(serviceProvider, "serviceProvider");

_serviceProvider = serviceProvider;
}

public virtual DataStore DataStore
{
get
{
return _dataStore
?? _serviceProvider.GetService<DataStore>()
?? ThrowNotConfigured<DataStore>();
}
[param: NotNull]
set
{
Check.NotNull(value, "value");

_dataStore = value;
}
}

public virtual EntityContext CreateContext()
{
return new EntityContext(this);
}

private static T ThrowNotConfigured<T>(string propertyName = null)
{
throw new InvalidOperationException(
Strings.MissingConfigurationItem(propertyName ?? typeof(T).Name));
}
}
}
10 changes: 5 additions & 5 deletions src/Microsoft.Data.Entity/EntityContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ namespace Microsoft.Data.Entity

public class EntityContext : IDisposable
{
private readonly Database _database = new Database();
private readonly EntityConfiguration _entityConfiguration;

public EntityContext([NotNull] string nameOrConnectionString)
public EntityContext([NotNull] EntityConfiguration entityConfiguration)
{
Check.NotEmpty(nameOrConnectionString, "nameOrConnectionString");
Check.NotNull(entityConfiguration, "entityConfiguration");

// TODO
_entityConfiguration = entityConfiguration;
}

public virtual int SaveChanges()
Expand All @@ -43,7 +43,7 @@ public void Dispose()

public virtual Database Database
{
get { return _database; }
get { return new Database(); }
}
}
}
32 changes: 32 additions & 0 deletions src/Microsoft.Data.Entity/EntityServices.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace Microsoft.Data.Entity
{
using System;
using JetBrains.Annotations;
using Microsoft.AspNet.DependencyInjection;
using Microsoft.AspNet.Logging;
using Microsoft.Data.Entity.Services;
using Microsoft.Data.Entity.Utilities;

public static class EntityServices
{
public static ServiceProvider CreateDefaultProvider()
{
var serviceProvider = new ServiceProvider();

AddDefaultServices(
(serviceType, implementationType)
=> serviceProvider.Add(serviceType, implementationType));

return serviceProvider;
}

public static void AddDefaultServices([NotNull] Action<Type, Type> serviceRegistrar)
{
Check.NotNull(serviceRegistrar, "serviceRegistrar");

serviceRegistrar(typeof(ILoggerFactory), typeof(ConsoleLoggerFactory));
}
}
}
1 change: 1 addition & 0 deletions src/Microsoft.Data.Entity/K10.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#if K10

// ReSharper disable CheckNamespace
namespace System.Collections.Immutable
{
using System.Collections.Generic;
Expand Down
2 changes: 2 additions & 0 deletions src/Microsoft.Data.Entity/Properties/InternalsVisibleTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#if !INTERNALS_INVISIBLE

[assembly: InternalsVisibleTo("Microsoft.Data.Entity.UnitTest")]
[assembly: InternalsVisibleTo("Microsoft.Data.Relational")]
[assembly: InternalsVisibleTo("Microsoft.Data.SqlServer")]

// for Moq

Expand Down
8 changes: 8 additions & 0 deletions src/Microsoft.Data.Entity/Resources/Resources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ internal static string InvalidPropertyExpression(object p0)
return string.Format(CultureInfo.CurrentCulture, GetString("InvalidPropertyExpression"), p0);
}

/// <summary>
/// "The EntityConfiguration property '{0}' has not been set."
/// </summary>
internal static string MissingConfigurationItem(object p0)
{
return string.Format(CultureInfo.CurrentCulture, GetString("MissingConfigurationItem"), p0);
}

private static string GetString(string name)
{
return _resourceManager.GetString(name)
Expand Down
3 changes: 3 additions & 0 deletions src/Microsoft.Data.Entity/Resources/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,7 @@
<data name="InvalidPropertyExpression" xml:space="preserve">
<value>The expression '{0}' is not a valid property expression. The expression should represent a property access: 't =&gt; t.MyProperty'.</value>
</data>
<data name="MissingConfigurationItem" xml:space="preserve">
<value>The EntityConfiguration property '{0}' has not been set.</value>
</data>
</root>
43 changes: 43 additions & 0 deletions src/Microsoft.Data.Entity/Services/ConsoleLoggerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace Microsoft.Data.Entity.Services
{
using System;
using System.Collections.Concurrent;
using JetBrains.Annotations;
using Microsoft.AspNet.Logging;
using Microsoft.Data.Entity.Utilities;

public class ConsoleLoggerFactory : ILoggerFactory
{

This comment has been minimized.

Copy link
@Eilon

Eilon Feb 6, 2014

Member

Would be interesting to tie this into the general tracing discussions that at some point need to happen. I don't think anyone has even started to look at it yet because the general tracing feature is not for alpha. I assume that in the meantime it's useful to have something in the Data stack just for debugging purposes?

This comment has been minimized.

Copy link
@davidfowl

davidfowl Feb 6, 2014

Member

The console logging would also probably be part of the logging assembly. It's pretty fundamental.

This comment has been minimized.

Copy link
@anpete

anpete Feb 6, 2014

Author Contributor

@Eilon, Yes, we use this to view log output such as generated SQL from test runners etc.
@davidfowl, Want me to send a PR to Logging?

This comment has been minimized.

Copy link
@Eilon

Eilon Feb 6, 2014

Member

Yeah I think this might be good to put in the Logging repo, even if it's not fully designed for the breadth of scenarios that we need. I think even just having a placeholder would be good - and that way when the next person who needs logging looks for something, they'll have something to poke at.

This comment has been minimized.

Copy link
@anpete

anpete Feb 6, 2014

Author Contributor

@Eilon Will do.

private readonly ConcurrentDictionary<string, ConsoleLogger> _loggers
= new ConcurrentDictionary<string, ConsoleLogger>(StringComparer.OrdinalIgnoreCase);

public virtual ILogger Create([NotNull] string name)
{
Check.NotEmpty(name, "name");

return _loggers.GetOrAdd(name, new ConsoleLogger(name));
}

private class ConsoleLogger : ILogger
{
private readonly string _name;

public ConsoleLogger(string name)
{
_name = name;
}

public bool WriteCore(
TraceType eventType, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
{
DebugCheck.NotNull(formatter);

Console.WriteLine("{0}: {1}: {2}", _name, eventType, formatter(state, exception));

return true;
}
}
}
}
9 changes: 9 additions & 0 deletions src/Microsoft.Data.Entity/Utilities/CodeAnnotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ internal sealed class InvokerParameterNameAttribute : Attribute
{
}
}

namespace Microsoft.Data.Entity
{
using System;

internal sealed class ValidatedNotNullAttribute : Attribute
{
}
}
4 changes: 2 additions & 2 deletions src/Microsoft.Data.Entity/Utilities/DebugCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace Microsoft.Data.Entity.Utilities
internal static class DebugCheck
{
[Conditional("DEBUG")]
public static void NotNull(object value)
public static void NotNull([ValidatedNotNull] object value)
{
Debug.Assert(value != null);
}

[Conditional("DEBUG")]
public static void NotEmpty(string value)
public static void NotEmpty([ValidatedNotNull] string value)
{
Debug.Assert(!string.IsNullOrWhiteSpace(value));
}
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.Data.Entity/project.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"version" : "0.1-pre-*",

This comment has been minimized.

Copy link
@Eilon

Eilon Feb 6, 2014

Member

Just wondering - is our MyGet feed "poisoned" by having a 0.1-pre-* of this package in there? I'm not sure if the Data package was pushed already to MyGet. I could imagine this might cause some issues with SemVer at some point. If needed, we can go delete the poison packages from MyGet.

BTW I think we said in the doc we wanted to use 0.1.0-alpha-* for the versions. Not sure it makes any practical difference but it might be good to be consistent on these anyway. (For all I know, all our packages have this wrong right now.)

This comment has been minimized.

Copy link
@pranavkm

pranavkm Feb 6, 2014

Contributor

There's a couple of -pre packages on MyGet now. We'll need to remove them if we want -alpha to work correctly.

This comment has been minimized.

Copy link
@davidfowl

davidfowl Feb 6, 2014

Member

I had to manually delete everything that messed up things recently. So yes we should do this.

This comment has been minimized.

Copy link
@Eilon

Eilon Feb 6, 2014

Member

@pranavkm do you want to work with @anpete to figure out what might need to be deleted with the feed? Me, @loudej and @davidfowl have access to delete stuff from the feed.

"version" : "0.1-alpha-*",
"dependencies": {
"Microsoft.AspNet.DependencyInjection" : "0.1-alpha-*"
"Microsoft.AspNet.DependencyInjection" : "0.1-alpha-*",
"Microsoft.AspNet.Logging" : "0.1-alpha-*"
},
"configurations": {
"net45": {
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Data.InMemory/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version" : "0.1-pre-*",
"version" : "0.1-alpha-*",
"dependencies": {},
"configurations": {
"net45": {},
Expand Down
24 changes: 24 additions & 0 deletions src/Microsoft.Data.Relational/RelationalDataStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace Microsoft.Data.SqlServer
{
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Utilities;

public class RelationalDataStore : DataStore
{
private readonly string _nameOrConnectionString;

public RelationalDataStore(string nameOrConnectionString)
{
Check.NotEmpty(nameOrConnectionString, "nameOrConnectionString");

_nameOrConnectionString = nameOrConnectionString;
}

public string NameOrConnectionString
{
get { return _nameOrConnectionString; }
}
}
}
6 changes: 4 additions & 2 deletions src/Microsoft.Data.Relational/project.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"version" : "0.1-pre-*",
"dependencies": {},
"version" : "0.1-alpha-*",
"dependencies": {
"Microsoft.Data.Entity" : "0.1-alpha-*"
},
"configurations": {
"net45": {},
"k10": {}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Data.SQLite/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version" : "0.1-pre-*",
"version" : "0.1-alpha-*",
"dependencies": {},
"configurations": {
"net45": {},
Expand Down
21 changes: 21 additions & 0 deletions src/Microsoft.Data.SqlServer/ApiExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace Microsoft.Data.SqlServer
{
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Utilities;

public static class ApiExtensions
{
public static EntityContext CreateContext(
this EntityConfiguration entityConfiguration, string nameOrConnectionString)
{
Check.NotNull(entityConfiguration, "entityConfiguration");
Check.NotEmpty(nameOrConnectionString, "nameOrConnectionString");

entityConfiguration.DataStore = new SqlServerDataStore(nameOrConnectionString);

return new EntityContext(entityConfiguration);
}
}
}
12 changes: 12 additions & 0 deletions src/Microsoft.Data.SqlServer/SqlServerDataStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace Microsoft.Data.SqlServer
{
public class SqlServerDataStore : RelationalDataStore
{
public SqlServerDataStore(string nameOrConnectionString)
: base(nameOrConnectionString)
{
}
}
}
5 changes: 4 additions & 1 deletion src/Microsoft.Data.SqlServer/project.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"version" : "0.1-pre-*",
"dependencies": {},
"dependencies": {
"Microsoft.Data.Entity" : "0.1-alpha-*",
"Microsoft.Data.Relational" : "0.1-alpha-*"
},
"configurations": {
"net45": {},
"k10": {}
Expand Down
2 changes: 1 addition & 1 deletion test/Microsoft.Data.Entity.FunctionalTest/_project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version" : "0.1-pre-*",
"version" : "0.1-alpha-*",
"dependencies": {
"xunit" : "1.9.2",
"Microsoft.Data.Entity" : "0.1-alpha-*"
Expand Down
Loading

0 comments on commit 6d12f1e

Please sign in to comment.