SBERRY at Tech

Ever wondered what if  we separate our models classes  into a different project and access them via other Project. How does it look in Code? Yes, it’s the time to see it in action with SBerry.

Before we begin, let me show you the  Shop project snapshot.

Shopie DataModel
Shopie Data Model

Let’s create our model classes called Data Models in a project called Shop.

namespace Shop
{
public partial class Shopie
{
public int ShopieId { get; set; }
public String ShopieName { get; set; }
public string Landmark { get; set; }
public decimal MinPrice { get; set; }
public String Email { get; set; }
}
}

And for now I keep DataAnnotations for the Shopie class with in the same project.

using System.ComponentModel.DataAnnotations;
namespace Shop
{
[MetadataType(typeof(ShopElob))]
public partial class Shopie
{
}
public class ShopElob
{
[Display(Name = "Shopiez Name")]
public String ShopieName { get; set; }
[DisplayFormat(DataFormatString = "{0:0.00}")]
public decimal MinPrice { get; set; }
[DataType(DataType.EmailAddress)]
public String Email { get; set; }
}
}

It’s time to access these Data Models in other Project which is of course the UI project as DataAnnotationsDemo and here is the Snapshot.
DataAnnotationDemo
Steps in to create reference between projects

      1. Right click on the References section of the DataAnnotationsDemo project to open up the Add Reference Pop up Window.
      2. click browse and navigate to the desired project’s bin->debug and select the dll.
      3. Once done, start accessing the referenced project via it’s namespace.
        Eg: using Shop;

        • Here In Shop project you could see that the namespace we provided is simply Shop. So we access it using just simple as
          using Shop;
        • If namespaces is something like Ays.Web.DataModel then we have to access it in as using Ays.Web.DataModel;

    namespace.pngNow When you create a strongly typed view, with scaffolding, the code is generated for you taking into considerations the DataAnnotations that are applied. The standard namespace naming is [CompanyName].[TechnologyName].[Feature].[Design]

    index.png And here is the final UI
    output
    Thought this works, but according to Separation of Concerns, the Data Annotations we applied must be still segregated into the presentation layer as  ViewModel so that the view takes up the View Model rather than the actual Data Model.
    Hope it helps and feel free to ask any question or suggest me incredible tips.

Advertisements