vendredi 14 août 2015

Map lists of nested objects with Dapper

I'm using Dapper and I have classes like this:

public class Article{
   public int Id { get; set; }
   public string Description{get;set;}
   public Group Group { get; set; }
   public List<Barcode> Barcode {get;set;}
   ...
}

public class Group{
   public int Id { get; set; }
   public string Description {get;set;}
}

public class Barcode{
   public int Id { get; set; }
   public string Code{get;set;}
   public int IdArticle { get; set; }
   ...
}

I can get all information about Article but I would like to know if is possible with one query get also the list of barcodes for each article. Actually what I do is this:

string query = "SELECT * FROM Article a " +
"LEFT JOIN Groups g ON a.IdGroup = g.Id ";

arts = connection.Query<Article, Group, Article>(query,
    (art, gr) =>
    { art.Group = gr;  return art; }
    , null, transaction).AsList();

I also found a good explanation here but I don't understand how to use it in my case, because I have also the Group class. How should I do this with Dapper, is it possible or the only way is to do different steps? Thanks



via Chebli Mohamed

adding reference to class library

I have a class library lets call it UtilityLibrary.

I have a console application. So I right clicked on my solution and added an existing project (UtilityLibrary). I noticed that I can change the code of UtilityLibrary from within my console application.

The issue is if I had added UtilityLibrary to another application and the code had been changed it could causes issues. I was trying to avoid adding a dll reference so thought I would add a reference to my project however I am worried about the code being edited.

Have I added the reference to my project incorrectly?



via Chebli Mohamed

Simultaneous output in the console and to a file

Is there a way in .NET to write the output stream Stream two at once?

That is, do Console.SetOut() for Console.Out and for StringWriter for example. To all that is written in the console at the same time it was written in the file.



via Chebli Mohamed

Sessions are killed after short time in 64bit application pool

We have a .net web application hosted on IIS 7.5. Earlier this application was running on a 32bit application pool but some time ago we've switched to 64 bit application pool.

Recently users have started to complain that after 1-2 minutes of idling their session is being killed which we have confirmed today.

In the web.config file the session timeout is set to 60 minutes. Also we have noticed in task manager that the w3wp process for this application consumes about 2-2,4GB of memory so maybe the problem is that the application pool is trying to recycle some memory?

The recycling is set to limited time periods 21:00 and 4:00

What could be the reason for this problems with sessions?



via Chebli Mohamed

mvc input one to many

I'd like to get some help with this :

<table>
<tr>
    <td>
        <select id="specieName" name="specieName">
            foreach(var item in Model)
            {
                <option>@item.potatoSpecie</option>
            }
        </select>
    </td>
    <td><input size=25 type="text" id="potatoName" name="potatoName"/></td>
    <td><input size=25 type="text" id="potatoSize" name="potatoSize"/></td>
</tr>

public class potato
{
    public potato()
    {
        category - new HashSet<category>();
    }
    public string name name { get; set; }
    public virtual ICollection<Category> Category { get; set; }
    public int IdPotato { get; set; }
}

public class category
{
    [Key]
    public int IdPotato { get; set; }
    public int potatoSize { get; set; }
    public virtual potatoSpecie potatoSpecie { get; set; }
}

public class specie
{
    public string specieName { get; set; }
    public int specieId { get; set; }
}
public ActionResult Potato(string submit)
{
    var new hotPotato = new potato();
    {
        hotPotato.category.potatoSize = int.Parse(Request.Form["potatoSize"]);
        hotPotato.name = Request.Form["potatoName"];
        hotpotato.category.specie.specieName - Request.Form["specieName"];
    }
    using(potatoesContext context - new potatoesContext())
    {
        context.potato.add(potato)
        context.SaveChanges();
    }
    return View(potato);
}

From here, there's a few things I'd like to understand, can I retrieve both razor and regular fields with one action ? is there a better way to do it than Request.Form? The biggest problem from here is that I don't exactly know where to go after that, I'm trying to put these potatoes in my database and I'd like to at least be sure if I'm on the right direction, thanks beforehand.



via Chebli Mohamed

Cannot find named event when created in Windows Service

I am developing a Windows Service in C# to centrally manage some application connectivity. It's a sleeper service in general, which performs some actions when awoken by an external executable. To this end I'm using named events, specifically the .NET EventWaitHandle. My code boils down to, at the service end:

        EventWaitHandleSecurity sec = new EventWaitHandleSecurity();
        sec.AddAccessRule(new EventWaitHandleAccessRule(
                  new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                  EventWaitHandleRights.FullControl,
                  AccessControlType.Allow));
        evh = new EventWaitHandle(false, EventResetMode.AutoReset, EVENT_NAME, 
                                  out created, sec);

        Log(created ? "Event created" : "Event already existed?");

As it's an internal application on trusted servers I don't mind that granting 'Full Control' to 'World' in general wouldn't be smart.

At the client end I have:

EventWaitHandle.TryOpenExisting(EVENT_NAME, EventWaitHandleRights.Modify, out evh)

The code above works perfectly when I run my service in console-based interactive mode. The event is found on both ends, the client can set, and the service kicks to work. Everybody's happy.

When installing the service however it doesn't work. The logging still reports that the event was created anew, but the client cannot find the event. As I thought it was security-related I added the World Full Control Allow access rule, but it didn't change anything. I changed the service to run as Local Admin, even as my own user account, but nothing - the client cannot find the event even though logs show the service is happily polling away on it. If I change the TryOpenExisting to OpenExisting I get an explicit exception:

System.Threading.WaitHandleCannotBeOpenedException: No handle of the given name exists.

What am I missing?



via Chebli Mohamed

App that monitors changes in a folder and reacts to them C#

I'm new to C# programming and I need some help. I have to write an app that, after an authentication phase, monitors the changes of a folder and when something happens (a file is added/deleted/updated) reacts by sending a notification to a server. What is the best way to do that? A windows service launched after the authentication? Please note that this monitoring activity should be performed while in parallel the user is navigating the ui.

Thank you all!



via Chebli Mohamed