ASP.NET 5 (i.e. Core 1.0) Immutable Configuration - asp.net

I can't get ASP.NET 5 (i.e. Core 1.0) to work with immutable configuration classes.
Let me demonstrate first using a mutable class.
config.json
{
"Name": "Lou",
"Age": 30
}
Config.cs (mutable)
public class Config
{
public string Name { get; set; }
public int Age { get; set; }
}
Registration in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
var configBuilder = new ConfigurationBuilder()
.AddJsonFile("config.json");
var config = configBuilder.Build();
services.Configure<Config>(config);
services.AddMvc();
}
The code above works well and the values of Config.Name and Config.Age are as expected.
But once I change Config's property setters to private, they are no longer set by the configuration builder:
public class Config
{
public string Name { get; private set; } // == null
public int Age { get; private set; } // == 0
}
I would have expected ASP.NET 5 to use reflection and set the properties like it does with ASP.NET 4, instead of simply ignoring them. Am I missing something?
I created a sample project to demonstrate: https://github.com/johnnyoshika/mvc6-immutable-config
The master branch uses a mutable Config class while the immutable-config branch uses an immutable Config class.

The ConfigurationBinder object doesn't set private properties: https://github.com/aspnet/Configuration/blob/ba1d9b4cbc363db9318f201f08fbfecc72e7922b/src/Microsoft.Extensions.Configuration.Binder/ConfigurationBinder.cs#L64-L82
Here's an issue tracking that: https://github.com/aspnet/Configuration/issues/394
One way to workaround this is to create an interface with getters only that's implemented by your Config code. Then, you can use that in your code.

Related

EF Core with CosmosDB: OwnsOne and OwnsMany throw NullReferenceException

