-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
- 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
There are no files selected for viewing
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 | ||
{ | ||
} | ||
} |
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)); | ||
} | ||
} | ||
} |
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)); | ||
} | ||
} | ||
} |
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.
Sorry, something went wrong.
Eilon
Member
|
||
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; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
{ | ||
"version" : "0.1-pre-*", | ||
This comment has been minimized.
Sorry, something went wrong.
Eilon
Member
|
||
"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": { | ||
|
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": {}, | ||
|
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; } | ||
} | ||
} | ||
} |
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": {}, | ||
|
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); | ||
} | ||
} | ||
} |
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) | ||
{ | ||
} | ||
} | ||
} |
I need to re-add the proj file - It was ignored by default, which I constantly forget :-)