Saturday, August 10, 2019

Json Serialzation in Entity Framework (EF Core 2.1)

I've had a number of instances where I want to serialize JSON into a single field in the database. In the past I resorted to a [NotMapped] field which held the complex object and a [JsonIgnore]d field which was serialized in EF to a Text SQL field.

I recently discovered that EF Core 2.1 includes easy value conversions! In the example below, I have a Stay entity which has a serialized Drive object. Drive is declared on SQL Server as a Text column.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
   base.OnModelCreating(modelBuilder);


   modelBuilder.Entity<Stay>()
   .Property(s => s.Drive)
   .HasConversion(
      d => JsonConvert.SerializeObject(d),
      d => JsonConvert.DeserializeObject<Drive>(d)
   );
}


Viola! No need for the extra fields on the model class.

Monday, May 20, 2019

Using lit-html inside an ASP.MVC .NET Core application with Typescript

This is the process for getting a lit-element component up and running inside and ASP.MVC .NET Core application.


Prerequisites: .NET Core, VSCode, Node. Install these before you start. You should be able to run “dotnet”, “code”, and “npm” successfully from a command line.


Create a project directory.
md Example.Web


Change to the project directory.
cd Example.Web


Create a new ASP.MVC project inside that directory.
dotnet new mvc


Open Visual Studio Code inside the new directory.
code .


Press F5 to start debugging the new app. VSCode will prompt you for an environment. Select “.NET Core” and a launch.json file will be created inside the .vscode directory. If you want to change the way the project launches when you debug, modify this launch.json file.


Press F5 again to start debugging. VSCode should launch your default browser with the web site up and running.


Return to VSCode and open a new terminal. Install lit-element.
npm install lit-element


Npm will create a “node_modules” subdirectory in your project. Just deal with the fact that now you have this folder related to node and don’t try to change the directory. It’s not likely worth it.


Create a scripts folder.
md scripts


Inside the scripts folder use VSCode to create a new file called my-element.ts. The following should be in the file:


import { LitElement, html } from '../node_modules/lit-element/lit-element';
class MyElement extends LitElement {
render(){
  return html`<p>Hello .NET Core and lit-html</p>`;
}
}
customElements.define('my-element', MyElement);


Save the file. Also inside the scripts folder, use VSCode to create another new file called main.js. The following text should be in the file.


import { MyElement } from './my-element';


Save the file. Using your terminal window, install typescript globally if you have not previously used typscript.
npm install -g typescript


Use VSCode to create a file called “tsconfig.json” in the root of your web project. The following text should be in the file.


{
  "compilerOptions": {
      "lib": ["es6"],
      "target": "es6",
      "noImplicitAny": true,
      "removeComments": true,
      "preserveConstEnums": true,
      "sourceMap": true
  },
  "include": [  "scripts" ],
  "exclude": [ "node_modules" ]
}


Run the typescript compiler in a terminal window.
tsc


You may see some errors from the lit-html compilation. I’m not what these are about but it seems safe to ignore them for now. You should now see my-element.js and my-element.map.js inside your scripts folder.


Now install webpack globally using npm in a terminal window if you have not previously used webpack.
npm install -g webpack


Note that the typescript and webpack installs only need to be done once on your machine. For future projects, you will not need to install them again.


Use VSCode to create “webpack.config.js” in the root of your web project. The following text should be in the file


var path = require('path');


module.exports = {
mode: 'development',
entry: './scripts/main.js',
output: {
  path: path.resolve(__dirname, 'wwwroot/js'),
  filename: 'bundle.js'
}
};


This configuration will tell webpack take everything that is referenced in the main.js file and bundle it into a single file inside wwwrooot/js/bundle.js. Run webpack from a terminal.
webpack


You should now see a “bundle.js” file inside wwwroot/js.


Edit the file /Views/Home/Index.html using vscode. Paste the following lines in at the end of the file.


<script type="module" src="/js/bundle.js"></script>
<my-element></my-element>


Return to the browser and you should see your lit-html component on the page.

Monday, April 3, 2017

Publishing to a Nuget Server

First, the Nuget CLI needs to be installed on your machine. Place the .exe in a folder and add that folder to your local path. The nuget CLI exe can be found here: 

https://dist.nuget.org/

Run the following command one time to set the api key permanently for your nuget server. 

nuget setApiKey yourapikey -source http://yournugeturl

Change directories to where your .csproj file is located and run this command: 

nuget spec

The command above will generate a nuspec file. Edit the nuspec file and delete or fill in any elements without $. The $'s indicate variables that will be automatically replaced during the pack process. Do not change those elements. 

Next edit your assembly info for your project:

Edit /properties/AssemblyInfo.cs using Visual Studio

In the assembly info file you'll find all the properties for your project. I recommend that you let Visual Studio supply the build and revision numbers (so 1.0.*) as mentioned in the comments in this file. Make sure other values are set to your liking. 

Next, from a command prompt, run the following command:

nuget pack

This will package up your project and create a .nupkg file for upload to the nuget server. Now you're ready to push your package to the server. Run the following command and you're done: 

nuget push yourpack.nupkg -source http://yournugeturl

Thursday, September 17, 2015

Efficient CSS Using Child-Selectors

So, I'm working away on acaexpress.com and I find myself forgetting this over and over. I figured it would be a good idea to write a blog post about it to remind me there's a better way. 

Here's a typical use of CSS to assign margin to some images: 

<style>
    .issuerpics { margin: 15px; }
</style>

