Year: 2018

Latest commits notes from github

For AOP with Roslyn I want to automatically get the latest commits message before doing a new commit.

This was  solved easily using https://github.com/octokit/octokit.net 

The code is  very simple:

public async Task<DateForCommit[]> BetweenLatest2Commits()
{
           
var client = new GitHubClient(new ProductHeaderValue("GitBetweenCommits"));
var rep = await client.Repository.Get(Author, RepositoryName);
var relLatest = await client.Repository.Release.GetLatest(rep.Id);
var relAll = await client.Repository.Release.GetAll(rep.Id, new ApiOptions() { PageSize = int.MaxValue - 1 });
var relBeforeLatest = relAll.OrderByDescending(it => it.CreatedAt).Skip(1).FirstOrDefault();

var dateLatest = relLatest.CreatedAt;
var dateBeforeLatest = relBeforeLatest.CreatedAt;

var commits = await client.Repository.Commit.GetAll(rep.Id, ApiOptions.None);
var res = commits
    .Where(it => 
    it.Commit.Author.Date >= dateBeforeLatest
    &&
    it.Commit.Author.Date<=dateLatest
    )
    .Select(it => new DateForCommit()
    {
        Author = it.Commit.Author.Name,
        Message = it.Commit.Message,
        CommitDate = it.Commit.Author.Date.DateTime
    })
    .ToArray();
return res;
}

It works also as a .NET Global tool
Install with

dotnet tool install –global dotnet-gcr

run with

dotnet gcr

for example for https://github.com/ignatandrei/AOP_With_Roslyn the arguments are

dotnet gcr ignatandrei AOP_With_Roslyn

NuGet at https://www.nuget.org/packages/dotnet-gcr/

Full Code at https://github.com/ignatandrei/GitCommitsBetweenReleases
enjoy

Connections strings to config

I have the opportunity to work on some pretty old code . There were many projects , all that had a sort of connection string to the database.  This kind of code were in like > 11 places :

string connectionstring = “Data Source=.\\SqlServer;Initial Catalog=myDB;Integrated Security=SSPI;”;

The task was to modify this in something that could read from a config ( json) file . But more than that, was to verify
1.that is called everywhere – because sometimes it were in methods, other time in a static variable and so on .
2. that the json exists or not ( if not return the default string – but log the fact that are not in the settings)

So – how to monitor that your code is actually hit ? One idea is to make breakpoints. Other is to make a class:

So I came up with this class

public class ConnectionSql
{
public ConnectionSql(
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{

Trace.WriteLine("member name: " + memberName);
Trace.WriteLine("source file path: " + sourceFilePath);
Trace.WriteLine("source line number: " + sourceLineNumber);
}
//TODO: if need speed, make this static - or strConnection...
public string ConnectionString()
{
var strConnection = ConfigurationManager.AppSettings["sql"];
if (string.IsNullOrWhiteSpace(strConnection))
{
Console.WriteLine("not found connection sql string in settings , going to default");
strConnection = ".\\SqlServer;Initial Catalog=myDB;Integrated Security=SSPI;";
}
return strConnection;

}

}

 

How we call ?


string connectionstring = new ConnectionSql().ConnectionString();

 

Because of the attributes https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/caller-information we can figure by looking at the trace if the code was hit or not and by which line.

Circular references on .NET , Entity Framework and WebAPI

Imagine having a class Department( ID, Name) and Employee ( ID , Name, IDDepartment) . You want to return in the WebAPI the Departments with the Employee . It is simple to wrote this code:


[HttpGet]
public IEnumerable<Department> Get([FromServices] MyTestDatabaseContext context)
{
var departments = context.Department.Include(iterator=>iterator.Employee).ToArray();
return departments;
}

But the problem is with circular references when serializing  : The Department has a list of  Employees  that have a Department that have a list of Employees that …

 

 

Solution 1 :  Delete/Comment from the EF generated code what you do not need ( in this case , the Department reference that the Employee has)


public partial class Employee
{
public int Idemployee { get; set; }
public string NameEmployee { get; set; }
public int Iddepartment { get; set; }

//public Department IddepartmentNavigation { get; set; }
}

Solution 2 : Nullify the reference


[HttpGet]
public IEnumerable<Department> Get([FromServices] MyTestDatabaseContext context)
{
var departments = context.Department.Include(iterator=>iterator.Employee).ToArray();
foreach (var dep in departments)
{
foreach (var emp in dep.Employee)
{
emp.IddepartmentNavigation = null;
}
}
return departments;
}

( maybe this should be add to the partial class of the employee ?)

 

 

Solution 3: Handle circular references in serialization


services
.AddMvc()
.AddJsonOptions(opt =>
{
opt.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});

Solution 4: Make a read DDD design. Read https://siderite.dev/2018/08/client-models-vs-data-transfer-objects.html

Andrei Ignat weekly software news(mostly .NET)

* indicates required

Please select all the ways you would like to hear from me:

You can unsubscribe at any time by clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.