Scott Hanselman

Exploring the .NET open source hybrid ORM library RepoDB

July 17, 2020 Comment on this post [9] Posted in Open Source
Sponsored By

ShutterstockIt's nice to explore alternatives, especially in open source software. Just because there's a way, or an "official" way doesn't mean it's the best way.

Today I'm looking at RepoDb. It says it's "a hybrid ORM library for .NET. It is your best alternative ORM to both Dapper and Entity Framework." Cool, let's take a look.

Michael Pendon, the author puts his micro-ORM in the same category as Dapper and EF. He says "RepoDb is a new hybrid micro-ORM for .NET designed to cater the missing pieces of both micro-ORMs and macro-ORMs (aka full-ORMs). Both are fast, efficient and easy-to-use. They are also addressing different use-cases."

Dapper is a great and venerable library that is great if you love SQL. Repo is a hybrid ORM and offers more than one way to query, and support a bunch of popular databases:

Here's some example code:

/* Dapper */
using (var connection = new SqlConnection(ConnectionString))
{
var customers = connection.Query<Customer>("SELECT Id, Name, DateOfBirth, CreatedDateUtc FROM [dbo].[Customer];");
}

/* RepoDb - Raw */
using (var connection = new SqlConnection(ConnectionString))
{
var customers = connection.ExecuteQuery<Customer>("SELECT Id, Name, DateOfBirth, CreatedDateUtc FROM [dbo].[Customer];");
}

/* RepoDb - Fluent */
using (var connection = new SqlConnection(ConnectionString))
{
var customers = connection.QueryAll<Customer>();
}

I like RepoDB's strongly typed Fluent insertion syntax:

/* RepoDb - Fluent */
using (var connection = new SqlConnection(connectionString))
{
var id = connection.Insert<Customer, int>(new Customer
{
Name = "John Doe",
DateOfBirth = DateTime.Parse("1970/01/01"),
CreatedDateUtc = DateTime.UtcNow
});
}

Speaking of inserts, it's BulkInsert (my least favorite thing to do) is super clean:

using (var connection = new SqlConnection(ConnectionString))
{
var customers = GenerateCustomers(1000);
var insertedRows = connection.BulkInsert(customers);
}

The most interesting part of RepoDB is that it formally acknowledges 2nd layer caches and has a whole section on caching in the excellent RepoDB official documentation. I have a whole LazyCache subsystem behind my podcast site that is super fast but added some complexity to the code with more Func<T> that I would have preferred.

This is super clean, just passing in an ICache when you start the connection and then mention the key when querying.

var cache = CacheFactory.GetMemoryCache();
using (var connection = new SqlConnection(connectionString).EnsureOpen())
{
var products = connection.QueryAll<Product>(cacheKey: "products", cache: cache);
}

using (var repository = new DbRepository<Product, SqlConnection>(connectionString))
{
var products = repository.QueryAll(cacheKey: "products");
}

It also shows how to do generated cache keys...also clean:

// An example of the second cache key convention:
var cache = CacheFactory.GetMemoryCache();
using (var connection = new SqlConnection(connectionString).EnsureOpen())
{
var productId = 5;
Query<Product>(product => product.Id == productId,
cacheKey: $"product-id-{productId}",
cache: cache);
}

And of course, if you like to drop into SQL directly for whatever reason, you can .ExecuteQuery() and call sprocs or use inline SQL as you like. So far I'm enjoying RepoDB very much. It's thoughtfully designed and well documented and fast. Give it a try and see if you like it to?

Why don't you head over to https://github.com/mikependon/RepoDb now and GIVE THEM A STAR. Encourage open source. Try it on your own project and go tweet the author and share your thoughts!


Sponsor: Have you tried developing in Rider yet? This fast and feature-rich cross-platform IDE improves your code for .NET, ASP.NET, .NET Core, Xamarin, and Unity applications on Windows, Mac, and Linux.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

How to make separate Work and Personal Profiles with the New Microsoft Edge on Beyonce's Internet

July 15, 2020 Comment on this post [16] Posted in Win10
Sponsored By

Edge for WerkI'm a long time Chrome user but have been slowly finding myself using the new Edge (Edgium?) and am now basically living in it full time.

Why?

  • My work use O365/M365 logins and it is great with a "Work profile" and "Personal profile" that keeps EVERYTHING separate.
  • It even has a "open link as Work/Personal" right click menu that I use WAY more often than I would have thought.
    Open Link as Work
  • It'll auto switch you to Work or Personal logins when you end up getting a OneDrive link or are logging into Azure.
  • Runs on my last Windows 7 machine through my Windows 10 machines, and <GASP> since it syncs everything AND has work/personal profiles I use it on my iPhone 11 Max.