<div style="text-align:center">
    <img src="=logobluecross.png" alt="Blue Cross" class="issuerpics" />
    <img src="=logoaetna.png" alt="Aetna" class="issuerpics" />
    <img src="=logocoventry.png" alt="Coventry One" class="issuerpics" />
    <img src="=logocigna.png" alt="Cigna" class="issuerpics" />
    <img src="~=logouhone.png" alt="United Health One" class="issuerpics" />
</div>

Note that each image has a CSS class assigned to it. It's a logical technique, but not the most elegant one. Below, the parent element (div) is assigned a class and then the CSS is written to style the child elements of that class. It's less code, more readable, and in general more elegant. 

<style>
    .issuerpics img { margin: 15px; }
</style>

<div class="issuerpics" style="text-align:center">
    <img src="=logobluecross.png" alt="Blue Cross" />
    <img src="=logoaetna.png" alt="Aetna" />
    <img src="=logocoventry.png" alt="Coventry One" />
    <img src="=logocigna.png" alt="Cigna" />
    <img src="=logouhone.png" alt="United Health One" />
</div>

In general, it's best to make the code as readable as possible. Less code is usually better as long as it doesn't sacrifice readability. This change accomplishes both. 

Thursday, August 14, 2014

Lime ORM for Android

Hey friends. I wanted to let everyone know I've released a little library called Lime for Android. It's an ORM layer inspired by Microsoft's Entity Framework. I feel it has a few advantages over some of the other layers out there.

  • It's fast. Lime uses reflection to populate objects from cursors, but object inspection is done only once and cached. 
  • It's efficient. Lists of objects are backed by cursors so scrolling through huge data sets only requires conservative memory allocation. 
  • It's flexible. Most layers in Lime are exposed to make using as much or as little as necessary a simple matter of choice. 
  • It's familiar. Lime works with Android's content providers, observers, and loaders. It's all the things you already know, but easier. 
Stop by the Lime site on Bitbucket and take it for a spin. I think you'll dig it. 

Wednesday, July 9, 2014

Pull To Refresh on Android with SwipeRefreshLayout

In March of 2014, without much fanfare, Google added the SwipeRefreshLayout widget to the Android SDK. This addition formalizes the pull-to-refresh mechanism on Android. Let's take a look at how we'll use it.

First, make sure you have R19.1.0 or newer version of the Android Support Library in your project. R20 is the first version of the library to include the new layout. Once it's in your project, create a simple activity layout with the SwipeRefreshLayout at the top of the view hierarchy. While this isn't explicitly necessary, it makes sense that the entire activity be pulled down to refresh.

It is explicitly necessary that this layout have only one child. In this example, we'll make the only child a TextView for simplicity. Here's a look at our activity layout:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent">

   <TextView android:id="@+id/text1" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:text="@string/hello_world" />

</android.support.v4.widget.SwipeRefreshLayout>


Now that we have the layout added, we'll need a little code to make the refresh happen. The new layout has a method to setOnRefreshListener. So let's take a look at our activity with that listener connected:

public class MainActivity extends Activity { 

   SwipeRefreshLayout layout; 
   TextView text1; 

   OnRefreshListener refreshListener = new OnRefreshListener() { 
      @Override public void onRefresh() { 
         text1.setText(new Date().toString()); 
         layout.setRefreshing(false); 
      } 
   }; 

   @Override protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       layout = (SwipeRefreshLayout)findViewById(R.id.layout); 
       text1 = (TextView)findViewById(R.id.text1);
       layout.setOnRefreshListener(refreshListener);
       layout.setColorSchemeResources(color.holo_blue_dark, color.holo_blue_light,color.holo_orange_dark, color.holo_green_light); 
   } 
}


Notice that we need to call layout.setRefreshing(false) when our refresh code is finished. This method stops the animation and let's the layout know that our work is done. Typically this would be called when a loader in the activity completes. 

Also, take a look at SwipeRefreshLayout.setColorSchemeResources. This method allows you to choose 4 colors to be in the animated bar while the layout is refreshing. 

SwipeRefreshLayout is a great addition to the SDK and goes a long way to standardizing a user interaction method. Implementing will give your users comfort that their app is acting in a standard fashion and also save you space on the ActionBar where your refresh button previously would have been!

Thursday, August 9, 2012

Emulating the Nexus 7 Display Properties

Ahh the new Nexus 7 tablet is a sweet piece of hardware! On top of that, it's selling like mad. Are you on the waiting list, but need to make sure your apps are ready for it? This blog post is about creating an emulator image to emulate the display of the Nexus 7.

Before we start, please read the Android developer blog post on getting ready for Nexus 7. It's a good start to our discussion. Getting Your App Ready for Jelly Bean and Nexus 7.

Now that you're up to date, you know the new tablet is one of the first to sport the tvdpi screen density. If your app isn't ready for it, you might be in trouble. So, what do we need to do to set up an emulator for this density?

  1. Open AVD Manager and create a new virtual device named "nexus_7".
  2. Unless you have set up the hardware accelerated emulator set the "Target:" to Android 2.3.3. This will run fast enough to test your apps for the display. Note that you're not creating a real Nexus 7 emulator but this will be sufficient to test your app for the display! If you really want to test Jelly Bean, you'll want to set up the hardware acceleration for your emulator. Hopefully we'll discuss that in another blog post.
  3. Set up a sufficient SD card size. 
  4. Under "Skin:" choose resolution and specify the Nexus 7 resolution of 720 x 1280. Note that we put 720 first because we want the emulator to start up in portrait mode just like the device.
  5. Under "Hardware:", choose an Abstracted LCD density of 213. This is tvdpi!
Now you're set! When you start the emulator you will probably want to set the Launch Options to scale the display to 8 inches, especially if you're on a laptop screen. Now you should be able to test your layouts for the new Nexus 7. Have fun.