c# class library required

I have below codes and working ok. But I want to make firstname REQUIRED,
so when my jr developer does not code l.firstname = "Sam", it will alert him.
Is it possible?
lead.cs
    public class Lead
    {
        public string subject { get; set; }
        public DateTime createdon { get; set; }
        public string fullname { get; set; }
        public string emailaddress1 { get; set; }
        public string firstname { get; set; }
        public string lastname { get; set; }
        public string telephone1 { get; set; }
        public string address1_name { get; set; }
        public string address1_city { get; set; }
        public string address1_stateorprovince { get; set; }
        public string address1_postalcode { get; set; }
        public string companyname { get; set; }
        public decimal revenue { get; set; }
        public DateTime new_primarydob { get; set; }
        public int leadsourcecode { get; set; } //optionset
        public int leadqualitycode { get; set; }
        public string websiteurl { get; set; }
    }

aspx.cs
	    Domain.Lead l = new Domain.Lead();
            l.subject = "Test";
            l.firstname = "Sam112323";
            l.lastname = "Yu";
            l.leadsourcecode = 8;
            l.leadqualitycode = 2;
            l.createdon = DateTime.Now;

            CRM.Service ser = new CRM.Service();
            string x = ser.CreateLead(l);
            Response.Write(x);

Open in new window

LVL 1
ITsolutionWizardAsked:
Who is Participating?
 
ste5anConnect With a Mentor Senior DeveloperCommented:
Well, as it looks like a DTO in DAL/repository scenario, e.g.

namespace Dto
{
    public interface IDataTransferObject
    {
        bool IsValid(out string validationMessages);
        string Serialize();
    }

    public class Lead : IDataTransferObject
    {
        public string FirstName { get; set; }

        public string FullName
        {
            get
            {
                return this.FirstName + " " + this.LastName;
            }
        }

        public string LastName { get; set; }

        public bool IsValid(out string validationMessages)
        {
            bool result = true;
            if (string.IsNullOrEmpty(this.FirstName))
            {
                result = false;
                validationMessages += "FirstName must be set.";
            }
            else if ( string.IsNullOrEmpty(this.FirstName.Trim()))
            {
                result = false;
                validationMessages += "FirstName must be set.";
            }

            if (string.IsNullOrEmpty(this.LastName))
            {
                result = false;
                validationMessages += "LastName must be set.";
            }
            else if ( string.IsNullOrEmpty(this.LastName.Trim()))
            {
                result = false;
                validationMessages += "LastName must be set.";
            }

            // etc.
            return result;
        }

        public string Serialize()
        {
            return "your serialized DTO."
        }
    }
}

Open in new window


with
Lead l = new Lead();
l.subject = "Test";
l.firstname = "Sam112323";
l.lastname = "Yu";
l.leadsourcecode = 8;
l.leadqualitycode = 2;
l.createdon = DateTime.Now;
string validationMessages;
if (l.IsValid(out validationMessages))
{
	Response.Write(l.Serialize());
}
else
{
  // show messages.
}

Open in new window


btw, stick to coding guidelines, like consistent naming. Also how can fullname have a public setter in your sample?

p.s. or consider using contracts.
0
 
käµfm³d 👽Connect With a Mentor Commented:
If some piece of data is required by a class, then that piece of information should be passed via the constructor. That is the only way to enforce (at compile time) that the data is required.
0
 
Nitin SontakkeConnect With a Mentor DeveloperCommented:
In addition, needless to mention,  make default parameter-less constructor private.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
AndyAinscowFreelance programmer / ConsultantCommented:
ps.  A class library (see question title) is something totally different.
0
 
ITsolutionWizardAuthor Commented:
sorry, i do not get it. please use my example if possible.
0
 
ste5anSenior DeveloperCommented:
Did you create an ER-diagram? Can you post it?
0
 
ITsolutionWizardAuthor Commented:
No
0
 
ste5anSenior DeveloperCommented:
Oops. Wrong thread... ;)

sorry, i do not get it. please use my example if possible.
I did. I just removed the unnecessary fields for a shorter sample.

 Did you test it?
0
 
käµfm³d 👽Commented:
@ste5an

Bit of a skewed distribution, don't you think?
0
 
ste5anSenior DeveloperCommented:
LoC and its more in the spirit of class library.. scnr.

hmm, how to change this?
0
 
ste5anSenior DeveloperCommented:
All are possible approaches to the given problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.