lundi 25 janvier 2016

POCOs, DTOs, DI in an N-Layered ASP.NET MVC Application

Did I just throw a bunch of concepts and technologies that I want to learn about ?

  • First Problem (Writing maintainable code)

    No, I need DI to follow best practice as the applications I'm building are starting to get complicated and I need to write more organized and maintainable code.

  • Second Problem (My POCOs are heavy)

    My application has large & complex entities and when I read about DTOs they seemed like a good practice instead of using POCOs all over the place

You can help me by proposing a project structure keeping in mind the below points from my long research:

  • I found out that I can implement MVVM but the articles about this topic is really limited without including WCF and other components that will distract me while I'm just getting started.
  • I'm not sure if I got this right or wrong but POCOs are meant to be used by the Bussiness Layer only ? For communication betwen DL and Views I should use DTOs.
  • I should have a separate layer (mapper) to populate DTOs and POCOs but I also found that AutoMappers can complicate things, while my DTOs are quite simple and I shall ask if there's any best practices about where should I place this code in my projects ?

  • Based on these points do I need a ViewModel ? or DTOs ? or both ?

  • If you can add some code to your answer along with a simple example to how the cycle will work that would be very nice.

I know some of these points or maybe all are addressed in lots of places including stackoverflow community but I couldn't really find an article or a question that addresses my exact problems and lots of articles are actually providing wrong information so I hope I can get some good advice here. Thanks!


Edit 1

To the people who will down vote this. You either have the option to do so feel free you are following the rules. or you can actually help someone who's done his research and asking a legitimate question that people write articles and books about it. I don't need a whole book just a proposed structure that you use and find it good. It's that simple! I really need some help here!

Which designer patterns used to create complex objects?

How I can simplify creating this objects? I try use Design Patterns Composite and Builder but I'm but I'm confused.

public class tcLoteRps
{
    public string Id { get; set; }
    public List<tcRps> Rps { get; set; }
}

public class tcRps
{
    public tcInfRps InfRps { get; set; }
    public string Signature { get; set; }
}

public class tcInfRps
{
    public tcIdentificacaoRps IdentificacaoRps { get; set; }
    public string Id { get; set; }
    public tcDadosServico Servico { get; set; }
}

public class tcIdentificacaoRps 
{
    public string Id { get; set; }
}

public class tcDadosServico
{
    public string Id { get; set; }
}

Positional Anchor System

So just for fun I try to implement an anchor system for positionable objects, being the basis for a drag and drop UI system.

As such an object has a relative position (x,y) to a parent, if that parent exists. Else the given position is absolute.

When coming down to describing an area with (x,y,w,h) and having it's 5 + relevant anchors (corners + center and maybe center of edge anchors) I already get a load of problems:

If I want to reposition the object, I need to change all anchors. I can of course make the position of all anchors relative to another anchor that represents the position of the area, but if I move a single anchor I also want to move the object itself. Features like "snapping anchors together" and the like come to mind. But the interaction between all these objects starts to get really complicated really quickly. Connecting each anchor with the right others and not having messages being sent between each other on every occasion or getting into loops within the system seems like a daunting task.

Since looking for information about imlementing such an anchor system seems really tedious since I get 100% HTML Anchor questions in search results, I am asking: How do I start my journey into this nightmare? Are there well working patterns that I am already missing?

Is this nested namespace inheritance pattern well implemented?

I´m trying to implement some nested namespaces with objects, and objects that inherit from those objects. I want to know if this first approach is well implemented:

// Namespaces
var firstDomain = {};
firstDomain.firstSubDomain = {};
firstDomain.firstSubDomain.secondSubDomain = {}

/*
* Base Object
*/
firstDomain.firstSubDomain.secondSubDomain.baseObject = (function(baseObject) {
  baseObject.init = function() {};

  return {
    init: baseObject.init
  }

})(firstDomain.firstSubDomain.secondSubDomain.baseObject || (firstDomain.firstSubDomain.secondSubDomain.baseObject = {}));

/*
* Extended Object
*/
firstDomain.firstSubDomain.secondSubDomain.extendedObject = (function(extendedObject) {
    extendedObject.init = function() {};
    extendedObject.init.prototype = Object.create(firstDomain.firstSubDomain.secondSubDomain.baseObject.init.prototype);
    extendedObject.init.prototype.extraFunction = function() {};

    return {
      init: extendedObject.init
    }

})(firstDomain.firstSubDomain.secondSubDomain.extendedObject || (firstDomain.firstSubDomain.secondSubDomain.extendedObject = {}))

// Objects instances

// Base object instance
var baseObject = new firstDomain.firstSubDomain.secondSubDomain.baseObject.init();

// Extended object instance and executing particular method
var extendedObject = new firstDomain.firstSubDomain.secondSubDomain.extendedObject.init();
extendedObject.extraFunction();

console.log(firstDomain);

Here is the repo link:

http://ift.tt/1QvShVR

Can we clear the confusion about MVC in php?

I'm here again. Still studying MVC.

Well there's a tone of contradictions around the web. I would like to clear all those things. But i can't because i'm still a noob in that. So to avoid learning wrong things i would like to ask you about this point.

THE MODEL VIEW CONFUSION (AKA MVC)

Jokes apart, this title reflects the web confusion about the MVC pattern ''implemented'' in PHP.

There are articles who says that the MODEL and the VIEW will NEVER interact directly. Instead the CONTROLLER takes place and handle the situation.

It ask the model to do something and return something, then it ask to the view to present the data that the model returned. All fine, right?

NOPE

I've landed here, where it says the complete opposite.

For example, this article on sitepoint which states:
"It is important to note that in order to correctly apply the MVC architecture, there must be no interaction between models and views: all the logic is handled by controllers."

This is wrong. It specifies this nowhere in MVC. In fact, it states the exact opposite. This article is a good example because it epitomises the attitude of most PHP developers and what's taught as "MVC" throughout the PHP community.

So, who is right then? Can the view acess directly to the model or not?

Is there a good solution to a composite pattern, where a root element is needed, but no element can be root?

I have a structure where, executing Update(); on the root element will recursively traverse the structure and execute Update(); on all child nodes.

This works just like I want it to, however, no element seem to qualify as being the root element. It looks like this

A simplified version of the data structure

This is what I ended up doing. I created a class called GameHandlerRoot, which does nothing, except act like a root. Call Update(); on this, and everything updates. Is this viable? There must be better solutions than a fake.

To be specific, even if you could find an element that would qualify as a root, I am only interested in a solution, where no such element exists.

Thank you in advance.

Regex pattern issue c#

I have a little trouble here. I am trying to find a Regex which will match all fallowing examples:

  • Method( "Team Details" )

    Should return: Team Details

  • Method( @"Team details item

    and more

    and More" )

    Should return: Team Details item and more and more

  • Method( "Team \"Details\"" )

    Should return: Team "Details"

  • Method( @"Team ""Details""" )

    Should return: Team "Details"

  • Method( @"Team

    ""Details""" )

    Should return: Team

    "Details"

I managed to create a regex pattern:

Method\(.*\"(.*)\".*\)

whcih is working in the first example and partially second one, but I found it very difficult to create one pattern for all 5 examples. Any help ?

Cheers