Add dependency injection and appsettings support to C# console app with dotnet core

AppSettings support

Access environment variables from c# console app:

var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

Loading appSettings.json and appSettings.ENVIRONMENT.json

var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", false)
    .AddJsonFile($"appsettings.{environmentName}.json", true)
    .AddEnvironmentVariables()
    .Build();

Map configuration values to a custom class

var customSettings = configuration.Get();

DI (Dependency injection) support

var serviceProvider = new ServiceCollection()
    .AddSingleton(config)
    .AddSingleton()
    .BuildServiceProvider();

Call your service and service provider will inject ConfigModel into PersonService if it’s added in constructor.

var personService = serviceProvider.GetService();
personService.YourMethod();

NuGet packages

<ItemGroup>
   <PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.8" />
   <PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.8" />
   <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.10" />
</ItemGroup>

Open contacts activity from your app

Navigation between applications in Android it’s smooth. You can go back and forward through activities without noticing which are built in and which are not.

The example below will show you a way of selecting a contact from your application, but first starting the built in functionality and then passing into your app the selected item, in our case only the display name.

 

Button click handler that will open Contact activity

private OnClickListener onClickPickContact = new OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 0);
}
};

 

How to grab that picked contact and show it in a toast over the screen

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (resultCode == Activity.RESULT_OK && requestCode == 0) {
Cursor cursor = getContentResolver().query(data.getData(),
new String[] { ContactsContract.Contacts.DISPLAY_NAME },
null, null, null);

if (cursor.moveToFirst()) {
int columnIndex = cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String name = cursor.getString(columnIndex);
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG)
.show();
}
}

};

Create a shape in Android

Web

All about Shapes can be found here. A simplest example of how to create a shape is the code snippet below. Save this xml under the drawable folder.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFF0000"
android:endColor="#80FF00FF"
android:angle="45"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="8dp" />
</shape>

To use this shape, just set the background property of an layout element using the “@drawable/shape_file_name”.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/colors"
android:layout_width="match_parent">
</RelativeLayout>

 

 image

With Endava at Hackathon

I had a great experience participating on Endava’s  Hackathon. logo_jpgThis event hosted at Casa Moțească was a big success in my option because I saw passion and willing for this to go well. Congrats to Adela and Diana for organizing this event.

I felt so good with ICARUS team, working tougher and struggle to build something to make our life’s easier. That’s was the Hackathon tagline.

 

561507_497378413619266_663196588_n

Icarus team ( from left to right) : Cristian Blaga, Viorel Petrea, Mihai Moldovan, Emil Marginean, Sebi Boga

 

47181_426993690698838_409681232_n

 

We build a solution to help everyone track and measure energy value of foods they eat. As a team we imagined this context in which as a simple user you could search for products or restaurant menus and create your recipes in order to keep your fit or  even loose some weight. But as a selling person/manufacturer you could promote your product for a fee. We offered to our users a nice user experience, interactive UI and also accurate data. 

Few screenshot’s below:

 

image

 

image

Return string as JSON or XML from WCF service

If you are in a case where you build your response dynamic and you end up having your response in a string you could return it as JSON or XML and not translated as string.

First make sure your web service method returns an object of System.ServiceModel.Channels.Message type.

[WebInvoke(UriTemplate = "", Method = "POST")]
public System.ServiceModel.Channels.Message GetData(QueryInfo info)
{ ... }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

During method execution you can access response and request using WebOperationContext.Current.

 

To set output response content type use:

WebOperationContext.Current.OutgoingResponse.ContentType = "application/json";

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

To set output response content use: opResult – is your response in json format

 return WebOperationContext.Current.CreateStreamResponse(new MemoryStream(Encoding.UTF8.GetBytes(opResult)), "application/json");

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Mobile Design Patterns&Navigation

Information’s extracted from Mobile Design Pattern Gallery: UI Patterns for Mobile Applications, I recommend you to read this book because has a lot of great content.

“Good navigation, like good design, is invisible.”

 

Springboard (Launchpad)

Use grid layout for items of equal importance, or an irregular layout to emphasize some items more than others.

Common layouts are 3×3, 2×3, 2×2, 1×2

image

 

Old versions but it’s showing great details about this pattern.

image

imageimage

 

Profile information can be added.

image

 

List menu

Works well for long titles or those that require sub text. All internal screens should offer an option for returning to List Menu.

image

image

image

 

Tabs

Tabs navigation is not OS neutral since each OS has their own guidelines for tab location and design.

image

image

image

 

Gallery

Surfaces individual pieces of content for navigation (articles, recipes, photos or products) can be arranged in a carousel, grid or slideshow.

Works great for frequently updated content.

image

image

 

Dashboard

image

How to sync LiveWriter drafts to Dropbox

Still having problems with synchronization, apparently Dropbox doesn’t detect the new changes made in the junction parent folder.

For me the Windows Live Writer is the best tool writing posts. A simple tutorial can be found here.
I like it because I can start writing the post offline, adding screenshots and save the draft locally. It’s like in Word.

What I would like is to keep this draft documents synced because I want to modify posts and publish them again.
So, I would like to use Dropbox the keep them synced.

Dropbox doesn’t know how to sync other directories besides the root directory choosen at install.
Livewriter only knows to save drafts in user documents folder.

 

So, the trick is to use a tool from Sysinternals called Junction which knows to create directory symbolic link.
In other words, you can create a directory link/shortcut in a folder like it were physically there.

Download Junction and run a command like this:

 

image

 

If you navigate to Dropbox folder:

image

 image

 

And now Dropbox thinks Livewriter is a new directory and starts to sync files.