But the main reason? I really like the way it deals with PWAs - Progressive Web Apps. Basically "make this website an app." I'll pretend if you pretend. You can use Outlook, Twitter, Gmail, Teams, TONS of websites as apps that are no-install. They all still run in Edge (with the Chromium heart) but they are pinned to your taskbar and/or start menu. A bunch of folks on my team legit don't install Office or Teams anymore. They use all of Office.com as a PWA. I was surprised but it works.

Here's some of my installed PWAs/apps:

Install this site as an app

If I visit Twitter.com for example and click the Circle with the Plus inside it in the URL bar, I'll see this.

image

Once it's "installed" I can pin it and run it and it looks like this in the taskbar. Four of these apps are PWAs with Edge. Gmail's icon is lame and old looking. They should fix that for their PWA.

Pinned apps with Edge

Lovely.

Making custom Pinned Edge icons with Profiles

I mentioned profiles before. Here's mine. I have no idea why I bothered to hide the emails.

Edge profile picker

The picker is nice, but I actually wanted TWO DIFFERENT EDGES pinned to my Taskbar. A Work and a Personal Edge. I find it easier to compartmentalize and easier than switching.

UPDATE: It seems as of a recent version of Edge you can just open Edge the usual way, switch to the Profile you want, then right click the running Edge in your Taskbar and "Pin to Taskbar" and you'll get your custom Edge with the Profile Directory switch correctly configured! Super convenient. That means the manual steps below are not needed unless you want to understand the internals, add a custom switch (which can also be done from Properties), or apply a custom icon.

Right click your Desktop and say New Shortcut from the right click menu.

File | New Shortcut

Put this in the location box above but change USERNAME to the right one for YOUR folder structure. And note --profile-directory. You can find your Profile folders in C:\Users\USERNAME\AppData\Local\Microsoft\Edge SxS\User Data. Mine are Profile 1 and 2, where 1 is my first Personal one and 2 is Work. Check yours and do the right thing. Note also the msGuidedSwitchAllowed switch that is optional. That tells you if you want Edge to suggest another profile if you visit a website that you really need your Work (or anotherr profile) logged in for.

"C:\Users\USERNAME\AppData\Local\Microsoft\Edge SxS\Application\msedge.exe" --profile-directory="Profile 2" --enable-features=msGuidedSwitchAllowed

Paste this in and hit Next, then Name the shortcut. If you like, use an Icon Editor and make a custom icon overlay for your work or personal Edge so you can tell the difference! I like Liquid Icon for simple editing or Greenfish Icon Editor.

I thought about using the Microsoft logo as an overlay for work but instead chose Beyoncé, so it's Edge for Werk.

image

Since it's a custom profile it can have a custom icon. Edge will make one for you you can use with YOUR face in your Profile X folder as mentioned above. Now my Desktop has two Edge icons, just like I like it.

I can also have the Canary (early builds), Dev version, or Stable Edge pinned. Lots of choices.

Edge for Werk

This makes it easy for me to run and keep track of what context I'm running in when I'm using a personal machine for Work.

Also note if you go to edge://settings/profiles/multiProfileSettings you can decide which profile a new URL external link will open with! You can also turn off Auto profile switching if you like.

Multiple profile preferences

All this, plus PWAs has made my browsing on Beyoncé's Internet quite nice lately.


Sponsor: Have you tried developing in Rider yet? This fast and feature-rich cross-platform IDE improves your code for .NET, ASP.NET, .NET Core, Xamarin, and Unity applications on Windows, Mac, and Linux.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

You're tired because your lizard brain knows that Zoom meetings aren't natural

June 26, 2020 Comment on this post [17] Posted in Musings | Remote Work
Sponsored By

Remote work isn't normal. It's great when it's not quarantine work, to be clear. I've worked remotely with success for over 13 years and written about it extensively. I'm pro-remote work. But.

Doing zoom calls all day can be super productive but they are also physically and emotionally exhausting. One of the reasons that isn't helping you is that a zoom meeting isn't a natural human state - it's a simulation of one and your body knows the truth.

I'm using "Zoom" the brand here as a pervasive generic verb like folks use Kleenex for tissue or Google for any websearch. I zoom with Teams and google with Bing, for example. You get the idea.

So what do I mean when I say "remote work isn't normal?" Why such a declarative statement? Because I'd propose that our lizard brains know that we're talking to ourselves, alone in a room.

"The brain stem, the cerebellum, and the basal ganglia – commonly known as “the lizard brain” because it’s the part we inherited from our reptilian predecessors – are responsible for fight or flight responses, fear, suspicion, and anxiety. The lizard brain kept our ancestors safe when a panther pounced from a limb just overhead. In an ironic twist, these most ancient parts of the brain are making it difficult for us to think rationally at a time when rational thought is key to our survival." - Courier Journal

Your body knows the people aren't there. Their electricity isn't in the room with you. They are flat. All their voices come from one point in space, not from all over the room. There is no sense of space. Even worse if you have headphones. They are 3 inch tall tiny talking heads while their voices are 1 inch from your ears. There's a strange detached intimacy where you're both closely connected to the group virtually while utterly disengaged physically.