I'm working on a new project that uses CosmosDB and Entity Framework Core (via the Microsoft.EntityFrameworkCore.Cosmos NuGet package, version 5.0.7; the project itself is .NET Core 5). I'm new to both, and running into an issue I can't sort out.
In short, I need to save a complex object to the database. It's a big model that will have multiple collections of classes underneath it, each with their own properties and some with collections underneath them as well. I'm trying to configure EF with OwnsOne and OwnsMany to store these child objects underneath the top-level one. The code compiles, and will save to the database so long as all the owned objects are left empty. But whenever I put anything into an owned object, either with OwnsOne or OwnsMany, I get a pair of NullReferenceExceptions.
I've tried to strip my code down to the very basics. Here's how it currently looks.
Owner and owned classes:
public class Questionnaire
{
// Constructors
private Questionnaire() { }
public Questionnaire(Guid id)
{
Test = "Test property.";
TV = new TestQ();
Id = id;
}
public Guid Id { get; set; }
public string Test { get; set; }
public TestQ TV { get; set; }
// Public Methods
public void AddForm(Form f)
{
// not currently using this method
//Forms.Add(f);
}
}
public class TestQ
{
public TestQ()
{
TestValue = "test ownsone value";
}
public string TestValue { get; set; }
}
DbContext:
public class QuestionnaireDbContext : DbContext
{
public DbSet<Questionnaire> Questionnaires { get; set; }
public QuestionnaireDbContext(DbContextOptions<QuestionnaireDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultContainer(nameof(Questionnaires));
modelBuilder.Entity<Questionnaire>().HasKey(q => q.Id);
modelBuilder.Entity<Questionnaire>().OwnsOne(q => q.TV);
}
}
And the code from the service that calls the dbContext (note that this is based on a generic service that I didn't set up originally). The actual exceptions are thrown here.
public virtual TEntity Add(TEntity entity)
{
_context.Entry(entity).State = EntityState.Added;
_context.SaveChanges();
return entity;
}
Ultimately I need this to work with OwnsMany and a collection, but I figured it might be simpler to get it working with OwnsOne first. The key thing to note here is that if I comment out the line
TV = new TestQ();
in the Questionnaire class, the model persists correctly into CosmosDB. It's only when I actually instantiate an owned entity that I get the NullReferenceExceptions.
Any advice would be much appreciated! Thank you!
Well, I'm not sure why this is the case, but the issue turned out to be with how we were adding the document. Using this generic code:
public virtual async Task<TEntity> Add(TEntity entity)
{
_context.Entry(entity).State = EntityState.Added;
await _context.SaveChanges();
return entity;
}
was the issue. It works just fine if I use the actual QuestionnaireDbContext class like so:
context.Add(questionnaire);
await context.SaveChangesAsync();

Global json serialization option in .Net Core 3.x

I have .Net Core 3.1 Web Api. I am using System.Text.Json serializer since it became a default for .Net Core 3.x applications. I have set global enum to string converter as follows:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddControllers()
.AddJsonOptions(options =>
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter())));
...
}
Enums are converted to string for controller responses (which makes sense since I configure on AddControllers() method). But if I try to manually serialize an object it still serializes enum as int. Sample below:
public class TestClass
{
public void Test()
{
var data = JsonSerializer.Serialize(new MyObject { Enum1 = MyEnum.Value1, Enum2 = MyEnum.Value2 });
}
public class MyObject
{
public MyEnum Enum1 { get; set; }
public MyEnum Enum2 { get; set; }
}
public enum MyEnum
{
Value1 = 1,
Value2 = 2
}
}
OUTPUT:
data [string]: "{\"Enum1\":1,\"Enum2\":2}"
If I add conversion attribute manually then it serializes as desired:
public class MyObject
{
[JsonConverter(typeof(JsonStringEnumConverter))]
public MyEnum Enum1 { get; set; }
public MyEnum Enum2 { get; set; }
}
OUTPUT
data [string]: "{\"Enum1\":\"Value1\",\"Enum2\":2}"
Is there a way to set global json serialization options (enum to string conversion) that would apply for manual serialization as well?
Or maybe I should just always stick to the explicit attributes?
Currently you can't. JsonSerializer being a static class with only purely static methods, default options cannot be set. You can check this open issue on Github. It seems that it has been designed this way for better performance.
As an alternative, you can create your own static class
public static class CustomJsonSerializer
{
private static JsonSerializerOptions serializerSettings =
new JsonSerializerOptions { /* whatever you need */};
public static T Deserialize<T>(this string json)
{
return JsonSerializer.Deserialize<T>(json, serializerSettings );
}
// etc.
}
I managed to get this to work by adding NewtonsoftJson and then register StringEnumConverter:
services
.AddControllers(jsonOptions => jsonOptions.ReturnHttpNotAcceptable = true)
.AddNewtonsoftJson(jsonOptions =>
{
jsonOptions.SerializerSettings.Converters.Add((JsonConverter) new StringEnumConverter());
}

How to specify default property values for owned entity types in Entity Framework Core 2.0?

I have a simple POCO type, say something like
public class OwnedEntity {
public string stringProperty { get; set; }
public decimal decimalProperty { get; set; }
public bool boolProperty { get; set; }
public int intProperty { get; set; }
}
and an actual entity with an OwnedEntity reference
public class SomeEntity {
public string Id { get; set; }
public OwnedEntity OwnedEntity { get; set; }
}
I set up the relationship like described in the documentation using EF Core's Fluent API:
protected override void OnModelCreating (ModelBuilder builder) {
base.OnModelCreating (builder);
builder.Entity<SomeEntity> ().OwnsOne (e => e.OwnedEntity);
}
I can't find anything on how to define default-values for all the properties of OwnedEntity. I tried to initialize the properties like this:
public class OwnedEntity {
public string stringProperty { get; set; } = "initial"
public decimal decimalProperty { get; set; } = -1M;
public bool boolProperty { get; set; } = false;
public int intProperty { get; set; } = -1;
}
but with no effect. Same goes with the [DefaultValueAttribute] (but that was to expect since it's explicitly mentioned).
There's a bit of information on how to handle initial values for regular entities:
modelBuilder.Entity<SomeOtherEntity>()
.Property(e => e.SomeIntProperty)
.HasDefaultValue(3);
But since I'm facing an Owned Entity Type, I can't access the type via Entity<T>.
Is there a way of doing what I'm looking for?
Some things worth mentioning:
I have a solid amount of specific entities where most of them are using the OwnsOne relation
Declaring all OwnedEntity-properties in a base class is not an option since not all the entities have those properties
I`m using EF Core 2.0.3 and ASP.NET Core MVC 2.0.4
Edit:
Originally, I wanted to have newly created SomeEntity instances to come with preset properties for all of the 'embedded' SomeEntity.OwnedEntity properties.
But looking at how my associated controller works, it all makes sense... I have the following methods for the 'Create' operation:
[HttpGet]
public IActionResult Create () {
return View (nameof (Create));
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create (SomeEntity model) {
context.Add (model);
await context.SaveChangesAsync ();
// redirect etc.
}
Which means that no object is created for the [HttGet] overload of Create and all the HTML inputs linked to properties (via asp-for) are initially empty. Okay. So I guess the proper way of doing this is to manually create a new instance of SomeEntity and pass it to the Create view like this:
[HttpGet]
public IActionResult Create () {
return View (nameof (Create), new SomeEntity());
}
Is this the right approach then or are there some more things to keep in mind?
Assuming you understand what EF Core Default Values are for, and just looking for equivalent of Entity<T>().Property(...) equivalent.
The owned entities are always configured for each owner type by using the ReferenceOwnershipBuilder<TEntity,TRelatedEntity> class methods. To access this class you either use the result of OwnsOne method, or use the OwnsOne overload taking second argument of type Action<ReferenceOwnershipBuilder<TEntity,TRelatedEntity>>.
For instance, using the second approach:
builder.Entity<SomeEntity>().OwnsOne(e => e.OwnedEntity, ob =>
{
ob.Property(e => e.stringProperty)
.HasDefaultValue("initial");
ob.Property(e => e.decimalProperty)
.HasDefaultValue(-1M);
// etc.
});

ASP.NET 5 DI app setting outside controller

I can DI app setting in the controller like this
private IOptions<AppSettings> appSettings;
public CompanyInfoController(IOptions<AppSettings> appSettings)
{
this.appSettings = appSettings;
}
But how to DI that in my custom class like this
private IOptions<AppSettings> appSettings;
public PermissionFactory(IOptions<AppSettings> appSetting)
{
this.appSettings = appSettings;
}
my register in Startup.cs is
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
The "proper" way
Register your custom class in the DI, the same way you register other dependencies in ConfigureServices method, for example:
services.AddTransient<PermissionFactory>();
(Instead of AddTransient, you can use AddScoped, or any other lifetime that you need)
Then add this dependency to the constructor of your controller:
public CompanyInfoController(IOptions<AppSettings> appSettings, PermissionFactory permFact)
Now, DI knows about PermissionFactory, can instantiate it and will inject it into your controller.
If you want to use PermissionFactory in Configure method, just add it to it's parameter list:
Configure(IApplicationBuilder app, PermissionFactory prov)
Aspnet will do it's magic and inject the class there.
The "nasty" way
If you want to instantiate PermissionFactory somewhere deep in your code, you can also do it in a little nasty way - store reference to IServiceProvider in Startup class:
internal static IServiceProvider ServiceProvider { get;set; }
Configure(IApplicationBuilder app, IServiceProvider prov) {
ServiceProvider = prov;
...
}
Now you can access it like this:
var factory = Startup.ServiceProvider.GetService<PermissionFactory>();
Again, DI will take care of injecting IOptions<AppSettings> into PermissionFactory.
Asp.Net 5 Docs in Dependency Injection
I recommend not passing AppSettings. A class shouldn't depend on something vague - it should depend on exactly what it needs, or close to it. ASP.NET Core makes it easier to move away from the old pattern of depending on AppSettings. If your class depends on AppSettings then you can't really see from the constructor what it depends on. It could depend on any key. If it depends on a more specific interface then its dependency is clearer, more explicit, and you can mock that interface when unit testing.
You can create an interface with the specific settings that your class needs (or something less specific but not too broad) and a class that implements it - for example,
public interface IFooSettings
{
string Name { get; }
IEnumerable Foos { get; }
}
public interface IFoo
{
string Color { get; }
double BarUnits { get; }
}
public class FooSettings : IFooSettings
{
public string Name { get; set; }
public List<Foo> FooList { get; set; }
public IEnumerable Foos
{
get
{
if (FooList == null) FooList = new List<Foo>();
return FooList.Cast<IFoo>();
}
}
}
public class Foo : IFoo
{
public string Color { get; set; }
public double BarUnits { get; set; }
}
Then add a .json file, fooSettings.json:
{
"FooSettings": {
"Name": "MyFooSettings",
"FooList": [
{
"Color": "Red",
"BarUnits": "1.5"
}, {
"Color": "Blue",
"BarUnits": "3.14159'"
}, {
"Color": "Green",
"BarUnits": "-0.99999"
}
]
}
}
Then, in Startup() (in Startup.cs) where we specify what goes into our Configuration, add fooSettings.json:
var builder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddJsonFile($"config.{env.EnvironmentName}.json", optional: true)
.AddJsonFile("fooSettings.json");
Finally, in ConfigureServices() (also in Startup.cs) tell it to load an instance of FooSettings, cast it as IFooSettings (so the properties appear read-only) and supply that single instance for all dependencies on IFooSettings:
var fooSettings = (IFooSettings)ConfigurationBinder.Bind<FooSettings>(
Configuration.GetConfigurationSection("FooSettings"));
services.AddInstance(typeof (IFooSettings), fooSettings);
Now your class - controller, filter, or anything else created by the DI container - can have a dependency on IFooSettings and it will be supplied from the .json file. But you can mock IFooSettings for unit testing.
Original blog post - it's mine so I'm not plagiarizing.
You can do dependency injection in your non-controller classes as well.
In your startup class,
public class Startup
{
public IConfigurationRoot Configuration { get; set; }
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
// register other dependencies also here
services.AddInstance<IConfiguration>(Configuration);
}
}
Now in your custom class, Have the constructor accept an implementation of IConfiguration
private IConfiguration configuration;
public PermissionFactory(IConfiguration configuration)
{
this.configuration = configuration;
}
public void SomeMethod()
{
var someSection = this.configuration.GetSection("SomeSection");
var someValue= this.configuration.Get<string>("YourItem:SubItem");
}
If you want to DI to action filter reference to Action filters, service filters and type filters in ASP.NET 5 and MVC 6 service filter part.

Store single instance in DbContext with ASP.NET MVC 5 CF EF (no list)

I have a class called AppSettings where I store some settings of my application. So far, I only used Lists in my DbContext like
public class MyDbContext: DbContext {
public DbSet<User> Users { get; get; }
}
But for the settings, I need no list. I only want to store a single instance of my AppSettings class. I tried to set it as a normal member
public class AppSettingsContext: DbContext {
public AppSettings AppSetting { get; get; }
}
But this is not working: EF will throw an exception that the entity type AppSettings is not a part of the model for the current context. The Code:
using(var db = new AppSettingsContext()) {
var setting = new AppSettings() {
AttributeA = "Test",
//...
};
db.Entry(setting).State = EntityState.Added;
db.SaveChanges();
}
Is it possible to do this with EF? Or am I forced to implement this logic on my own by using a not mapped attribute where I make sure that only one single instance is stored and returned by the database?
If you want to store your settings in the DB, you can't store singular, that's not how TSQL works.
If you only want singular settings for a user, I would recomend web.config. If you REALLY want to store it in the DB though and want it to have a more concrete feeling you could just extend your database context like so:
public class MyDbContext: DbContext {
public DbSet<User> Users { get; get; }
public DbSet<AppSettings> AppSettings { get; set;}
}
public static class MyDbExtensions
{
public static async Task<AppSettings> DbSettings(this MyDbContext context, Guid settingsGuid)
{
return await context.AppSettings.FirstAsync(as => as.Id == settingsGuid)
}
// OR
public static async Task<AppSettings> UserSettings(this MyDbContext context)
{
return await context.AppSettings.FirstAsync(as => as.Id == UserSettingsDbGuid)
}
public static Guid UserSettingsDbGuid = "Guid of user settings goes here"
}
// example usage:
var context = GETDBCONTEXTMETHOD();
var userSettings == context.DbSettings(MyDbExtensions.UserSettingsDbGuid);
// OR
userSettings == context.UserSettings();

Resources