Archives

Archives / 2008 / August
  • Played around with Microsoft Extensibility Framework - MEF

    I played around with the June CTP of the Microsoft Extensibility Framework (MEF). I wanted to try both Dependency Injection (DI) and adding classes during runtime to extend an application with more logic without shutting down the main app. As an example I created a LogManager, I thought of the most simple possible thing to do. The LogManager will use different kind of loggers, to log exceptions. During runtime I wanted to add more loggers. For example lets pretend we are building an app where we log all exceptions into a database, suddenly we need to log it into a XML file also. If we don't use a provider based solution, we would probably open our application, add the new code to log into an XML file, recompile and deploy. But what with the MEF we can add the extra logger without recompiling the app, we don't even need to shut it down. (Well, this nothing new, this have bean possible for ages with framework like Spring).

    I will start writing about my Loggers before I go into the LogManager and how to add "components" to the MEF frameworks containers. Here is a common interfaced used by the Loggers:

    Read more...

  • Map Objects to one Object

    In my previous post I wrote about how we could use Dynamic Compilation for mapping object to object in a configuration file, mosly for fun to simply try something different. I spend a few minutes to see if I could do something more with it. It ended up that I can use it to several things, like creating Factories, or send in different kind of objects and map several into one. Here is how I for example can send in more domain object to map them into a PresentationCustomer:

    var presentationCustomer = map.Map<PresentationCustomer>(
                                        "CustomerToPresentationCustomer", 
                                        customer, 
                                        address);

    My configuration file look like this:

    public PresentationCustomer CustomerToPresentationCustomer(Customer customer, Address address)
    {
           return new PresentationCustomer()
                      {
                           ID = customer.ID,
                           CompanyName = customer.CompanyName,
                           FirstName = customer.FirstName,
                           LastName = customer.LastName,
                           Street = address.Street
                      };
    }

    Some comments I got was also to automatically map properties from object with the same name, so I added an extension method called MapTo, to make that possible, it will also open up support for mapping specific properties manually:

    public PresentationCustomer CustomerToPresentationCustomer(Customer customer, string lastName)
    {
          var presentationCustomer = new PresentationCustomer();
    
          customer.MapTo(presentationCustomer);
    
          presentationCustomer.FullName = customer.FirstName + " " + customer.LastName;
    
          return presentationCustomer;
    }


    I did also add support so other developers can simply add their own code which could be used by the configuration file. It's done by adding the namespace and an assembly of the code to the mapper.

    DynamicMapper map = new DynamicMapper();
    
    map.Assemblies.Add("MyHelper.dll");
    map.Namespaces.Add("MyHelper");
    
    var presentationCustomer = map.Map<PresentationCustomer>(
                                                   "CustomerToPresentationCustomer", 
                                                    customer, 
                                                    "Normén");


    Here is the MyHelper:


    namespace MyHelper
    {
        public static class MyExtension
        {
            public static string GetName(this string value)
            {
                return "Fredrik";
            }
        }
    }


    I can now use this extension method in my configuration file:


    public PresentationCustomer CustomerToPresentationCustomer(Customer customer, string Name)
    {
           return new PresentationCustomer()
                      {
                           ID = customer.ID,
                           CompanyName = customer.CompanyName,
                           FirstName = name.GetName(),
                           LastName = customer.LastName,
                           Street = address.Street
                      };
    }


    Because I can pass in several objects and map them to one single object, I can also pass in XmlDocument, DataSet, DataTable and SqlDataReaders etc and map them to an object:


    var presentationCustomer = map.Map<PresentationCustomer>(
                                        "CustomerToPresentationCustomer", 
                                        customer, 
                                        sqlReader);
    public PresentationCustomer CustomerToPresentationCustomer(Customer customer, SqlDataReader reader)
    {
           return new PresentationCustomer()
                      {
                           ID = customer.ID,
                           CompanyName = customer.CompanyName,
                           FirstName = customer.FirstName,
                           LastName = customer.LastName,
                           Street = reader["Street"]
                      };
    }


    I can think of using this solution if I don't use an ORM but still want to bind data from a SqlDataReader to an object, or combine several object and use formatting and specifying default values which could be changed over time, and when it does I don't need to recompile my code, only change the configuration file.

    Read more...

  • How to map a Domain model to a Presentation Model, can this be something?

    If you have read my previous post about Presentation Model, I decided to try to see if I could make the mapping between the Presentation Model and the Domain Model simple by creating an Object to Object Mapper. I don’t want to use a messy XML file or attributes on the classes to handle the configuration of how object should be mapped to each other. Instead I wanted to use the nice Object Initializer feature in C# 3.0 which I think is easy to read and easy to use. Here is a modified version of my Mapping method I had in my CustomersController class in my previous post:

    Read more...

  • What do you think about using a presentation model for presenting your domain model?

     

    I’m writing a small app for a demonstration purpose. What I have is a domain model where I have a Customer, I also have CustomerRepository.

    public class Customer
    {   public int ID { get; set; }
    
           public string CompanyName { get; set; }
    
           public string FirstName { get; set; }
    
           public string Lastname { get; set; }
    }
    public interface ICustomerRepository
    {
           IList<Customer> GetCustomers();
    
           Customer GetCustomerByID(int ID);
    
           void Remove(Customer customer);
    
           void Update(Customer customer);
    
           void Add(Customer customer);
    }

    I decided to not make the Customer immutable and use the new automatic property feature to speed up the creation of the Customer entity. My CustomerRepository have some few method. About the Get methods, should they have the name GetCustomerByID, or GetByID or only Get!? The name of the class indicates that it's a customer I want, do I need to repeat my self and add the word Customer to the method name? The argument of the GetCustomerByID method indicates that I need an ID, so the method could be Get(int ID). But of some reason I feel more comfortable to name it GetCustomerByID, because the method should get a Customer based on a given ID. What do you think, how should you have named that method and why, please feel free to add a comment. This was out of the topic of this blog post, but I'm very interested in how you should have named it.

    I use ASP.Net to build my client. What I was needed was a model for the presentation, I call it Presentation Model. A Presentation Model is a model used for presentation purpose only. For example if I want to bind a Customer entity to a Grid and one column should display the FullName of a Customer, it's not sure that every country want to display the FirstName first. I didn't want to add the logic of how a FullName should be displayed into the code-behind of my page or on the .aspx page. Instead I created a Presentation class for my Customer which has a FullName property.

    public class PresentationCustomer : MyApp.DomainLayer.Entities.Customer
    {
        public PresentationCustomer()
        {
        }
    
    
        public string Fullname
        {
            get { return base.FirstName + " " + base.Lastname; }
        }
    }

    I still wanted to have the same members as the Customer entity so I inherited the Customer class and added a Fullname property to my new Presenstion model. I didn't know what I should named the Presentation class of the Customer, so I used what it is, a presentation of the Customer (PresentationCustomer). In this example I do not have logic to display the FirstName and LastName in a specific order. I often use a Presentation Model in my app, for example say that I have a checkbox, this should be selected or note based on the domain entities state. Instead of adding a condition statement to my code-behind, I use a presentation class with a property which return true or false based on condition, for example let say I have a Customer and I wanted to mark a checkbox if the Customer is and adult or not. The only information my Customer entity has is Age. So instead of adding the code into the code-behind or .aspx page, I created a Presentation class of the Customer and added a IsAdult property, which I bound to a checkbox.

    public bool IsAdult
    {
        get { return (this.Age > 18) }
    }

    This will of course require me to write several of new classes, but by using a Presentation Model I can easy reuse my client even if my underlying data source is changed, just map the new data to my Presentaion Model, so the data could for example be simple XML documents. It's still the Presentation Model my Views will work against and not directly to my domain model. As you may know if we use a ObjectDataSource and bind an object to it, only the fields which are bounded to the Grid will be updated and passed down to the Update method. In that case we can't map our ObjectDataSource directly to a Repository. Well we can hook up to the Updating event and get the original customer and make sure the unbound field will be filled with the original value instead of getting the null value. But use a another solution to reduce the code in the code-behind, I use a "Controller", and each View can have it's own "Controller" which can handle View specific problems. So I can add a Update method to my "Controller" which will be used by a ObjectDataSource, this method can then help me solve the null problem with unbound fields.

    When I use a Presentation Model, I need a way to easy map my Customer to a PresentationCustomer, this can for example be done by using constructor injection, where I inject the Customer entity into the PresentationCustomer's constructor.

    public class PresentationCustomer
    {
        Customer _customer;
    
        public PresentationCustomer(Customer customer)
        {
            this._customer = customer;
        }
    
        public string Fullname
        {
            get { return this._customer.FirstName + " " + this._customer.Lastname; }
        }
    }

    In this case if I want to use the Customer entity's members, I need to add all of them again to the PresentationCustomer class. This is something that I didn't wanted to do. I wanted to reuse as much code as possible. So I decided to inherit the Customer class instead and do a manual mapping by using code. I could have used a object to object mapping tool, but that may require me to setup the mapping, and the time to do that is the same time to write the mapping by code.

    To make sure my Views work against my Presentation Model, I created a new class which should also have the responsibility to handle the flow and state of my Views. I'm so bad at naming things, but I used the name CustomerController. This class has several method to handle navigation after a new customer is added or updated etc. Two methods that I have is ViewCustomer and GetCustomers. The ViewCustomer method's responsibility is to display a Customer, the GetCustomers should be used by a ObjectDataSource control to fill a Grid with Customers, those method should return and use a PresentationCustomer and not the Customer entity. To do that I need to map all Customer into PresentationCustomer. I did that by using the new object initializer feature of C# 3.0.

    private PresentationCustomer MappCustomerToPresentationCustomer(Customer customer)
    {
         return new PresentationCustomer()
         {
            ID = customer.ID,
            FirstName = customer.FirstName,
            Lastname = customer.Lastname,
            CompanyName = customer.CompanyName
         };
    }

    The ViewCustomer and GetCustomers in the CustomerController now use this method, the implementation of the methods look like this:

    public void ViewCustomer(int? ID)
    {
        if (!ID.HasValue)
            throw new ApplicationException(Resources.Resource.CustomerIDCantBeNullArgumentValidation);
    
        var customer = this._customerRepository.GetCustomerByID(ID.Value);
    
        if (customer == null)
            throw new ApplicationException(string.Format(Resources.Resource.CantFintCustomerByIDErrorMsg, ID));
    
        var presentationCustomer = this.MappCustomerToPresentationCustomer(customer);
    
        base.AddPageState("Customer", presentationCustomer);
        base.MoveToView("CustomerDetails.aspx");
    }
    
    public IList<PresentationCustomer> GetCustomers()
    {
        var customers = this._customerRepository.GetCustomers() as List<Customer>;
    
        var presentationCustomers = new List<PresentationCustomer>();
    
        customers.ForEach(c => presentationCustomers.Add(this.MappCustomerToPresentationCustomer(c)));
    
        return presentationCustomers;
    }

    My question is how should you have done it? Is there a way you think is much easier and faster way to implement it, do you like this approached with a Presentation Model or is there anything that you think is much better? Please feel free to add a comment, I would like to hear about your thoughts and suggestions etc.

    Read more...

  • Who's to blame, Microsoft and/or the .Net Community

    I’m interested in why most of Microsoft and some other developer’s demons about writing apps mostly begin with the creation of a database? The design of the app will more or less in that case be driven by the database schema, after the database in place the Data Access Layer will be built and the entities will mostly reflecting the database schema. A data base like Microsoft SQL Server is a relation database, so our model will more or less be a relational model instead of a more object oriented model. That can make it harder to use good design patterns and principles to solve some Implementation problems. OO has a rich toolset for modeling the domain than the relation model.

    Read more...