Teams and Zoom calls aren't natural

A Brady Bunch grid of faces is not what the brain expects when you're having a meeting, so we're constantly fighting against the cognitive dissonance, the background process load, the psychic weight, that we ARE in the same room talking with our co-workers.

Your conscious brain says you're having a chat in a room with a dozen of your co-workers but the unconscious brain says you're not. It's small but I'd propose it's there. Doing this for hours and hours can give you the same kind of unease that you get from flying in a plane. You get off the plane and you're exhausted from sitting. Sure there's humidity and oxygen issues but there's also the "you know you're moving but parts of your brain disagrees" cognitive load that can't be ignored.

Remote video group calls are an amazing enabling technology but it's also enabling to acknowledge that they can be draining. That simple acknowledgement - huh, that's a thing! - is itself empowering. If you name it you can claim it. It's not just you!


Sponsor: Upgrade from file systems and SQLite to Actian Zen Edge Data Management. Higher Performance, Scalable, Secure, Embeddable in most any programming language, OS, on 64-bit ARM/Intel Platform.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Using the Blockly visual programming editor to call a .NET Core WebAPI

June 24, 2020 Comment on this post [7] Posted in ASP.NET | ASP.NET Web API | Open Source
Sponsored By

I like to showcase interesting and cool open source projects that need more attention! Go give our friend a star on GitHub! NetCoreBlockly on GitHub is clever and fun!

Blockly is a JavaScript library for building visual programming editors. If you've used languages like Scratch you've seen block-style programming environments. The picture below shows you a demo of calling a .NET Core WebAPI using Blockly code!

Remember that once you have a WebAPI you can make it available in a number of ways. The "projection" of the methods that the WebAPI makes available can be presented visually, as Swagger/OData/GraphQL, however you like. Another project is WebAPI2CLI that lets you call WebAPI endpoints easily from a CLI (Command Line Interface) that is more high fidelity than curl or wget.

.NET Core as a Blockly Editor

What .NET Core Blockly is doing is looking at the projection/interface of your WebAPI and generating Blockly blocks! That means that anyone (business users, student, tester, whatever) could try out your WebAPI with a simple drag and drop interface in their browser!

You can pull in Swagger, OData, or GraphQL interfaces.

I love seeing fun projects like this that make the web (and .NET code) easier to use! Go give them some stars and get involved!


Sponsor: Upgrade from file systems and SQLite to Actian Zen Edge Data Management. Higher Performance, Scalable, Secure, Embeddable in most any programming language, OS, on 64-bit ARM/Intel Platform.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

The Importance of Nesting when Remote Working and Quarantine Working

June 12, 2020 Comment on this post [9] Posted in Musings
Sponsored By

"08.30.10" by colemama is licensed under CC BY-NC-SA 2.0.We've all learned the hard way that Quarantine work is not Remote work. It doesn't feel the same because it's not the same. It's a hard time right now and tension is high.

"People are overwhelmed, afraid, and stressed. There's a background pressure - a psychic weight or stress - that is different in these times. This isn't a problem you can fix with a new webcam or a podcasting mic."

I really believe that self-care is important and one should be as deliberate as one can in how they live.

One day we were working in the office and the next day we were home indefinitely. Some in spare bedrooms, most in our kitchens, laundry rooms, garages and front porches.

What does "nesting" mean?

Nesting is not just what a bird does to prepare their space for the coming family, it's also what we can do as humans to make a space for ourselves to be successful. It's the deliberate practice of setting up your work area so that you can be successful and fulfilled.

Your space doesn't need to be fancy. Nesting isn't blinging your space or making it look expensive - nesting is making it YOURS.

  • Can you sit and work comfortably? Is your space ergonomic as it can be?
    • Is your monitor or laptop angled in a way that doesn't cause eyestrain or neck strain?
  • Do the things around you feed your spirit? Toys? Gadgets? Family pics? Post-It Notes?
    • Be intentional. Don't just let your space happen. What is calming for you? What's productive? Do you like a nice whiteboard?
    • Perhaps a fresh notebook can de-stress you? Your office can be a Zen garden. Does clutter calm? Fill it with fun. Do you like open space? Clear your desk.
  • What's your lighting situation?
    • Do you have natural light or a window nearby? Maybe you should.

Stop for a second. Perhaps while reading this blog post. Look around and ask yourself - why is this space like this? How did it get this way and do I feel good here. What can you do? Maybe it's just straightening up. Perhaps literally just turning another direction to adjust light.

This is the start of a process to make your space you own. You can't control your quarantine situation but you can be intentional about your nest.

What have YOU done to make your space your own? How are you nesting?


Sponsor: Have you tried developing in Rider yet? This fast and feature-rich cross-platform IDE improves your code for .NET, ASP.NET, .NET Core, Xamarin, and Unity applications on Windows, Mac, and Linux.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.